RecyclerView项的属性在使用DataBinding时自动更改
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView项的属性在使用DataBinding时自动更改相关的知识,希望对你有一定的参考价值。
我正在尝试使用DataBinding(在Java中)构建没有任何设计模式的简单任务。场景是有一个RecyclerView,其中的项目/产品是从HardCoded列表中绑定的。每当用户单击任何item时,都应在左侧(空白)显示具有默认值(属性)的特定项目。现在,当用户单击左侧的item时,其属性(数量,价格等)应更改(仅在该左侧的框中)。现在一切都很好。问题在于,更改左侧项目的属性后,每当用户再次单击同一item(他之前单击过)时,它都会在item上显示更改后的值。左边。但是我希望再次单击它应该显示具有默认值的item。我已经附上了所有的课程和屏幕截图。如果我做错了,请识别我。
Product.java
public class Product {
private String title;
private String price;
private String image;
private String quantity;
private String discount;
public Product(String title, String price, String image, String quantity, String discount) {
this.title = title;
this.price = price;
this.image = image;
this.quantity = quantity;
this.discount = discount;
}
//Getters and Setters
}
MainTestActivity.java
public class MainTestActivity extends AppCompatActivity implements ProductsListAdapter.ProductItemClickListener{
ActivityMainTestingBinding binding;
private Product myCurrentProduct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main_testing);
getItems();
}
private void getItems(){
List<Product> products = new ArrayList<>();
products.add(new Product("Black Grapes", "480", "", "1", "0"));
products.add(new Product("Oranges", "198", "", "1", "0"));
products.add(new Product("Pears", "170", "", "1", "0"));
products.add(new Product("Apple", "169", "", "1", "0"));
products.add(new Product("Jonagold", "110", "", "1", "0"));
products.add(new Product("Lemon", "198", "", "1", "0"));
binding.rvProductList.setLayoutManager(new GridLayoutManager(this, 5));
binding.rvProductList.setAdapter(new ProductsListAdapter(products, this));
}
@Override
public void onProductItemClicked(Product product) {
myCurrentProduct = product;
binding.setPopUpProduct(myCurrentProduct);
binding.cvCurrentProduct.setVisibility(View.VISIBLE);
binding.cvCurrentProduct.setOnClickListener(v -> {
myCurrentProduct.setDiscount("100");
myCurrentProduct.setPrice("50000");
myCurrentProduct.setQuantity("5");
myCurrentProduct.setTitle("Null");
binding.setPopUpProduct(myCurrentProduct);
});
}
}
ProductsListAdapter.java
public class ProductsListAdapter extends RecyclerView.Adapter<ProductsListAdapter.ViewHolder> {
private ProductItemClickListener mListener;
private List<Product> products;
public ProductsListAdapter(List<Product> products, ProductItemClickListener mListener) {
this.products = products;
this.mListener = mListener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
ItemListProductsBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.item_list_products, viewGroup, false);
return new ViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
holder.bind(products.get(i), mListener);
}
@Override
public int getItemCount() {
return products.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ItemListProductsBinding binding;
public ViewHolder(ItemListProductsBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
void bind(Product currentProduct, ProductItemClickListener clickListener){
//For each item, corresponding product object is passed to the binding
binding.setProduct(currentProduct);
binding.setProductItemClick(clickListener);
//This is to force bindings to execute right away
binding.executePendingBindings();
}
}
public interface ProductItemClickListener {
void onProductItemClicked(Product product);
}
}
activity_main_testing.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="popUpProduct"
type="tech.oratier.parlour.models.Product" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:id="@+id/cvCurrentProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:cardCornerRadius="5dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/garamond"
android:hint="Product Title"
android:text="@{popUpProduct.title}"
android:textSize="30sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/garamond"
android:text="Quantity:"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/garamond"
android:gravity="end"
android:text="@{popUpProduct.quantity}"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/garamond"
android:text="Discount:"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/garamond"
android:gravity="end"
android:text="@{popUpProduct.discount}"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/garamond"
android:text="Price:"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/garamond"
android:gravity="end"
android:text="@{popUpProduct.price}"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/seperator" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/garamond"
android:text="Total"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/garamond"
android:gravity="end"
android:text="@{popUpProduct.price != null ? (popUpProduct.quantity != null ? (popUpProduct.discount != null ? String.valueOf((Double.parseDouble(popUpProduct.price) * Double.parseDouble(popUpProduct.quantity)) - Double.parseDouble(popUpProduct.discount)) : String.valueOf(Double.parseDouble(popUpProduct.price) * Double.parseDouble(popUpProduct.quantity))) : @string/nothing) : @string/nothing}"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
<View
android:layout_width="3dp"
android:layout_height="match_parent"
android:background="@color/seperator" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvProductList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
item_list_products.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="product"
type="tech.oratier.parlour.models.Product" />
<variable
name="productItemClick"
type="tech.oratier.parlour.adapters.ProductsListAdapter.ProductItemClickListener" />
</data>
<com.google.android.material.card.MaterialCardView
android:layout_marginTop="5dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:onClick="@{()->productItemClick.onProductItemClicked(product)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:contentPaddingRight="5dp"
android:orientation="vertical">
<LinearLayout
android:paddingStart="16dp"
android:paddingRight="16dp"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="100dp"
android:layout_height="wrap_content">
<ImageView
android:layout_marginTop="8dp"
android:src="@mipmap/ic_launcher_round"
android:layout_width="100dp"
android:layout_height="100dp"/>
<TextView
android:fontFamily="@font/garabd"
android:paddingLeft="8dp"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:background="@color/priceBackground"
android:textColor="@color/defaultTextColor"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="@{product.price}"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<com.google.android.material.textview.MaterialTextView
android:singleLine="true"
android:textSize="20sp"
android:textColor="@android:color/black"
android:fontFamily="@font/raleway_regular"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:text="@{product.title}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.textview.MaterialTextView
android:singleLine="true"
android:textSize="20sp"
android:textColor="@android:color/black"
android:fontFamily="@font/raleway_regular"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:text="@{product.quantity}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
问题是我正在使用
myCurrentProduct = product;
此处将myCurrentProduct与product / layout绑定在一起,因此其值会自动更新。因此,为了摆脱这一点,我声明了另一个对象tempProduct as,
tempProduct = product;
然后我从tempProduct]获取值
myCurrentProduct = new Product(); //create a default constructor in Product model class myCurrentProduct.setTitle(tempProduct.getTitle()); myCurrentProduct.setQuantity("5"); myCurrentProduct.setPrice("50000"); myCurrentProduct.setDiscount(tempProduct.getDiscount()); binding.setPopUpProduct(myCurrentProduct);
这样,我还可以从单击的项目中获取值,也可以设置自己的值。
以上是关于RecyclerView项的属性在使用DataBinding时自动更改的主要内容,如果未能解决你的问题,请参考以下文章
当列表填满屏幕时,RecyclerView 显示第一项的副本
java 默认情况下,包含progressbar项的base recyclerview适配器类。
从recyclerview项目中保存模型类中选定的RadioButton文本
在 RecyclerView 列表之后显示 ProgressBar