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