Search code examples
androidlayoutviewandroid-inflate

DataBindingUtil bind layout from include


I have such content.xml Layout

<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

And how can I bind fragment_list from include to DataBindingUtil.inflate or DataBindingUtil.bind?

like DataBindingUtil.inflate(inflater, R.layout.fragment_list, container, false); but throw content.xml Layout


Solution

  • To use DataBinding in for fragment in android, use following way->

    Create ->

    private FragmentYourVideosBinding binding;
    private View view;
    

    Then use - >

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

    To access the FragmentBindinding just type FragmentBinding then you will see your Fragment Binding layout name in dropdownlist. Is described in above link you can read from there.

    Firstly wrap your all child layouts by layout tag as below. You can copy the code its same as your code.

                <?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>