Android - 使用数据绑定的条件文本值

Raj*_*ran 13 data-binding android android-databinding

我想有条不紊地将TextView的文本设置为其中一个或另一个.

Android数据绑定文档建议您可以在文本是视图模型的属性时有条件地设置文本.例如

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

但有没有办法从strings.xml中设置文本而不是在我的视图模型中添加它?我想要这样的东西 -

android:text="@{viewModel.expanded ? @string/collapse : @string/expand}"
Run Code Online (Sandbox Code Playgroud)

XML看起来有点像这样:

<?xml version="1.0" encoding="UTF-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bind="http://schemas.android.com/apk/res-auto">
   <data class="TravellerInfoBinding">
      <import type="android.view.View" />
      <variable name="viewModel" type="com.myproject.viewmodel.TravellerInfoViewModel" />

   </data>
   <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
      <ImageView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical"
        android:src="@drawable/expandable_arrow_blue" />

      <TextView style="@style/primary_pair_element_value" 
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content" 
        android:text="@{viewModel.expanded ? @string/taxes_fees_detail : @string/hide_taxes_fees_detail}"
        android:textSize="12sp" />

   </LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

这是我的视图模型 -

package com.myproject.viewmodel;

imports...

public class TravellerInfoViewModel extends BaseObservable {

  @Bindable
  private final TaxDetailsViewModel taxDetailsViewModel;

  @Bindable
  private boolean expanded;

  Constructor....

  public boolean isExpanded() {
    return expanded;
  }

  public void setExpanded(boolean expanded) {
    this.expanded = expanded;
    notifyPropertyChanged(BR.expanded);
  }

  public void toggleExpanded() {
    setExpanded(!expanded);
  }

}
Run Code Online (Sandbox Code Playgroud)

And*_*sky 8

其实对我来说很好

<TextView
    android:id="@+id/btnEdit"
    style="@style/Common.Toolbar.Action.Text"
    android:onClickListener="@{onEditClick}"
    android:text="@{vm.editMode ? @string/contacts_done : @string/contacts_edit}"
    tools:text="@string/contacts_edit"/>
Run Code Online (Sandbox Code Playgroud)

哪里vm-这是一个ViewModel,editMode-是ObservableBoolean


Gen*_* Bo 2

这是一个修复/解决方法:

  1. 定义您想要条件值的布局的重复 Xml 定义
  2. 对于每个块,设置一个条件值
  3. 根据数据绑定布尔值设置每个 Xml 定义的可见性

不是理想的解决方案,不是很漂亮..但功能相同 - 并且在找到正确的解决方案之前可以暂时工作。

以下是我解决该问题的方法,其中我有一个以粗体android:textStyle显示值的特殊情况要求。

    <variable
        name="viewModel"
        type="com.demo.app.SomeViewModel"/>

        ...

    <TextView
        style="@style/RowValue"
        android:visibility="@{ ! viewModel.boldRow ? View.VISIBLE : View.GONE}"
        android:text="@{viewModel.currentValue}"
        />

    <TextView
        style="@style/RowValue"
        android:visibility="@{ viewModel.boldRow ? View.VISIBLE : View.GONE}"
        android:text="@{viewModel.currentValue}"
        android:textStyle="bold"
        />
Run Code Online (Sandbox Code Playgroud)