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

问题描述 投票:3回答:2

这里是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

好。一切正常。

但现在我需要从另一个模型中读取设置的电话号码。在id 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. 我如何从第二个变量获取数据?

或者可能创建连接这2个模型的新模型?

android android-databinding
2个回答
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访问者?听起来更像是问题就在于这些问题。


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/下面共享

© www.soinside.com 2019 - 2024. All rights reserved.