我有一个绑定(bind)适配器,它根据从数据库返回的数据以编程方式添加 View 。
@BindingAdapter("addViews")
fun addViews(view: LinearLayout, fruit: List<Fruits>){
fruit.forEach{
val v = View.inflate(view.context, R.layout.item_fruit_layout, null)
v.text_view_fruit_name.text = it.name
view.addView(v)
}
}
我试图不对 TextView 使用 setText,因为我使用的是数据绑定(bind),但我不知道如何使用 DataBindingUtil.setContentView
或其他任何方法来绑定(bind)数据。谁能告诉我我是否可以做到这一点?
谢谢
最佳答案
这是我的解决方案:
val v = View.inflate(view.context, R.layout.item_fruit_layout, null)
val binding = ItemFruitLayoutBinding.bind(v)
binding.setVariable(BR.fruit, it)
关于android - 在 kotlin Android 中以编程方式生成的 View 的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58753092/