You need to enable JavaScript to run this app. Data Binding搭配RecyclerView使用时列表条目渲染异常问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

Data Binding搭配RecyclerView使用时列表条目渲染异常问题咨询

问题根因

你在布局中给TextView配置了@{text}的绑定表达式,关联了布局定义的text变量,但在适配器bind方法中没有给dataBinding的text字段赋值,而是直接操作TextView的text属性。Data Binding会在绑定流程中用当前text变量的值刷新控件,初始时text变量为空,覆盖了你直接设置的文本,所以导致初始渲染空白。滚动过程中ViewHolder复用触发的绑定逻辑才会把之前的设置显示出来,就出现了你遇到的异常表现。

修复方案

仅需要修改适配器ViewHolder的bind方法,把直接修改TextView文本的逻辑改为给绑定变量赋值即可:

inner class SimpleAdapterViewHolder(
    private val dataBinding: SimpleAdapterViewHolderBinding
) : RecyclerView.ViewHolder(dataBinding.root) {
    fun bind(str: String) {
        Log.e("MYTAG", "setting TextView: $str")
        // 改为给布局声明的text变量赋值,触发Data Binding更新
        dataBinding.text = str
        dataBinding.executePendingBindings()
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 21:06:08