简体   繁体   English

如何将多个回收视图设置为单个适配器

[英]How to set multiple Recycle View to Single Adapter

How to set multiple recycleview to single adapter?如何将多个recycleview设置为单个适配器?

I am creating an app, there are lot of categories in app and creating multiple adapter, model and recycle view makes an app complicated.我正在创建一个应用程序,应用程序中有很多类别并创建多个适配器,model 和回收视图使应用程序变得复杂。 It hard to make different adapter for multiple recycleview很难为多个recycleview制作不同的适配器

Can I set multiple recycleview to one single adapter.我可以将多个recycleview 设置为一个适配器吗? By using if or else or switch cases how to do this?通过使用 if 或 else 或 switch 案例如何做到这一点?

Or is there any alternative??或者有什么替代方法??

I think this might help you.我想这可能会对你有所帮助。

data class CategoryCheckList(var type: Int, val list: Any)

fun initData() {
    val category1 = Category1()
    val list1 = arrayOfList<CategoryCheckList>()
    list1.add(CategoryCheckList(CATETORY_1), category1)

    recyclerView.adapter = SingleListAdapter(context, list)

    val category2 = Category2()
    val list2 = arrayOfList<CategoryCheckList>()
    list2.add(CategoryCheckList(CATETORY_2), category2)

    recyelerView2.adapter = SingleListAdapter(context, list2)
}

class SingleListAdapter(
    private val context: Context,
    private val list: ArrayList<CategoryCheckList>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            CATETORY_1 -> {
                val binding = ItemCategory1Binding.inflate(
                    LayoutInflater.from(parent.context), parent, false)
                Category1ViewHolder(binding)
            }
            else -> {
                val binding = ItemCategory2Binding.inflate(
                    LayoutInflater.from(parent.context), parent, false)
                Category2ViewHolder(binding)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is Category1ViewHolder -> {
                list[position].let { holder.bind(it, position) }
            }
            is Category2ViewHolder -> {
                list[position].let { holder.bind(it, position) }
            }
            else -> {
                // default
                list[position].let { (holder as Category1ViewHolder).bind(it, position) }
            }
        }
    }

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

    override fun getItemViewType(position: Int): Int {
        return list[position].type
    }
}

class Category1ViewHolder(var binding: ItemCategory1Binding) :
    RecyclerView.ViewHolder(binding.root) {

    fun bind(categoryCheckList: CategoryCheckList, position: Int) {
        val nameHeader = categoryCheckList.list as Category1

        // do something
    }
}

class Category2ViewHolder(var binding: ItemCategory2Binding) :
    RecyclerView.ViewHolder(binding.root) {

    fun bind(categoryCheckList: CategoryCheckList, position: Int) {
        val checklist = categoryCheckList.list as Category2
        // do something
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM