[英]How to convert Kotlin synthetics to view binding for inflated XML in a loop?
While trying to convert an application from the defunct Kotlin synthetics to the newer/supported view binding method, I ran into this issue where layouts are being inflated in a loop and attached to the target parent view:在尝试将应用程序从已失效的 Kotlin 合成转换为更新/支持的视图绑定方法时,我遇到了这个问题,其中布局在循环中膨胀并附加到目标父视图:
for (item in itemList) {
val view = LayoutInflater.from(this).inflate(R.layout.imageitem_row, binding.linear, false)
view.text1.text = item.title
view.text2.text = item.pubdate
binding.linear.addView(view)
}
Since Kotlin synthetics are being removed from the code, text1
and text2
are not valid properties of view
.由于 Kotlin 合成正在从代码中删除,因此text1
和text2
不是view
有效属性。 So how do I apply view binding to this inflated layout?那么如何将视图绑定应用到这个膨胀的布局呢? Or does that not work here and I should be using findViewById()
instead for text1
and text2
?或者这在这里不起作用,我应该使用findViewById()
代替text1
和text2
?
Just to explain what's going on - with the synthetics library, it was basically doing view binding in the activity itself (or whatever).只是为了解释发生了什么 - 使用合成库,它基本上是在活动本身(或其他)中进行视图绑定。 For each view with an id in your layout, it was adding a property you could access directly.对于布局中带有 id 的每个视图,它添加了一个您可以直接访问的属性。 So you could just call text1.text
in the current scope, and that would have been automatically bound to a view in the hierarchy.所以你可以在当前范围内调用text1.text
,它会自动绑定到层次结构中的视图。
With view binding, there's no magic and no throwing properties into that top-level scope.使用视图绑定,没有魔法,也没有将属性扔到顶级范围内。 Instead, each layout has an automatically generated class with Binding
added to the end of the name, like ImageItemRowBinding
.相反,每个布局都有一个自动生成的类,并在名称的末尾添加了Binding
,例如ImageItemRowBinding
。 This class has a property for each view with an id, so it's all encapsulated in one place.这个类有一个属性,每个视图都有一个id,所以它都封装在一个地方。
You either call bind(view)
on it to get an instance with all the properties assigned (by finding them in the view
hierarchy you pass in), or you can call inflate
if you don't have the view yet, and you want it to inflate that while it's binding.您可以在其上调用bind(view)
以获取分配了所有属性的实例(通过在您传入的view
层次结构中找到它们),或者如果您还没有视图并且想要它,则可以调用inflate
在它具有约束力的同时膨胀它。 You end up getting back an instance of the binding class with all the view properties assigned (and you can access the top of the view hierarchy through the root
property).您最终会得到一个绑定类的实例,并分配了所有视图属性(并且您可以通过root
属性访问视图层次结构的顶部)。
Aside from that, it's basically the same!除此之外,基本上是一样的! You just have to manually create your binding instances, and access your views through that.您只需要手动创建绑定实例,并通过它访问您的视图。 Don't forget you can use Kotlin functions like with(binding)
or binding.run { }
to work with its properties without needing the binding
prefix every time, so you can pretty much just wrap your original synthetics-based code if it's convenient不要忘记,您可以使用像with(binding)
或binding.run { }
类的 Kotlin 函数来处理其属性,而无需每次都使用binding
前缀,因此如果方便,您几乎可以将原始的基于合成的代码包装起来
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.