小编ped*_*dro的帖子

如果绑定适配器提供 getter,请检查适配器注释是否正确以及参数类型是否匹配

我在 Android 中使用 databindig 时遇到了一些问题。我想通过 databindig 设置 ImageView 的可见性,并且我认为我已经完成了 Android 博客上与数据绑定相关的所有操作,尽管我收到了构建错误消息。

我的布局 XML 文件如下:

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

    <data>
        <variable
            name="mapViewModel"
            type="neptun.jxy1vz.cluedo.ui.map.MapViewModel" />
    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/mapLayout"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/ivMap"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:src="@drawable/map"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    android:contentDescription="@string/map_description" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/ivDoor"
                    android:src="@drawable/door_left"
                    app:layout_constraintStart_toStartOf="@id/guidelineCol7"
                    app:layout_constraintTop_toTopOf="@id/guidelineRow1"
                    android:visibility="@={mapViewModel.doorVisibility}"/>

                    <!-- Guidelines... they are not interesting -->

            </androidx.constraintlayout.widget.ConstraintLayout>
        </HorizontalScrollView>
    </ScrollView>
</layout>
Run Code Online (Sandbox Code Playgroud)

我的 ViewModel 类:

package neptun.jxy1vz.cluedo.ui.map

import android.view.View
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import …
Run Code Online (Sandbox Code Playgroud)

android visibility android-imageview android-databinding android-binding-adapter

5
推荐指数
1
解决办法
1万
查看次数