难以实现视图(Kotlin)
我尚未遵守单个活动模式,并且在应用程序中有多个活动。 我可以使用ExplablAbleStview找到所有示例都需要绑定。
我有:
1。)
buildFeatures {
viewBinding = true
}
2。) 我的XML中的布局标签
我还尝试使用我在某些教程中发现的Include标签,但
val binding = ProfileActivityBinding.inflate(layoutInflater)
并未解决绑定。 我是否在误解了如何使用它?
任何提示都会受到赞赏
我应该注意,在MainAttivity中。
I have not adhered to the Single Activity Schema and have multiple Activities in my application.
All examples I can find using expandableListView require bindings.
I have:
1.)
buildFeatures {
viewBinding = true
}
2.)
The layout tag within my xml
I have also tried using the include tag I found in some tutorial but
val binding = ProfileActivityBinding.inflate(layoutInflater)
is not resolving to a binding.
Am I misinterpreting how to use this?
Any tips are appreciated
I should note that within MainActivity.kt attempting to assign MainActivityBinding does not work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
启用视图绑定时,对于每个布局XML,它将生成以其命名的绑定类。因此,如果您具有
profile_activity.xml
,它将生成一个名为prileactivityBinding
的类。该类的一个实例将对您的布局中的每个view
具有一个具有ID的变量(类似地命名,但它会从Snake Case中进行一些转换-EGmy_button
将是称为mybutton
)您可以通过调用其
Aptrate
方法或bind
一个来获得此类的实例。充气
会将profile_activity
xml文件夸大成视图层次结构,bind
允许您传递到已经被夸大的一个(例如)活动
或fragment
'sview
)。您可以使用任何方便的方法 - 如果您无论如何都需要充气一个(例如,如果您创建了
view holder
recyClerview
),则可以使用ground> Aptrate
。否则,绑定
将采用现有的层次结构,并查找所需的所有视图。无论哪种方式,您最终都会为布局中的每个ID提供一个变量,每个人都指向该hiechyy中的实际view
,就好像它确实findviewbyid
。如果所有这些都是有道理的,并且仍然无法正常工作,请尝试构建您的应用程序(您只需在“构建菜单”中进行
重建
)。创建布局时,绑定类应自动生成,并且代码编辑器应立即识别它们,但有时需要踢。如果它正在播放,可能需要进行干净或无效的缓存 /重新启动When you have view binding enabled, for every layout XML you have, it'll generate a binding class named after it. So if you have
profile_activity.xml
, it'll generate a class calledProfileActivityBinding
. An instance of that class will have a variable for eachView
in your layout that has an ID (named similarly, but it does some conversion from snake case - e.g.my_button
will be calledmyButton
)You can get an instance of this class by either calling its
inflate
method, or thebind
one.inflate
will inflate theprofile_activity
XML file into a view hierarchy,bind
allows you to pass in one that's already been inflated (e.g. anActivity
orFragment
'sview
).You can use whichever one's convenient - if you need to inflate one anyway (say if you're creating a
ViewHolder
for aRecyclerView
) you may as well useinflate
. Otherwisebind
will take an existing hierarchy and look up all the views it needs to. Either way, you end up with a variable for each ID in the layout, and each one points to the actualView
in that hiearchy, as though it didfindViewById
for each one.If all that makes sense, and it's still not working, try building your app (you can just do
Rebuild
in the Build menu). The binding classes should be automatically generated when you create a layout, and the code editor should recognise them pretty much immediately, but sometimes it needs a kick. Might need to do a Clean or Invalidate Caches / Restart if it's playing up