具有一种数据类型模型的两个适配器 DataBinding

Two adapters DataBinding with One data type model

我正在使用两个带有数据绑定工具的适配器和每个适配器的一个特定项目布局,但是我对其中两个使用了一种数据模型类型...这真的可能吗?标准编码可以吗? 因为我在生成的数据绑定之一中出错 类

我的主要问题是:它是 DataBinding 模式的标准还是一种好的做法?为什么?

错误: 错误:找不到符号 导入 packageName.ItemCheckStepsBindingImpl;

我的适配器项目布局:

    <?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="checkSteps"
                type="packageName.data.model.Steps"/>//in this Line i'm using again in another Item layout
        </data>

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/white"
            app:cardElevation="8dp"
            style="@style/MyCard"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="2">



                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/user_confirm"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left|center_vertical"
                    android:layout_margin="@dimen/inner_margin"
                    android:layout_weight=".1"
                    app:srcCompat="@drawable/ic_check_green_24dp"
                    android:visibility="invisible"
                    />

                <LinearLayout
                    android:id="@+id/container"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="@dimen/inner_margin"
                    android:orientation="vertical"
                    android:layout_gravity="right"
                    android:layout_weight="1.8"
                    >


                    <androidx.appcompat.widget.AppCompatTextView
                        android:id="@+id/title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:text='@{steps.title}'
                        style="@style/HeadTextView"
                        android:layout_margin="@dimen/inner_margin"
                        />


                    <androidx.appcompat.widget.AppCompatTextView
                        android:id="@+id/description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:text='@{steps.description}'
                        style="@style/BodyTextView"
                        android:layout_margin="@dimen/inner_margin"
                        />


                </LinearLayout>

            </LinearLayout>

        </com.google.android.material.card.MaterialCardView>

</layout>

我的第二个适配器项目:

<?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="steps"
            type="com.isatelco.diettrainer.data.model.Steps"/>
    </data>

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/white"
        app:cardElevation="8dp"
        style="@style/MyCard"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="2">


            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/deleteStep"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=".1"
                android:layout_gravity="center_vertical"
                app:srcCompat="@drawable/ic_clear_red_24dp"
                />

            <LinearLayout
                android:id="@+id/container"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/inner_margin"
                android:orientation="vertical"
                android:layout_gravity="right"
                android:layout_weight="1.8"
                >


                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text='@{steps.title}'
                    style="@style/HeadTextView"
                    android:layout_margin="@dimen/inner_margin"
                    />


                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text='@{steps.description}'
                    style="@style/BodyTextView"
                    android:layout_margin="@dimen/inner_margin"
                    />


            </LinearLayout>

        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>


</layout>
<data>

    <variable
        name="name"
        type="com.example.Item"/>
</data>

/in this Line i'm using again in another Item layout

这与Java class中的一样,只是一个普通变量。您可以根据需要多次声明一个变量。

error: cannot find symbol import packageName.ItemCheckStepsBindingImpl;

正确的导入应该是

<data>
    <variable
        name="checkSteps"
        type="com.yourpackage.data.model.Steps"/>//in this Line i'm using again in another Item layout
</data>

如有可能出错,请在类型中输入步骤,然后接受建议。

如果问题没有解决,那么还要检查 以确保您没有犯任何此回答错误。

更新

确保您在适配器中导入 yourpackage.databinding.ItemCheckStepsBinding NOT packageName.ItemCheckStepsBindingImpl

更新

you can not have two items with two different name in data variable type :

你错了。您可以定义任意数量的具有多个名称的变量。

layout_one.xml

<variable
    name="itemOne"
    type="com.example.Item"/>

在javaclass

  binding.setItemOne();

layout_two.xml

<variable
    name="itemTwo"
    type="com.example.Item"/>

在javaclass

  binding.setItemTwo();

layout_three.xml

<variable
    name="itemOne"
    type="com.example.Item"/>

<variable
    name="itemTwo"
    type="com.example.Item"/>

在javaclass

  binding.setItemOne();      
  binding.setItemTwo();

以上情况都可以。

okay.i找出problem.you在数据变量类型和一个数据模型中不能有两个具有两个不同名称的项目:

<data>
    <variable
        name="name"
        type="com.example.Item"/>
</data>

我认为这是数据绑定的一个未知方面,即您不能在两个项目变量 "name" 中使用一个模型 "type" 给出两个不同的名称,因此解决方案是为两个项目指定一个特定名称项目。