简体   繁体   English

从 Kotlin 合成迁移 ConstraintLayout 视图绑定

[英]ConstraintLayout view binding migration from Kotlin synthetics

I have an existing view that extends from ConstraintLayout which looks something like this:我有一个从ConstraintLayout扩展的现有视图,它看起来像这样:

class LandingTemplate: ConstraintLayout {

  init {
    inflate(context, R.layout.landing_template, this)
    
    // Currently this 'recyclerView' is a kotlin synthetic
    recyclerView.run {
      // this sets up the recycler view
    }
}

I'm familiar with view binding with activities and fragments, but I can't find any documentation around the extends layout case.我熟悉带有活动和片段的视图绑定,但我找不到关于扩展布局案例的任何文档。 My question is, what do I replace that initial inflate call with here?我的问题是,我用什么来代替最初的inflate调用?

I'm assuming you have a context available from your constructor and your XML layout's top level tag is <merge> .我假设您的构造函数提供了一个上下文,并且您的 XML 布局的顶级标记是<merge> You can use your binding class's inflate to create and add the child layout.您可以使用绑定类的inflate来创建和添加子布局。

And since this can all be set up in the constructor, you don't need lateinit var like in the Activity/Fragment examples, and can just use val instead.由于这一切都可以在构造函数中进行设置,因此您不需要像 Activity/Fragment 示例中那样的lateinit var ,而可以使用val代替。

class LandingTemplate(context: Context, attrs: AttributeSet): ConstraintLayout(context, attrs) {

    private val binding = LandingTemplateBinding.inflate(LayoutInflater.from(context), this)

    init {
        binding.recyclerView.run {
            // this sets up the recycler view
        }
    }
}

you can get layout inflater like below你可以得到像下面这样的布局充气器

 val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

 val view = inflater.inflate(R.layout.landing_temple,this,true)

and you must have valid view construct too并且您也必须具有有效的视图构造

 LandingTemple(Context) // for creating view programmatically

 LandingTemple(Context,AttrributeSet) // to inflate view from xml , and 
 //the constructor context is one that you use to call `getSystemService

for more information check欲了解更多信息, 请检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Kotlin 合成迁移到 Jetpack 视图绑定后出现 TextView 问题 - TextView issues after migrate from Kotlin synthetics to Jetpack view binding 如何将 Kotlin 合成转换为循环中查看膨胀 XML 的绑定? - How to convert Kotlin synthetics to view binding for inflated XML in a loop? 用视图绑定替换 kotlin 合成后应用程序强制关闭 - App is force closing after replacing kotlin synthetics with view binding Kotlin / 迁移到视图绑定 - Kotlin / Migration to View Binding 如何让视图绑定与 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? Android:如何在基础 class 中使用视图绑定并从 kotlin 合成 - Android: How to use viewbinding in base class and migrate from kotlin synthetics 使用 ConstraintLayout Kotlin 时视图超出屏幕 - View goes out of screen with ConstraintLayout Kotlin 在使用 ViewBinding 而不是 Kotlin Synthetics 时访问 View 的子项时如何设置 View id? - How do I set View ids when accessing children of View when using ViewBinding instead of Kotlin Synthetics? 来自 Kotlin 视图的 EditText 绑定不起作用 - EditText Binding from View in Kotlin not working Kotlin中的Android View模型绑定 - Android View Model binding in kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM