Как добавить два класса viewModel в xml с помощью Adapter View Adapter - PullRequest
0 голосов
/ 03 октября 2019

Мне нужно добавить два класса 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 я хочу установить класс List, который я пробовал в адаптере, в соответствии с циклом.

СпасибоВы заранее, и я ценю каждый ваш ответ и комментарий

1 Ответ

1 голос
/ 03 октября 2019

Вы уже получаете какие-либо ошибки?

Установка нескольких переменных в связывании возможна, нет реальной разницы, когда вы делаете это из действия / фрагмента или адаптера.

Как правило, установка 2-й переменной, как вы сделали с первой, должна работать. Однако я не понимаю, почему вы используете там цикл для установки текста в вашем представлении.

Кроме того, не забудьте создать проект с новой переменной в вашем 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);
        }
...