android - 数据绑定(bind) : set property if it isn't null

标签 android android-databinding

无法理解...如何仅在变量字段不为空的情况下设置 View 的某些属性?
例如

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="item"
            type="com.test.app.Item" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_margin="16dp"
            android:src="@{item.getDrawable()}"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginEnd="16dp"
            android:layout_marginLeft="72dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="72dp"
            android:layout_toLeftOf="@id/action"
            android:layout_toStartOf="@id/action"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textColor="@color/black_87"
                android:textSize="16sp"
                android:text="@{item.getTitle()}"/>

            <TextView
                android:id="@+id/text2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:autoLink="web|email"
                android:linksClickable="false"
                android:singleLine="true"
                android:textColor="@color/black_54"
                android:textSize="14sp"
                android:text="@{item.getSubtitle()}"/>


        </LinearLayout>

    </RelativeLayout>
</layout>

Item 的某些字段可以为空,我不会不必要地调用布局 View 的方法。而且我不会得到 NullPointerException。只有当它不为空时,我才能设置属性?
附言对不起英语。

最佳答案

数据绑定(bind)通常通过检查 NullPointerException 来避免它并分配默认值(例如 null),即使 item 本身在您的示例中为 null。

但对项目属性进行空值检查的基本示例:

android:text='@{item.title != null ? user.title : ""}'

或者使用“Null Coalescing Operator”。 null 合并运算符 (??) 如果不为 null 则选择左操作数,如果为 null 则选择右操作数。

android:text='@{item.title ?? ""}'

请注意,titlegetTitle 无关紧要。

关于android - 数据绑定(bind) : set property if it isn't null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36227194/

相关文章:

android - Android开发中MVP模式下如何控制多线程?

android - canvas.drawBitmap 没有绘制任何东西

java - 检测布局中的第一个按钮

android - 数据绑定(bind)和 MutableLiveData 的问题

android - 在 Android 上使用亚马逊简单存储服务 (S3)

Android CoordinatorLayout,Scrolling Activity 模板所有布局都飞走了

android - 带有数据绑定(bind)的约束布局没有响应

android - 不应该为 RecyclerView 项目 View 使用数据绑定(bind)吗?

android - 使用数据绑定(bind)设置 ImageView 的色调

android - 如何正确地将数字绑定(bind)到 Android editText