簡體   English   中英

找不到屬性 app:visibleGone 的 setter

[英]Cannot find setter for attribute app:visibleGone

我正在嘗試在我的 android 應用程序中實現 MVVM 架構。 我正在使用 Kotlin 做同樣的事情。

這是我的綁定適配器類:

class BindingAdapter {
    companion object {
        @JvmStatic @BindingAdapter("app:visibleGone") fun showHide(view: View, show: Boolean) {
            view.visibility =
                    if (show)
                        View.VISIBLE
                    else
                        View.GONE
        }
    }
}

這是我的 XML 文件:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
                name="isLoading"
                type="boolean"/>
    </data>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/cardview_light_background"
            android:orientation="vertical">

        <TextView
                android:id="@+id/loading_rates"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/loading_rates"
                android:textAlignment="center"
                app:visibleGone="@{isLoading}"/>

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/cardview_light_background"
                android:orientation="vertical"
                app:visibleGone="@{!isLoading}">

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="20dp"
                    android:layout_marginTop="20dp"
                    android:gravity="center_horizontal"
                    android:text="@string/rate_list"
                    android:textAlignment="center"
                    android:textSize="@dimen/rate_text"
                    android:textStyle="bold"/>

            <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rate_list"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layoutManager="LinearLayoutManager"/>

        </LinearLayout>

    </LinearLayout>
</layout>

錯誤消息說:

Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:visibleGone' with parameter type boolean on android.widget.TextView. file:/home/sparker0i/CurrencyConverter/app/src/main/res/layout/fragment_rate_list.xml loc:23:35 - 23:43 ****\ data binding error ****

第 23 行解析為 app:visibleGone 語句正上方的 loading_rates TextView 行

我無法理解,盡管在我的 Kotlin 類中設置了 BindingAdapter,但為什么我無法成功編譯代碼?

要解決該問題,請檢查以下事項:

在 root build.gradle 你有

buildscript {
    ext.android_plugin_version = '3.1.2'
    dependencies {
        classpath "com.android.tools.build:gradle:$android_plugin_version"
    }
}

在 app/build.gradle 中

apply plugin: 'kotlin-kapt'
dependencies {
    ...
    kapt "com.android.databinding:compiler:$android_plugin_version"
}

有了所有這些東西,問題應該會消失,您可以為基類編寫@BindingAdapter並將它們應用於該基類的子類。

您可以做的是嘗試在 xml 本身中隱藏和顯示視圖,您不需要單獨的綁定適配器。 我希望這個答案是你的問題。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
   <data>
       <import type="android.view.View" />

       <variable
            name="isLoading"
            type="boolean"/>
  </data>

    <TextView
            android:id="@+id/loading_rates"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/loading_rates"
            android:textAlignment="center"
            android:visibility="@{isLoading?View.VISIBLE:View.GONE}"/>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/cardview_light_background"
            android:orientation="vertical"
            android:visibility="@{isLoading?View.GONE:View.VISIBLE}">
 <layout>

您可以嘗試不使用BindingAdapter注釋中的app:前綴並更改您的第一個參數類型:

@JvmStatic
@BindingAdapter("visibleGone")
fun showHide(view: TextView, show: Boolean) {
   view.visibility = if (show) View.VISIBLE else View.GONE
}

在嘗試任何復雜的解決方案之前,只需驗證您已在app.gradle應用了以下插件

apply plugin: 'kotlin-kapt'

如果缺少它,它將不斷給出錯誤。 經過 8 個小時的爭吵和閱讀其他 SOF 帖子后,我發現我的遺漏了。 有效。

添加圖片以供參考:

添加圖片以供參考

暫無
暫無

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

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