简体   繁体   中英

Android data binding with include tag doesn't work

I'm using android data binding and it is working fine whenever I'm not using the include tag.

If I included a view using thetag (even it is displaying on the app) but the data binding object doesn't recognize the views of the included layout. These are my layout files.

internet_unavailable.xml

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

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

        <TextView
            android:id="@+id/internet_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:gravity="center_horizontal"
            android:text="@string/msg_internet_unavailable"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/colorSecondaryText" />

        <Button
            android:id="@+id/internet_button_retry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="@dimen/internet_margin_top"
            android:backgroundTint="@color/colorAccent"
            android:paddingEnd="@dimen/internet_padding_h"
            android:paddingStart="@dimen/internet_padding_h"
            android:text="@string/str_retry"
            android:textColor="@android:color/white" />

    </LinearLayout>
</layout>

fragment.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.ncimsl.fragments.ProgressNotesFragment">

        <include
            android:id="@+id/progress_notes_internet"
            layout="@layout/internet_unavailable" />

        <ProgressBar
            android:id="@+id/progress_notes_progressbar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/progress_notes_swiperefreshlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/progress_notes_recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </android.support.v4.widget.SwipeRefreshLayout>
    </FrameLayout>
</layout>

As you can see the progressBar, swipeRefreshLayout and the recyclerView are displaying at the suggestions except the included layout and the views inside it.

Any idea to fix this?

Performing Rebuild solves the issue for me.

重建项目

Following is the screenshot from my AndroidStudio after Rebuild:

建造工程

My build.gradle:

android{
    ...
    dataBinding {
        enabled = true
    }
}

My gradle.properties:

android.databinding.enableV2=true

Thanks Guys. I think it was an issue in Android Studio of my PC. I opened the project on another PC and now it's working. Probably I should re-install the Android Studio and give a try.

在此处输入图片说明

Wanna share my experience to those who still struggle... I had the same problem and it wasn't resolved until turning on viewBinding (in addition to dataBinding) in app's build.gradle. (Android Studio 4.2

buildFeatures {
    dataBinding = true
    viewBinding = true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM