jeff
jeff

Reputation: 23

how to use databinding in fragment

I use DataBinding in fragment ,but there are some trouble this is the fragment

enter image description here

and this is my layout

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
</layout>

how can I fix this?

Upvotes: 1

Views: 1535

Answers (2)

Sharath kumar
Sharath kumar

Reputation: 4132

Change binding.getRoot() to inflate.getRoot()

Upvotes: 2

Headcracker
Headcracker

Reputation: 531

You must wrap your layout in a layout tag:

<layout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

<layout>

Also ensure that you enable databinding in your module's build.gradle file:

dataBinding {
    enabled true
}

And note that you named your binding variable inflate but you use binding.getRoot() in the return statement. So rename your variable to binding. Or change your return statement to return inflate.getRoot(); Whatever you prefer.

Upvotes: 2

Related Questions