Fragments RecyclerView Databinding Kotlin - 未解析的引用变量名称

Posted

技术标签:

【中文标题】Fragments RecyclerView Databinding Kotlin - 未解析的引用变量名称【英文标题】:Fragments RecyclerView Databinding Kotlin - Unresolved reference variable name 【发布时间】:2021-12-20 07:48:05 【问题描述】:

我在一个有一些片段的小项目上尝试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")
    

FragmentShop.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_
    android:layout_
    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_
        android:layout_ />
</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_
        android:orientation="vertical"
        android:layout_>


        <ImageView
            android:id="@+id/productIV"
            android:layout_
            android:layout_
            android:layout_gravity="center"
            app:srcCompat="@drawable/iphone" />

        <TextView
            style="@style/TextAppearance.MaterialComponents.Body1"
            android:layout_gravity="center"
            android:id="@+id/textView"
            android:layout_
            android:layout_
            android:text="@productModel.name" />

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

        <TextView
            android:layout_gravity="center"
            android:id="@+id/availableTV"
            style="@style/TextAppearance.MaterialComponents.Caption"
            android:layout_
            android:layout_
            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_
            android:layout_
            android:text="Add to cart" />
    </LinearLayout>
</layout>

ProductModel.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;
        

    


我找不到问题,请帮忙。

【问题讨论】:

【参考方案1】:

问题:

您使用了错误的生成绑定类;因为FragmentShopBindingfragment_shop.xml 的生成类,它是一个片段布局,包含RecyclerView ifselft 而不是RecyclerView 项目布局。因此它没有解析 product 视图。

解决办法:

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

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

    //......

【讨论】:

以上是关于Fragments RecyclerView Databinding Kotlin - 未解析的引用变量名称的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 AsyncTask 更新 RecyclerView 项目

CoordinatorLayout中BottomNavigationView覆盖的Recyclerview

使用 recyclerview 和 Asynctask 设置 Viewpager

单击后退按钮时,在 RecyclerView 中获得相同的项目位置

在通知单击时将项目添加到片段内的 recyclerview

如何动态地将列表项添加到recyclerView?