简体   繁体   English

如何将 Kotlin 合成转换为循环中查看膨胀 XML 的绑定?

[英]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 合成正在从代码中删除,因此text1text2不是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()代替text1text2

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.

相关问题 如何让视图绑定与 RecyclerView.ViewHolder 中的 XML 布局高度一起使用,就像他们对 Kotlin Synthetics 所做的那样? - How to get view binding to work with XML layout heights in RecyclerView.ViewHolder like they did with Kotlin Synthetics? 从 Kotlin 合成迁移 ConstraintLayout 视图绑定 - ConstraintLayout view binding migration from Kotlin synthetics 用视图绑定替换 kotlin 合成后应用程序强制关闭 - App is force closing after replacing kotlin synthetics with view binding 从 Kotlin 合成迁移到 Jetpack 视图绑定后出现 TextView 问题 - TextView issues after migrate from Kotlin synthetics to Jetpack view binding 如何将膨胀的视图转换为位图? - How to convert a inflated View to Bitmap? 在使用 ViewBinding 而不是 Kotlin Synthetics 时访问 View 的子项时如何设置 View id? - How do I set View ids when accessing children of View when using ViewBinding instead of Kotlin Synthetics? 如何知道Android中的资源文件(xml)是否放大了视图? - How to know if a view was inflated from an resource file (xml) in Android? 如何遍历在android服务类中放大的视图上的所有按钮? - how to loop through all buttons on a view inflated in an android service class? Android:如何在基础 class 中使用视图绑定并从 kotlin 合成 - Android: How to use viewbinding in base class and migrate from kotlin synthetics 如何使用视图绑定在 Kotlin 中正确扩展视图 - How to inflate a view properly in Kotlin with View Binding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM