小编Leo*_*der的帖子

DataBinding可见性取决于列表大小

我有一个在数据绑定中用作变量的对象。

public class Company {
    private int id;
    private String name;
    private List<Employee> employees;
    private List<Room> rooms;
}


<data>
     <variable
         name="item"
         type="com.blablabla.model.entity.Company"/>

</data>
Run Code Online (Sandbox Code Playgroud)

想要根据列表大小(员工)更改视图的可见性,因此,如果列表为空或空-可见性为GONE,否则为可见。

到目前为止,我尝试了什么:
1)直接设置可见性:

  android:visibility="@{item.employees.size() > 0 ? View.VISIBLE : View.GONE}"
Run Code Online (Sandbox Code Playgroud)

实际上,能见度一直都没有。

我当然进口了

<import type="android.view.View"/>
Run Code Online (Sandbox Code Playgroud)

2)使用BindingConverter:

@BindingConversion
    public static int convertCollectionToVisibility(Collection collection) {
        Log.d(TAG, "collection: " + (collection == null ? "null" : collection.size()));
        return collection == null || collection.isEmpty() ? View.GONE : View.VISIBLE;
    }
Run Code Online (Sandbox Code Playgroud)

然后在布局中:

android:visibility="@{item.employees}"
Run Code Online (Sandbox Code Playgroud)

此处的日志显示,该集合始终为null。但这绝对不是

Company company = new Company(1, "Company 1");
List<Employee> employees = …
Run Code Online (Sandbox Code Playgroud)

android android-databinding

5
推荐指数
2
解决办法
3391
查看次数

MotionLayout 和约束组

我将开始使用MotionLayout.
ConstraintLayout具有简单运动场景的 MotionLayout替换现有后,
我注意到它androidx.constraintlayout.widget.Group不再起作用了。
最初,我根据条件显示了两个组中的一个,
但现在它们都可见,即使我将可见性设置为GONE.
我能否以某种方式让 MotionLayout 与 Groups 一起工作,或者我应该使用不同的 MotionLayouts?

android android-motionlayout

4
推荐指数
1
解决办法
792
查看次数

折叠工具栏和DrawerLayout

我创建了一个具有视差效果的布局,我在片段中使用它.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/collapsingImageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="fitXY"
                android:src="@drawable/baner_1"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />   
        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!--OTHER SCROLABLE STUFF--> 

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

只要片段在Activity的根目录中膨胀(进入container_main),它就可以正常工作.
所以Activity的布局如下所示:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

现在我想在我的Activity中添加一个DrawerLayout,这就是我卡住的地方.
我通过添加DrawerLayout更改了Activity的布局.
然后将上面提到的片段膨胀成相同的container_main,但是既没有视差也没有NestedScrollView工作.

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
    </FrameLayout>

    <ListView
        android:id="@+id/drawerListView"
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        />
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

使用DrawerLayout和使用折叠工具栏的诀窍在哪里?

android android-layout drawerlayout android-toolbar android-collapsingtoolbarlayout

3
推荐指数
1
解决办法
6157
查看次数

Android Annotations - 这是一个很好的做法吗?

我是初学Android开发人员(开发1.5年).
在此期间,我的主要研究来源是
www.developer.android.com和关于Udacity的谷歌培训.
最近我收到了一个重构项目,这个项目是由一位资深开发人员创建的,他深入使用了Android Annotations,在重构代码时我有点沮丧.因为这段代码看起来与我之前学到的很多不同.
然后我在一些项目中实现了Android Annotations,并惊讶于在处理REST服务,分离背景和UI任务等方面有多么方便.
所以问题是:

  1. 在代码样式约定和其他术语方面使用Android Annotations是一个好习惯吗?
  2. 什么时候(在哪个项目中)我应该使用它们什么时候不应该?

android-annotations

1
推荐指数
1
解决办法
169
查看次数