以编程方式将viewModel添加到视图

时间:2019-06-16 13:14:07

标签: android android-recyclerview viewmodel android-databinding

我有一个RecycleView和一个StaggeredGridLayoutManager。我在RecycleViewAdapter中以编程方式添加了视图。问题在于视图的布局使用数据绑定:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">

  <data>

    <variable
      name="viewModel"
      type="com.myapp.myViewModel" />
  </data>

  <androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

将视图模型添加到recycleView时,如何将viewModel设置为视图?

这是我的RecycleViewAdapter:

 class RecycleViewAdapter(context: Context, private val layouts: List<Int>) :
        RecyclerView.Adapter<RecycleViewAdapter.ViewHolder>() {
      override fun getItemCount(): Int {
        return layouts.size
      }

      private val mInflater: LayoutInflater = LayoutInflater.from(context)
      private var mClickListener: ItemClickListener? = null

      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = mInflater.inflate(R.layout.recycleview_row, parent, false)

        return ViewHolder(view)
      }

      override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val layout = layouts[position]
        val view = mInflater.inflate(layout, null)
        holder.layout.addView(view)
      }


      inner class ViewHolder internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView),
          View.OnClickListener {
        internal val layout: LinearLayout = itemView.findViewById(R.id.layout)

        init {
          itemView.setOnClickListener(this)
        }

        override fun onClick(view: View) {
          if (mClickListener != null) mClickListener!!.onItemClick(view, adapterPosition)
        }
      }

      internal fun getItem(id: Int): Int {
        return layouts[id]
      }

      internal fun setClickListener(itemClickListener: ItemClickListener) {
        this.mClickListener = itemClickListener
      }

      interface ItemClickListener {
        fun onItemClick(view: View, position: Int)
      }
    }

我的recycleview_row是:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

  <data>

    <variable
      name="viewModel"
      type="com.myApp.BaseViewModel" />

  </data>

  <LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

  </LinearLayout>
</layout>

我将布局以编程方式包含在ID为LinearLayout的{​​{1}}中,并且每个viewModel都扩展了BaseViewModel。

1 个答案:

答案 0 :(得分:0)

这是我们在动态视图中设置ViewModel的方式。

  LayoutInflater lInflater = LayoutInflater.from(mContext);
      ItemListBinding itemBinding = ItemListBinding .inflate(lInflater, parent, false);
  itemBinding .setviewModel(viewModel);
  itemBinding.executePendingBindings();

查看此博客here