簡體   English   中英

帶有包含標記的 Android 數據綁定不起作用

[英]Android data binding with include tag doesn't work

我正在使用 android 數據綁定,只要我不使用包含標簽,它就可以正常工作。

如果我使用了一個視圖標記(即使它顯示在應用程序上)但數據綁定對象無法識別包含的布局的視圖。 這些是我的布局文件。

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>

片段.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>

如您所見,progressBar、swipeRefreshLayout 和 recyclerView 顯示在建議中,除了包含的布局和其中的視圖。

有什么想法可以解決這個問題嗎?

執行重建為我解決了這個問題。

重建項目

以下是我的 AndroidStudio 重建后的截圖:

建造工程

我的 build.gradle:

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

我的 gradle.properties:

android.databinding.enableV2=true

謝謝各位。 我認為這是我 PC 的 Android Studio 中的一個問題。 我在另一台 PC 上打開了該項目,現在它正在運行。 可能我應該重新安裝 Android Studio 並嘗試一下。

在此處輸入圖片說明

想與那些仍在掙扎的人分享我的經驗......我遇到了同樣的問題,直到在應用程序的 build.gradle 中打開viewBinding (除了dataBinding )才解決。 (Android Studio 4.2

buildFeatures {
    dataBinding = true
    viewBinding = true
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM