【问题标题】:How to use View Binding with Included Views?如何使用包含视图的视图绑定?
【发布时间】:2020-02-26 00:16:05
【问题描述】:

视图绑定随 v3.6 发布。

文档: https://developer.android.com/topic/libraries/view-binding

我的问题是,有人知道如何将视图绑定与包含的布局一起使用吗?

包含另一个布局的给定布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/my_header"
        layout="@layout/item_header"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</LinearLayout>

我正在尝试引用 item_header 布局内的项目。

binder.my_header (<-- this just returns back the view)
binder.root (<-- this just returns back the root view)

即使我将 id 添加到 item_header 的根目录,例如 id="@+id/parent_id" 并尝试引用它,我也会收到空指针异常

binder.parentId (<-- I have access to views inside of the item_header, however, I receive exceptions. Says that "parentId" cannot be found)

如何引用布局,item_header

【问题讨论】:

  • 在我刚刚运行的实验中,binder.my_headerItemHeaderBinding,而不是View。我可以毫无问题地引用其中的小部件(例如,binder.my_header.foo)。
  • @CommonsWare 你这个人!我没有意识到我可以参考。这现在就像一个魅力。您介意发表您的评论作为答案吗?我会接受的。
  • 这是一个重复的问题。在这里查看我的答案stackoverflow.com/a/60616894/8040697

标签: android binding


【解决方案1】:

假设您问题中的布局是activity_main.xml。为它生成的视图绑定类是ActivityMainBinding。同样,对于item_header.xml,生成的视图绑定为ItemHeaderBinding

如果我们假设 item_header.xml 有一个名为 @+id/fooTextView,那么你最终会得到这部分 Kotlin:

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val mainBinding = ActivityMainBinding.inflate(layoutInflater)

    setContentView(mainBinding.root)

    mainBinding.myHeader.foo.text = "this is a test"
  }
}

因此,ActivityMainBinding 对象应该有一个带有 android:id 的属性,在这种情况下,您提供给 &lt;include&gt;myHeader。那应该是ItemHeaderBinding,因为视图绑定似乎为&lt;include&gt; 设置了嵌套绑定对象。由于myHeaderItemHeaderBinding,因此您可以引用它上面的小部件,就像您自己直接膨胀ItemHeaderBinding 一样。

请注意,视图绑定似乎将lower_snake_case 转换为lowerCamelCase,因此就生成的代码而言,my_header ID 变为myHeader

【讨论】:

    【解决方案2】:

    我已经在 J​​ava 中检查过这个问题。

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <include
            android:id="@+id/my_header"
            layout="@layout/item_header"
            android:layout_width="match_parent"
            android:layout_height="100dp" />
    
    </LinearLayout>
    

    item_header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="false"
        android:background="@android:color/transparent">
        <TextView
            android:id="@+id/foo"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>
    

    MainActivity.java

    package com.example.myapplication;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import com.example.myapplication.databinding.ActivityMainBinding;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
    
            setContentView(binding.getRoot());
    
            binding.myHeader.foo.setText("this is a test");
        }
    }}
    

    我检查了它在我的新项目中是否有效。 我希望它会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 2016-01-20
      • 1970-01-01
      • 2019-12-03
      • 2018-01-31
      • 1970-01-01
      相关资源
      最近更新 更多