难以实现视图(Kotlin)

发布于 2025-01-29 10:31:52 字数 413 浏览 3 评论 0原文

我尚未遵守单个活动模式,并且在应用程序中有多个活动。 我可以使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是我的挚爱i 2025-02-05 10:31:52

启用视图绑定时,对于每个布局XML,它将生成以其命名的绑定类。因此,如果您具有profile_activity.xml,它将生成一个名为prileactivityBinding的类。该类的一个实例将对您的布局中的每个view具有一个具有ID的变量(类似地命名,但它会从Snake Case中进行一些转换-EG my_button将是称为mybutton

您可以通过调用其Aptrate方法或bind一个来获得此类的实例。 充气会将profile_activity xml文件夸大成视图层次结构,bind允许您传递到已经被夸大的一个(例如)活动fragment's view)。

您可以使用任何方便的方法 - 如果您无论如何都需要充气一个(例如,如果您创建了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 called ProfileActivityBinding. An instance of that class will have a variable for each View in your layout that has an ID (named similarly, but it does some conversion from snake case - e.g. my_button will be called myButton)

You can get an instance of this class by either calling its inflate method, or the bind one. inflate will inflate the profile_activity XML file into a view hierarchy, bind allows you to pass in one that's already been inflated (e.g. an Activity or Fragment's view).

You can use whichever one's convenient - if you need to inflate one anyway (say if you're creating a ViewHolder for a RecyclerView) you may as well use inflate. Otherwise bind 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 actual View in that hiearchy, as though it did findViewById 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文