是否可以在xml中绑定两个变量?

时间:2017-12-14 19:02:37

标签: android android-databinding

此处 point_of_sale_list_item.xml

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

    <data>    
       <variable
            name="item"
            type="com.myproject.android.customer.api.model.Merchant" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageViewPhoto"
            android:layout_width="90dp"
            android:layout_height="90dp"/>

        <TextView
            android:id="@+id/phoneTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

    </android.support.constraint.ConstraintLayout>

</layout>

这里是root适配器:

public abstract class PreviewSortAdapter extends RealmRecyclerViewAdapter {

    public PreviewSortAdapter(Context context, @Nullable OrderedRealmCollection data, boolean autoUpdate) {
        super(data, true);
        setHasStableIds(true);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, viewType, parent, false);
        setClickHandler(binding);
        return new PreviewBaseViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Object obj = getItem(position);
        PreviewBaseViewHolder previewViewHolder = (PreviewBaseViewHolder) holder;
        ViewDataBinding viewDataBinding = previewViewHolder.getBinding();
        viewDataBinding.setVariable(BR.item, obj);
        viewDataBinding.executePendingBindings(); //binding must be executed immediately
    }

    @Override
    public int getItemViewType(int position) {
        return getLayoutForPosition(position);
    }

    protected abstract int getLayoutForPosition(int position);

    private class PreviewBaseViewHolder extends RecyclerView.ViewHolder {

        private final ViewDataBinding binding;

        private PreviewBaseViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        private ViewDataBinding getBinding() {
            return binding;
        }
    }

}

我使用方法setVariable()将变量“ item ”与模型绑定。

这里有具体的子适配器:

public class MapListSortAdapter extends PreviewSortAdapter {

    private static final String TAG = MapListSortAdapter.class.getName();

    public MapListSortAdapter(Context context, OrderedRealmCollection<Merchant> data) {
        super(context, data, true);
    }

    @Override
    protected int getLayoutForPosition(int position) {
        return R.layout.point_of_sale_list_item;
    }

}

在这个I适配器中,我只指定了布局xml: point_of_sale_list_item

行。一切正常。

但现在我需要从另一个模型中读取设定的电话号码。 在身份android:id=@+id/phoneTextView

中的xml中

所以我需要绑定第二个变量。 像这样:

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

    <data>    
       <variable
           name="item"
           type="com.myproject.android.customer.api.model.Merchant" />

       <variable
            name="point"
            type="com.myproject.android.customer.api.model.PointOfSale" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <ImageView
            android:id="@+id/imageViewPhoto"
            android:layout_width="90dp"
            android:layout_height="90dp" />

        <TextView
            android:id="@+id/phoneTextView"
            android:layout_width="0dp"
            android:text="@{point.phone}"
            android:layout_height="wrap_content" />

    </android.support.constraint.ConstraintLayout>

</layout>

这里有pojo代码:

public class PointOfSale {
    private int id;
    private String name;
    private String address;
    private String phone;
    private String email;

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }
}

但当然我收到了错误,因为第二个可用()没有绑定:

java.lang.IllegalStateException: failed to analyze: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor com.myproject.android.customer.api.model.PointOfSale.phone
file:\myProject\app\src\main\res\layout\point_of_sale_list_item.xml
loc:61:28 - 61:38
****\ data binding error ****

所以:

  1. 是否可以绑定第二个变量?
  2. 如何从第二个变量中获取数据?
  3. 或者可能创建连接这2个模型的新模型?

2 个答案:

答案 0 :(得分:2)

我确实做到了。就我而言:

<data>
    <variable name="viewModel" type="com.todtenkopf.myfrontpage.viewmodel.favoriteteam.FavoriteTeamVM"/>
    <variable name="matchup" type="com.todtenkopf.myfrontpage.viewmodel.favoriteteam.FavoriteTeamVM.Matchup"/>

您不会显示PointOfSale的代码。 phone是公共字段还是getPhone访问者?听起来更像是问题就在于这些问题。

答案 1 :(得分:0)

   <data>   
      <variable
                name="object name"
                type="your pojo classpath" />

      <variable
                name="handlers"
                type="your handler class path " /> 
    </data>

工作示例,下面共享链接 https://www.androidhive.info/android-working-with-databinding/