如何在DataBinding中获得包含的XML绑定

时间:2018-11-14 12:47:08

标签: android data-binding android-databinding

我有一个DataBinding类ActivityMainBinding,其中XMl像这样

    <?xml version="1.0" encoding="utf-8"?>
    <layout>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <include layout="@layout/content_main" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@drawable/ic_add" />

    </android.support.design.widget.CoordinatorLayout>
    </layout>

content_main.xml是另一个绑定

    <?xml version="1.0" encoding="utf-8"?>
    <layout>

        <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            tools:showIn="@layout/activity_main">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:clickable="true"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:scrollbars="vertical"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>
    </layout>

现在,在我的活动中,我这样设置内容视图。

        ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

现在,我可以使用此绑定访问所有XML元素。像

        Toolbar toolbar = activityMainBinding.toolbar;            

但是我的一些小部件在包含的布局中,ContentMainBinding

现在,如何从ContentMainBinding创建一个ActivityMainBinding实例?

我尝试了

        contentMainBinding = DataBindingUtil.getBinding(activityMainBinding.container);

但是,它崩溃并显示错误

         ActivityMainBindingImpl cannot be cast to ContentMainBinding                 

2 个答案:

答案 0 :(得分:3)

无需在您的content_main.xml文件中添加<layout>----</layout>标记。您需要设置包含布局的ID,例如:

 <?xml version="1.0" encoding="utf-8"?>
    <layout>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <include layout="@layoutcontent_main" android:id="@+id/contentMain" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@drawable/ic_add" />

    </android.support.design.widget.CoordinatorLayout>
    </layout>

然后您可以使用

直接访问包含的布局视图
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
activityMainBinding.contentMain.recyclerView.setAdapter();

答案 1 :(得分:2)

添加ID以包含标签

<include
    android:id="@+id/included"
    layout="@layout/content_main"/>

在代码中使用

activityMainBinding.included.recyclerView

检查link