如何解决Anroid RecyclerView 中的数据绑定不起作用 (Kotlin)
这是我的 OnBindViewHolder 函数代码:
override fun onBindViewHolder(holder: ViewHolder,position: Int) {
val country = countries[position]
var countryInfoCardBinding = DataBindingUtil.setContentView<CountryInfoCardBinding>(context as Activity,R.layout.country_info_card)
countryInfoCardBinding.country = country
}
这是我的 XML 文件:
解决方法
您应该在 onCreateViewHolder
而不是 onBindViewHolder
中扩充数据绑定对象。现在你正在膨胀一个与你的视图没有联系的对象,这就是为什么(我假设)什么都没有出现
override fun onBindViewHolder(holder: ViewHolder,position: Int) {
val country = countries[position]
holder.binding.country = country
}
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): ViewHolder {
val binding = CountryInfoCardBinding.inflate(
LayoutInflater.from(parent.context),parent,false
)
return ViewHolder(binding)
}
inner class ViewHolder(val binding: CountryInfoCardBinding) : RecyclerView.Viewholder(binding.root)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。