使用数据绑定填充 RecyclerView 内的 MultiView ViewHolder

Posted

技术标签:

【中文标题】使用数据绑定填充 RecyclerView 内的 MultiView ViewHolder【英文标题】:Filling in a MultiView ViewHolder inside a RecyclerView With Data Binding 【发布时间】:2021-11-23 22:59:47 【问题描述】:

所以我能够接近创建 MultiView ViewHolder,但我仍然对一些细节感到困惑。首先,我将如何填写 RecyclerView,因为我有多个数据类(在这种情况下,手动)。其次,适配器如何知道何时显示特定视图?我把代码放在这里

数据类

sealed class InfoRecyclerViewItems

    class WithPicture (
        val id: Int,
        val movieName: String,
        val thoughts: String
            ): InfoRecyclerViewItems()

    class WithoutPicture(
        val id: Int,
        val movieName: String,
        val thoughts: String
    ): InfoRecyclerViewItems()


适配器

class RecyclerViewAdapter(infoItems: MutableList<InfoRecyclerViewItems>): RecyclerView.Adapter<MainViewHolder>() 
    private var infoItems1: MutableList<InfoRecyclerViewItems>

    init 
        this.infoItems1 = infoItems
    


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder 
        return when(viewType)
           R.layout.container_one -> MainViewHolder.WithPictureViewHolder(
               ContainerOneBinding.inflate(
                   LayoutInflater.from(parent.context), parent, false)
           )

           R.layout.container_two -> MainViewHolder.WithoutPictureViewHolder(
               ContainerTwoBinding.inflate(
                   LayoutInflater.from(parent.context), parent, false)
           )
            else -> throw IllegalArgumentException("Invalid view given")
        
    

    override fun onBindViewHolder(holder: MainViewHolder, position: Int) 
        when(holder)
            is MainViewHolder.WithPictureViewHolder -> holder.bind(infoItems1[position] as InfoRecyclerViewItems.WithPicture)
            is MainViewHolder.WithoutPictureViewHolder -> holder.bind(infoItems1[position] as InfoRecyclerViewItems.WithoutPicture)
        
    

    override fun getItemCount() = infoItems1.size

    override fun getItemViewType(position: Int): Int 
        return when(infoItems1[position])
            is InfoRecyclerViewItems.WithPicture -> R.layout.container_one
            is InfoRecyclerViewItems.WithoutPicture -> R.layout.container_two
        
    


ViewHolder

sealed class MainViewHolder(binding: ViewBinding) : RecyclerView.ViewHolder(binding.root) 

    class WithPictureViewHolder(private val binding: ContainerOneBinding) : MainViewHolder(binding)
        fun bind(items: InfoRecyclerViewItems.WithPicture)
            binding.part1 = items
            binding.executePendingBindings()
        
    

    class WithoutPictureViewHolder(private val binding: ContainerTwoBinding) : MainViewHolder(binding)
        fun bind(items: InfoRecyclerViewItems.WithoutPicture)
            binding.part2 = items
            binding.executePendingBindings()
        
    


主要活动

class MainActivity : AppCompatActivity() 

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)



        binding.recyclerView.apply 
            setHasFixedSize(true)
            layoutManager = LinearLayoutManager(this@MainActivity)
        
    

欢迎任何建议,谢谢。

【问题讨论】:

视图绑定还是数据绑定?在我看来,尝试将数据绑定与 RecyclerView 一起使用只是自虐。 【参考方案1】:

所以我想通了。基于这个设置,我可以创建一个空的 MutableList,然后调用我想要填充的每个不同的数据类。这些数据类链接到与之关联的 ViewHolder,从而在 RecylerView 中创建两个不同的视图。

class MainActivity : AppCompatActivity() 

    private lateinit var binding: ActivityMainBinding
    private lateinit var manager: LinearLayoutManager

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val information: MutableList<InfoRecyclerViewItems> = ArrayList()
        information.add(InfoRecyclerViewItems.WithPicture(2, "The Fallen", "ok"))
        information.add(InfoRecyclerViewItems.WithoutPicture(4, "Black Panther", "10/10, Fantastic Movie"))

        manager = LinearLayoutManager(this)

       binding.recyclerView.apply 
           adapter = RecyclerViewAdapter(information)
           layoutManager = manager
       

    

然后您可以继续添加到您选择添加到的任何视图中

附:其他文件(ViewHolder、Adapter 和 Data Class)保持不变。

【讨论】:

以上是关于使用数据绑定填充 RecyclerView 内的 MultiView ViewHolder的主要内容,如果未能解决你的问题,请参考以下文章

android: ScrollView 内的 RecyclerView

RecyclerView的缓存机制

使用从 SQLite 概括的数据填充 RecyclerView/ListView

使用 Firebase 实时数据库参考填充 RecyclerView

如何使用大型 json 数据填充 RecyclerView [重复]

为啥我的 RecyclerView 没有填充来自改造的数据?