You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为RecyclerView Adapter迁移View Binding遇阻,求解决方案

在RecyclerView Adapter中适配View Binding的解决方案

步骤1:确保View Binding全局开启

首先确认你的模块级build.gradle(通常是app/build.gradle)中已经开启View Binding:

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

步骤2:修改ViewHolder类

将ViewHolder改为持有布局对应的Binding实例(布局just_played_row.xml会自动生成JustPlayedRowBinding类):

class JustPlayedViewHolder(val binding: JustPlayedRowBinding) : RecyclerView.ViewHolder(binding.root)

步骤3:修改Adapter的onCreateViewHolder方法

使用Binding类的inflate方法替代传统的LayoutInflater.inflate,直接创建Binding实例并传入ViewHolder:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JustPlayedViewHolder {
    val binding = JustPlayedRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return JustPlayedViewHolder(binding)
}

步骤4:修改onBindViewHolder方法

通过ViewHolder持有的Binding实例直接访问控件,无需再通过itemView,同时移除冗余的context变量(可通过binding.root.context获取上下文):

override fun onBindViewHolder(holder: JustPlayedViewHolder, position: Int) {
    val item = lastPlayed[position]
    val albumArt = lastPlayedAlbumart[position]

    with(holder.binding) {
        tijd.text = item[0]
        artiest.text = item[1]
        titel.text = item[2]

        tijd.contentDescription = item[0]
        artiest.contentDescription = item[1]
        titel.contentDescription = item[2]

        albumart.setImageBitmap(albumArt)

        val appleMusicUrl = item[3]
        if (appleMusicUrl.isNotEmpty()) {
            root.isClickable = true
            appleMusicIcon.isVisible = true

            root.setOnClickListener {
                println("stationAPP: appleMusicURL = $appleMusicUrl")
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(appleMusicUrl))
                root.context.startActivity(intent)
            }
        } else {
            root.isClickable = false
            appleMusicIcon.isVisible = false
        }
    }
}

完整修改后的Adapter代码

class JustPlayedAdapter: RecyclerView.Adapter<JustPlayedViewHolder>() {

    override fun getItemCount(): Int {
        return lastPlayed.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JustPlayedViewHolder {
        val binding = JustPlayedRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return JustPlayedViewHolder(binding)
    }

    override fun onBindViewHolder(holder: JustPlayedViewHolder, position: Int) {
        val item = lastPlayed[position]
        val albumArt = lastPlayedAlbumart[position]

        with(holder.binding) {
            tijd.text = item[0]
            artiest.text = item[1]
            titel.text = item[2]

            tijd.contentDescription = item[0]
            artiest.contentDescription = item[1]
            titel.contentDescription = item[2]

            albumart.setImageBitmap(albumArt)

            val appleMusicUrl = item[3]
            if (appleMusicUrl.isNotEmpty()) {
                root.isClickable = true
                appleMusicIcon.isVisible = true

                root.setOnClickListener {
                    println("stationAPP: appleMusicURL = $appleMusicUrl")
                    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(appleMusicUrl))
                    root.context.startActivity(intent)
                }
            } else {
                root.isClickable = false
                appleMusicIcon.isVisible = false
            }
        }
    }
}

class JustPlayedViewHolder(val binding: JustPlayedRowBinding) : RecyclerView.ViewHolder(binding.root)

关键注意点

  • 生成的Binding类名称是布局文件名的驼峰命名(比如just_played_row.xml → JustPlayedRowBinding)
  • 若布局使用了<merge>标签,需调整inflate方式:JustPlayedRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)改为JustPlayedRowBinding.inflate(LayoutInflater.from(parent.context)),并在onCreateViewHolder中手动将binding.root添加到parent
  • 移除原代码中的context变量,避免空指针风险,直接通过binding.root.context获取上下文

内容的提问来源于stack exchange,提问作者Patrick Koning

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 07:43:13