首页 > 解决方案 > Fragments RecyclerView Databinding Kotlin - 未解析的引用变量名称

问题描述

我正在尝试DataBinding一个包含一些片段的小项目,其中一个片段托管一个RecyclerView. 子项目有一个DataBinding我无法在RecyclerView适配器中访问的。如何解决这个问题? 在此处输入图像描述

ShopFragment.KT

class ShopFragment : Fragment(),ShopItemsAdapter.ShopInterface {
  lateinit  var fragmentShopBinding: FragmentShopBinding;
    lateinit var shopItemsAdapter: ShopItemsAdapter ;
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        fragmentShopBinding= FragmentShopBinding.inflate(inflater,container,false)

        return  fragmentShopBinding.root;

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        shopItemsAdapter = ShopItemsAdapter();
        fragmentShopBinding.shopRV.adapter=shopItemsAdapter;
        super.onViewCreated(view, savedInstanceState)
    }

    override fun addItem(productModel: ProductModel) {
        TODO("Not yet implemented")
    }

    override fun onItemClick(productModel: ProductModel) {
        TODO("Not yet implemented")
    }
}

片段商店.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".views.ShopFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/shopRV"
        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        app:spanCount="2"
        tools:listitem="@layout/shop_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

shopItem.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="productModel"
        type="com.example.mvvm_shopping_cart.models.ProductModel" />
    </data>

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


        <ImageView
            android:id="@+id/productIV"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            app:srcCompat="@drawable/iphone" />

        <TextView
            style="@style/TextAppearance.MaterialComponents.Body1"
            android:layout_gravity="center"
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{productModel.name}" />

        <TextView
            android:layout_gravity="center"
            android:id="@+id/priceTV"
            style="@style/TextAppearance.MaterialComponents.Caption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{productModel.price.toString()}" />

        <TextView
            android:layout_gravity="center"
            android:id="@+id/availableTV"
            style="@style/TextAppearance.MaterialComponents.Caption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{productModel.available? `Available` : `Out of stock`}" />

        <Button
            android:textAppearance="@style/TextAppearance.MaterialComponents.Caption"
            style="@style/Widget.MaterialComponents.Button.TextButton"
            android:id="@+id/AddBtn"
            android:enabled="@{productModel.available}"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add to cart" />
    </LinearLayout>
</layout>

产品模型.kt

class ProductModel(var id : Number,var  name : String,var  imageUrl : String, var price : Number, var isAvailable : Boolean) {
    override fun toString(): String {
        return super.toString()
    }

    override fun equals(other: Any?): Boolean {
        return super.equals(other)
    }

    class ShopItemCallBack : DiffUtil.ItemCallback<ProductModel>() {
        override fun areItemsTheSame(oldItem: ProductModel, newItem: ProductModel): Boolean {
            return newItem.id == oldItem.id;
        }

        override fun areContentsTheSame(oldItem: ProductModel, newItem: ProductModel): Boolean {
            return newItem == oldItem;
        }

    }

}

ShopItemAdapter.kt

class ProductModel(var id : Number,var  name : String,var  imageUrl : String, var price : Number, var isAvailable : Boolean) {
    override fun toString(): String {
        return super.toString()
    }

    override fun equals(other: Any?): Boolean {
        return super.equals(other)
    }

    class ShopItemCallBack : DiffUtil.ItemCallback<ProductModel>() {
        override fun areItemsTheSame(oldItem: ProductModel, newItem: ProductModel): Boolean {
            return newItem.id == oldItem.id;
        }

        override fun areContentsTheSame(oldItem: ProductModel, newItem: ProductModel): Boolean {
            return newItem == oldItem;
        }

    }

}

我找不到问题,请帮忙。

标签: androidkotlinandroid-fragmentsandroid-recyclerviewdata-binding

解决方案


在此处输入图像描述

问题:

您使用了错误的生成绑定类;因为它FragmentShopBinding是生成的类,fragment_shop.xml它是一个片段布局,包含RecyclerViewifselft 不是RecyclerView项目布局。因此它没有解决这个product观点。

解决方案:

要解决此问题,您需要将 替换为FragmentShopBinding与其所在的行项目布局关联的生成类shopItem.xml,生成的类将是ShopItemBinding

class ShopViewHolder(var itemView: ShopItemBinding): RecyclerView.ViewHolder(itemView.root) {

    //......
}

推荐阅读