gpt4 book ai didi

android - binding.root 表示什么?

转载 作者:行者123 更新时间:2023-12-05 00:18:24 28 4
gpt4 key购买 nike

以下是我的代码 fragment 。

class DashBoardHolder(val binding: ActivityDashBoardBinding) : RecyclerView.ViewHolder(binding.root) {
internal var tvName: TextView = itemView.findViewById(R.id.textViewGrandrukName)

最佳答案

  • binding.root 是对 Root View 的引用。
  • Root View 是最外层 View
    布局中的容器。
  • 当您调用 binding.root 时,将返回
    LinearLayout Root View 。(下面的 xml 代码)
  • 在 View 绑定(bind)之前,当我们调用 findViewById() 时,我们可以从任何布局调用任何 id,即使给定布局除外。它不会显示编译时错误,就像你从其他布局调用 id 一样,该 View 不在您提供的布局中.当我们运行时,我们将获得“Null”对象引用。但是 View 绑定(bind)将从 Root View 提供所有绑定(bind)ID。我们只能调用biding.name和biding.button。我们不能调用其他布局绑定(bind)id。所以当我们使用时查看投标,我们不会得到“NULL”。这是 View 绑定(bind)的特定功能。都是因为 Root View 。
     <LinearLayout ... >
    <TextView android:id="@+id/name" />
    <ImageView android:cropToPadding="true" />
    <Button android:id="@+id/button"
    android:background="@drawable/rounded_button" />
    </LinearLayout>
  • 关于android - binding.root 表示什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68173707/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com