在使用ViewBinding而不是Kotlin合成材料时,如何设置视图ID?
我正在更新一个 Android 项目以使用 ViewBinding
而不是 Kotlin Synthetics
。我很难弄清楚如何更改以下代码,以便我可以从布局 ID 访问视图。
binding.myLinearLayout.children
.filter { it.checkboxInput is CheckBox }
在这种情况下,children
都是通用的 View
类型,无法像以前使用 Kotlin Synthetics 那样访问
。checkboxInput
ID
我收到错误未解析的参考:checkboxInput
解决此问题的方法是什么?有没有办法检查视图是否是绑定类型?我需要创建自定义视图类来执行此操作吗?还有其他想法吗?
感谢您的帮助!
编辑
我还有另一个有点令人困惑的案例。
binding.formItems.children
.filter { it.getTag(R.id.tag_guest_identifier) != null }
.map { view ->
Guest(
guestIdentifier = view.getTag(R.id.tag_guest_identifier).toString(),
name = view.playerName.valueText.toString(),
...
)
}
}
在这里,我得到了通用View
的列表,因此我无法访问它们的属性(即view.playerName...等)。
我是否需要创建一个 View 子类,然后将视图转换为该类型?有没有更简单的方法来实现这一目标?
再次感谢!
I am updating an Android project to use ViewBinding
instead of Kotlin Synthetics
. I'm having difficulty figuring out how to change the following code so I can access the views from their layout IDs.
binding.myLinearLayout.children
.filter { it.checkboxInput is CheckBox }
In this case children
are all generic View
types and can't access the checkboxInput
IDs like it used to be possible using Kotlin Synthetics
.
I get the error Unresolved reference: checkboxInput
What would be the way to solve this? Is there a way to check if the View is of a binding type? Do I need to make custom View classes to do this? Any other ideas?
Thanks for your help!
EDIT
I have another case that's a bit confusing.
binding.formItems.children
.filter { it.getTag(R.id.tag_guest_identifier) != null }
.map { view ->
Guest(
guestIdentifier = view.getTag(R.id.tag_guest_identifier).toString(),
name = view.playerName.valueText.toString(),
...
)
}
}
Here, I get a list of generic View
s so I can't access their properties (ie. view.playerName... etc.).
Do I need to create a View subclass and then cast the view to that type? Is there an easier way to achieve this?
Thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看绑定基本上与合成材料相同的方法,除非您拥有具有
condbox_input
的ID的东西,而不是在活动
。 /code>或erther,它会在activechackboxinput
contive> activityactive
viewBinding
对象中创建它。因此,如果您以前这样访问它:现在,您可以在
viewBinding
对象上访问它:您不需要进行任何搜索,这会破坏观点绑定!它是自动绑定的视图,可与方便对象中的变量进行自动绑定。
您的代码将与
过滤器一起使用{是复选框}
,然后您将在布局的那部分中获取所有复选框
项目(您也可以使用> filterisinstance< checkbox>
,同一件事)。但是,如果您想要具有特定ID的一个,则必须查看其ID属性 - 到那时,也可能只需使用findViewById
!View binding works basically the same way synthetics did, except when you have something with an ID of
checkbox_input
, instead of magically creating a variable calledcheckboxInput
on theActivity
or whatever, it creates it in theViewBinding
object instead. So if you were accessing it like this before:now you access it on the
ViewBinding
object instead:You don't need to do any searching, that defeats the point of view binding! It's automagically binding views to variables in a convenient object.
Your code would work with
filter { it is CheckBox }
, and then you'd get all theCheckbox
items within that part of the layout (you can also usefilterIsInstance<CheckBox>
, same thing). But if you wanted the one with a specific ID, you'd have to look at its ID attribute - and at that point, might as well just usefindViewById
!使用ViewBinding,您可以访问视图键入仅
绑定。
查看viewID是XML中每个视图的ID,使用attribuite
android:ID
访问复选框。
在您的情况下,您可以使用binding.checkboxinput
With viewBinding you can access views typing just
binding.viewId
Where viewId is the id defined for each view in your xml with the attribuite
android:id
In your case you can access the checkbox using
binding.checkboxInput