DataBindingUtil 从 include 绑定布局

DataBindingUtil bind layout from include

我有这样的content.xml布局

<androidx.constraintlayout.widget.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"
android:orientation="vertical">
    <view.TouchyWebView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/content_view" >
    </view.TouchyWebView>

    <include
        android:id="@+id/view_list"
        layout="@layout/fragment_list"
        android:layout_width="match_parent"
        android:layout_height="596dp"
        app:layout_constraintTop_toBottomOf="@+id/content_view"
        tools:layout_editor_absoluteX="-9dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
View view = inflater.inflate(R.layout.content, container, false);
WebView webView = view.findViewById(R.id.content_view);
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_list, container, false); // this does'n work

如何将 fragment_list 从 include 绑定到 DataBindingUtil.inflate 或 DataBindingUtil.bind?

喜欢DataBindingUtil.inflate(inflater, R.layout.fragment_list, container, false);但抛出content.xml布局

要在 android 中的片段中使用 DataBinding,请使用以下方式->

创建 ->

private FragmentYourVideosBinding binding;
private View view;

然后使用->

binding = DataBindingUtil.inflate(inflater, R.layout.fragment_your_videos, container, false);
view = binding.getRoot();
return view;

要访问 FragmentBindinding,只需键入 FragmentBinding,然后您将在下拉列表中看到您的 Fragment Binding 布局名称。上面有描述 link 你可以从那里阅读。

首先用布局标签包裹所有子布局,如下所示。您可以复制与您的代码相同的代码。

            <?xml version = "1.0" encoding = "utf-8" ?>
                <layout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    xmlns:bind="http://schemas.android.com/tools" >


                    <androidx.constraintlayout.widget.ConstraintLayout 
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">
                        <view.TouchyWebView
                            android:layout_width="match_parent"
                            android:layout_height="100dp"
                            android:id="@+id/content_view" >
                        </view.TouchyWebView>

                        <include
                            android:id="@+id/view_list"
                            layout="@layout/fragment_list"
                            android:layout_width="match_parent"
                            android:layout_height="596dp"
                            app:layout_constraintTop_toBottomOf="@+id/content_view"
                            tools:layout_editor_absoluteX="-9dp" />
                    </androidx.constraintlayout.widget.ConstraintLayout>

                </layout>