数据绑定:如果属性不为null,则设置属性

Шах*_*Шах 25 android android-databinding

无法理解......如果变量字段不为空,如何设置视图的某些属性?
例如

<?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>
Run Code Online (Sandbox Code Playgroud)

Item的某些字段可以为null,我不会不必要地调用布局视图的方法.我不会得到NullPointerException.如果属性不为null,我该怎么设置属性?
PS对不起英文.

Mat*_*der 78

好的数据绑定通常通过检查它来避免NullPointerException,并指定默认值(例如null),即使item在示例中它本身为null也是如此.

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

android:text='@{item.title != null ? user.title : ""}'
Run Code Online (Sandbox Code Playgroud)

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

android:text='@{item.title ?? ""}'
Run Code Online (Sandbox Code Playgroud)

注意titlegetTitle无关紧要.

  • 应该可以正常工作,并且还可以使用数据绑定来提高可见性!`android:visibility="@{item.drawable == null ? View.INVISIBLE : View.VISIBLE}"` 你需要在 `data` 元素中为 View 添加一个导入:`&lt;import type="android. view.View"/&gt;` (3认同)

Khe*_*raj 17

数据绑定不需要检查空值,它会由绑定类处理。

如果您需要出于其他目的检查 null(例如设置默认值),那么您可以像这样使用。

android:text='@{item.gender != null ? item.gender : @string/male}'
Run Code Online (Sandbox Code Playgroud)

或者

android:text='@{item.gender ?? @string/male}'
Run Code Online (Sandbox Code Playgroud)

上面两个例子都是一样的。这@string/male是默认值,当item.gendernull


小智 8

我尝试过这样的事情。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

  <data>
     <import type="android.text.TextUtils"/>
     <variable
        name="mOrder"
        type="com.test.app.models.Order" />
  </data>
  <TextView
    android:id="@+id/mLblSource"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text='@{TextUtils.isEmpty(mOrder.order_id) ? "- -" : mOrder.order_id}'
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" />
</layout>
Run Code Online (Sandbox Code Playgroud)