具有数据绑定和viewModel的Recyclerview非法状态异常

时间:2019-04-29 09:39:48

标签: android android-recyclerview android-databinding android-viewmodel

我正在从api获取类别列表,并将其设置为recyclerview。适配器代码编写在viewModel类中,并由调用api的片段调用。以下是设置适配器的方法。

fun getAdapter(
   listener: OtherVideoCategoriesRecyclerAdapter.OtherVideoCategoriesRecyclerAdapterListener,context: Context
): OtherVideoCategoriesRecyclerAdapter {
    if (categoriesRecyclerAdapter == null)
        categoriesRecyclerAdapter = OtherVideoCategoriesRecyclerAdapter(listener,context)

    return categoriesRecyclerAdapter as OtherVideoCategoriesRecyclerAdapter

}

fun setItems(categories: ArrayList<OtherCategoriesItem>) {
    categoriesList = categories
    categoriesRecyclerAdapter!!.setItems(categoriesList!!)

}

这就是我从片段类中调用这些方法的方式。

otherVideoViewModel.setItems(it.first.data!!.otherCategories as ArrayList<OtherCategoriesItem>)

设置适配器方法

private fun setAdapter() {
        otherVideosCategoriesBinding.recyclerView.layoutManager = LinearLayoutManager(activity)
        otherVideosCategoriesBinding.recyclerView.itemAnimator = DefaultItemAnimator()
        adapter = otherVideoViewModel.getAdapter(adapterListener,activity!!)
        otherVideosCategoriesBinding.recyclerView.adapter = adapter
    }

这是适配器类。

class OtherVideoCategoriesRecyclerAdapter(private val listener: OtherVideoCategoriesRecyclerAdapterListener,val context: Context): RecyclerView.Adapter<OtherVideoCategoriesRecyclerAdapter.ViewHolder>() {

    var categories = ArrayList<OtherCategoriesItem>()

    interface OtherVideoCategoriesRecyclerAdapterListener {
        fun onItemClicked(position: Int)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OtherVideoCategoriesRecyclerAdapter.ViewHolder {
        val inflater=LayoutInflater.from(context)
        val binding = ItemOtherVideoCategoryBinding.inflate(inflater, parent, false)

        return OtherVideoCategoriesRecyclerAdapter.ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return categories.size
    }

    override fun onBindViewHolder(holder: OtherVideoCategoriesRecyclerAdapter.ViewHolder, position: Int) {
        val item = categories[position]
        holder.bindViews(item)

    }

    class ViewHolder(private val binding: ItemOtherVideoCategoryBinding): RecyclerView.ViewHolder(binding.root) {
        fun bindViews(model: OtherCategoriesItem){
            binding.model=model
            binding.executePendingBindings()
        }

    }
    fun setItems(categoriesList: ArrayList<OtherCategoriesItem>) {
        categories = categoriesList
        notifyDataSetChanged()
    }
}

当我运行这段代码时,它崩溃并出现以下异常。

  

java.lang.IllegalStateException:创建视图时不得附加ViewHolder视图。确保您没有将'true'传递给LayoutInflater.inflate(...,boolean attachToRoot)的attachToRoot参数

我已经尝试了所有与该错误有关的答案,但是对于我的情况,它们都不起作用,因为其中许多答案都未包含数据绑定。

1 个答案:

答案 0 :(得分:0)

嘿,只是稍微改变一下onCreateViewHolder。试试这个:

 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OtherVideoCategoriesRecyclerAdapter.ViewHolder {
    val inflater=LayoutInflater.from(context)
    val binding:ItemOtherVideoCategoryBinding = DataBindingUtil.inflate(inflater,R.layout.your_layout_name, parent, false)

    return OtherVideoCategoriesRecyclerAdapter.ViewHolder(binding)
}