android - 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;
}
}
}
我找不到问题,请帮忙。
解决方案
问题:
您使用了错误的生成绑定类;因为它FragmentShopBinding
是生成的类,fragment_shop.xml
它是一个片段布局,包含RecyclerView
ifselft 不是RecyclerView
项目布局。因此它没有解决这个product
观点。
解决方案:
要解决此问题,您需要将 替换为FragmentShopBinding
与其所在的行项目布局关联的生成类shopItem.xml
,生成的类将是ShopItemBinding
:
class ShopViewHolder(var itemView: ShopItemBinding): RecyclerView.ViewHolder(itemView.root) {
//......
}
推荐阅读
- python - 将一个列表中的两个子列表合并为一个子列表python
- javascript - Python:如何对语句进行分组,例如 JavaScript 如何使用括号进行分组
- mule - 在 mule 中将没有密钥的 Json 数组转换为 SOQL 格式
- oracle - 从 EXECUTE IMMEDIATE PLSQL 输出结果
- reactjs - D3.js + React - 在折线图中显示多个工具提示
- web-services - JEE 如何决定是使用 SOAP 1.1 还是 SOAP 1.2 来实现 SOAP 服务?
- android - Unity 中 PlayerPrefs 的问题?
- java - 正则表达式字符串中的两个相等字符
- checkbox - 填写复选标记时的时间戳脚本和公式
- haskell - 如何在`hedgehog`中使用`GenT`