【问题标题】:How to add two viewModel class in xml with recycler view Adapter如何使用回收器视图适配器在 xml 中添加两个 viewModel 类
【发布时间】:2019-10-03 07:53:44
【问题描述】:

我需要在一个回收器视图中添加两个 ViewModel 类,但我还没有得到任何解决方案。我不知道您是否有任何解决方案而不是回答我并建议我尝试这样做:

public class PlannedListAdapter extends
RecyclerView.Adapter<PlannedListAdapter.ViewHolder> {
    private List<AddMedicineData> dataModelList;
    private Context context;
    public PlannedListAdapter(List<AddMedicineData> dataModelList, Context ctx) {
        this.dataModelList = dataModelList;
        context = ctx;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        PlannedRecyclerItemBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.planned_recycler_item, parent, false);
        return new ViewHolder(binding);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AddMedicineData dataModel = dataModelList.get(position);
       /* holder.itemRowBinding.timePill.setText();*/

        for (int j=0;j<dataModelList.get(position).getPilldates().size();j++){
            String timeList = dataModelList.get(position).getPilldates().get(j).getPilltime();
            Log.d("@@PillTimes", timeList);
            holder.itemRowBinding.timePill.setText(dataModel.getPilldates().get(j).getPilltime());
        }
        holder.bind(dataModel);
    }
    @Override
    public int getItemCount() {
        return dataModelList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public PlannedRecyclerItemBinding itemRowBinding;
        public ViewHolder(PlannedRecyclerItemBinding itemRowBinding) {
            super(itemRowBinding.getRoot());
            this.itemRowBinding = itemRowBinding;
        }
        public void bind(Object obj) {
            itemRowBinding.setVariable(BR.medicineModel, obj);
            itemRowBinding.executePendingBindings();
        }
    } }

在我的 XML 中,我添加了另一个像这样的视图模型:

<?xml version="1.0" encoding="utf-8"?>

<data>
    <variable
        name="medicineModel"
        type="com.kulsoft.care4cute.databinding.AddMedicineData" />

  <!--  <variable
        name="pillTakeModel"
        type="com.kulsoft.care4cute.models.MedicalModel.PilltakeModel" />-->
</data>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/lin_medicine"
        android:background="@color/white"
        android:layout_marginTop="@dimen/padding_10"
        android:layout_marginStart="@dimen/padding_10"
        android:layout_marginEnd="@dimen/padding_10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


            <RelativeLayout
                android:id="@+id/imgClick"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView

                    android:src="@drawable/clock"
                    android:layout_width="@dimen/dimen_20dp"
                    android:layout_height="@dimen/dimen_20dp">

                </ImageView>

            </RelativeLayout>

            <LinearLayout
                android:orientation="vertical"
                android:padding="@dimen/padding_10"
                android:layout_below="@+id/imgClick"
                android:layout_width="match_parent"
                android:layout_height="100dp">

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">


                    <com.kulsoft.care4cute.fonts.OpenSensRegular
                        android:layout_weight="1"
                        android:text="@{medicineModel.medName}"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                    </com.kulsoft.care4cute.fonts.OpenSensRegular>


                    <com.kulsoft.care4cute.fonts.OpenSensSemiBold
                        android:id="@+id/timePill"
                        android:gravity="end"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">


                    </com.kulsoft.care4cute.fonts.OpenSensSemiBold>

                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <com.kulsoft.care4cute.fonts.OpenSensThin
                    android:layout_weight="1"
                    android:text="@{`1 ` +medicineModel.medType}"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">


                </com.kulsoft.care4cute.fonts.OpenSensThin>

                    <com.kulsoft.care4cute.fonts.OpenSensThin
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text="@{medicineModel.medDose}"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                    </com.kulsoft.care4cute.fonts.OpenSensThin>

                    <com.kulsoft.care4cute.fonts.OpenSensThin
                        android:layout_weight="1"
                        android:gravity="start"
                        android:text="@{medicineModel.medUnit}"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                    </com.kulsoft.care4cute.fonts.OpenSensThin>

                    <com.kulsoft.care4cute.fonts.OpenSensThin
                        android:gravity="end"
                        android:layout_weight="1"
                        android:text="Medication Reminder"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">


                    </com.kulsoft.care4cute.fonts.OpenSensThin>

                </LinearLayout>


            </LinearLayout>



    </RelativeLayout>

    <View
        android:visibility="gone"
        android:layout_below="@+id/lin_medicine"
        android:layout_width="match_parent"
        android:background="@color/black"
        android:layout_marginTop="@dimen/padding_10"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:layout_height="0.1dp"/>
</RelativeLayout>

在 XML 中,我想根据循环设置我在适配器中尝试过的模型类列表。

在此先感谢您,感谢您的每一个回答和评论

【问题讨论】:

    标签: android data-binding android-recyclerview


    【解决方案1】:

    您是否已经遇到任何错误?

    可以在绑定中设置多个变量,当您从活动/片段或适配器执行此操作时没有真正的区别。

    一般来说,像设置第一个变量一样设置第二个变量 - 应该可以。但是,我不明白您为什么要在那里使用循环来在视图中设置文本。

    另外,不要忘记在您的 XML 中仅使用新变量构建项目以生成 BR 属性

    public void onBindViewHolder(ViewHolder holder, int position) {
           final AddMedicineData dataModel = dataModelList.get(holder.getAdapterPosition());
    holder.itemRowBinding.timePill.setText(dataModel.getPilldates().get(0).getPilltime());
                holder.bind(dataModel);
            }
    

    【讨论】:

    • 我没有收到错误,但循环内的值只重复最后一个
    • 绑定没有错误我只想从模型类内部获取List数据,List也有模型类
    • 在您的循环中,您从 dataModelList 项中获取所有值,并设置为相同的 textView,因此 textView 仅显示最后一个值。
    • 我并没有真正得到预期的结果
    • 是的,我收到了很多条目,只设置了最后一个
    猜你喜欢
    • 2019-05-03
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多