[英]Variable value resets in onBindViewHolder as i am scrolling down my recycler view for the first time
我在做什么的基本想法:
我正在從我的“類別”片段中設置我的回收者視圖,該片段顯示產品圖像、其標題、價格和計數器,單擊時會更改該特定項目的數量。 我在 ProductRecyclerViewAdapter 中定義了一個名為 totalQTY 的全局變量,它存儲生成的回收器視圖中所有產品的總量,並將數據發送回我的“類別”片段中的方法。
我面臨的問題是什么?
當我第一次向下滾動我的回收器視圖時,之前正常更新的 totalQTY 被重置為 0。但是,當我從這里向上然后再次向下滾動時,totalQTY 現在正在增加,因為它應該和永遠不會被重置。 簡而言之,重置僅在第一次向下滾動回收器視圖時發生。
我的 ProductRecyclerViewAdapter 類
public class ProductRecyclerViewAdapter extends RecyclerView.Adapter<ProductRecyclerViewAdapter.ProductViewHolder>{
private Context context;
private List<Product> productList;
private CategoriesFragment fragment;
private int totalQTY;
private double totalPrice = 0;
public ProductRecyclerViewAdapter(Context context, List<Product> productList, CategoriesFragment fragment) {
this.context = context;
this.productList = productList;
this.fragment = fragment;
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View view = inflater.inflate(R.layout.product_recyclerview_list_item,viewGroup,false);
ProductViewHolder holder = new ProductViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final @NonNull ProductViewHolder prouctViewHolder, int i) {
final Product product = productList.get(i);
prouctViewHolder.pdtTitle.setText(product.getTitle());
prouctViewHolder.pdtPrice.setText("MRP Rs " + String.valueOf(product.getPrice()));
prouctViewHolder.pdtImageView.setImageDrawable(context.getResources().getDrawable(product.getImageId()));
totalQTY = Integer.parseInt(prouctViewHolder.counter.getText().toString()); //Initially 0
prouctViewHolder.qtyminus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int qty = Integer.parseInt(prouctViewHolder.counter.getText().toString());
if (qty > 0) {
qty--;
totalPrice = totalPrice - product.getPrice();
totalQTY--;
fragment.setCheckoutToolbarText(totalQTY,totalPrice);
if(totalQTY==0) {
fragment.makeCheckoutGone();
}
} else {
Toast.makeText(context, "Cannot have a negative quantity\nCAN YOU?", Toast.LENGTH_LONG).show();
}
prouctViewHolder.counter.setText(String.valueOf(qty));
}
});
prouctViewHolder.qtyplus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int qty = Integer.parseInt(prouctViewHolder.counter.getText().toString());
qty++;
totalPrice += product.getPrice();
totalQTY++
prouctViewHolder.counter.setText(String.valueOf(qty));
fragment.setCheckoutToolbarText(totalQTY,totalPrice);
fragment.makeCheckoutVisible();
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
ImageView pdtImageView;
TextView pdtTitle, pdtPrice, counter;
Button qtyminus, qtyplus;
public ProductViewHolder(@NonNull View itemView) {
super(itemView);
pdtImageView = itemView.findViewById(R.id.product_imageView);
pdtTitle = itemView.findViewById(R.id.textViewTitle);
pdtPrice = itemView.findViewById(R.id.textViewPrice);
counter = itemView.findViewById(R.id.qty_counter);
qtyminus = itemView.findViewById(R.id.qty_minus);
qtyplus = itemView.findViewById(R.id.qty_plus);
}
}
我的 product_recycler_view_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:background="@drawable/product_background"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp">
<ImageView
android:id="@+id/product_imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_marginStart="16dp"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:scaleType="centerInside"
android:background="@null"/>
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="@id/product_imageView"
android:text="Banana"
android:fontFamily="@font/baloo"
android:textSize="20sp"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/textViewPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewTitle"
android:layout_marginStart="16dp"
android:layout_toEndOf="@id/product_imageView"
android:text="Rs 120"
android:textStyle="bold"
android:textColor="@color/green"/>
<Button
android:id="@+id/qty_minus"
android:background="@null"
android:text="-"
android:textColor="@color/red"
android:textSize="20sp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="@+id/textViewPrice"
android:layout_toEndOf="@id/product_imageView"
android:layout_marginTop="16dp"/>
<TextView
android:id="@+id/qty_counter"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewPrice"
android:layout_toEndOf="@id/qty_minus"
android:layout_marginTop="26dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"/>
<Button
android:id="@+id/qty_plus"
android:background="@null"
android:text="+"
android:textColor="@color/red"
android:textSize="20sp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="@+id/textViewPrice"
android:layout_toEndOf="@id/qty_counter"
android:layout_marginTop="16dp"/>
</RelativeLayout>
</LinearLayout>
下面是方法,在 Categories Fragment 中,它從 ProductRecyclerViewAdapter 的 onBindViewHolder 方法中獲取 totalQTY
public void setCheckoutToolbarText(int totalQTY, double totalPrice){
Log.i("totalQTY",Integer.toString(totalQTY));
}
目前,我已將 6 個產品添加到我的產品列表中,屏幕上顯示了 4 個,向下滾動時顯示剩余的 2 個。 我為調試所做的是,我分別點擊了屏幕上 4 個產品中每一個的 + 計數器,因此 log 中的 totalQTY 從 1 增加到 4。
但是,當我向下滾動並單擊第 5 個產品時,onBindViewHolder 中的 totalQTY 重置為 0,在單擊時更新並將 1 發送回上述方法而不是發送 5。然后我單擊第 6 個圖像,totalQTY 變為2. 然后我再次向上滾動並點擊第一個 4 個產品,totalQTY 變成了 3、4、5、6。 然后當我再次向下滾動時,這次在單擊最后 2 個產品時 totalQTY 正確顯示為 7,8。
因此,我面臨的問題只有在我第一次向下滾動時才會發生。
日志.i
2019-07-03 10:11:32.311 17011-17011/com.example.gofresh I/totalQTY: 1
2019-07-03 10:11:34.150 17011-17011/com.example.gofresh I/totalQTY: 2
2019-07-03 10:11:35.996 17011-17011/com.example.gofresh I/totalQTY: 3
2019-07-03 10:11:39.659 17011-17011/com.example.gofresh I/totalQTY: 4
2019-07-03 10:11:41.015 17011-17017/com.example.gofresh I/example.gofres: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2019-07-03 10:11:41.956 17011-17011/com.example.gofresh I/totalQTY: 1
2019-07-03 10:11:51.176 17011-17011/com.example.gofresh I/totalQTY: 2
2019-07-03 10:11:53.421 17011-17011/com.example.gofresh I/totalQTY: 3
2019-07-03 10:11:56.142 17011-17011/com.example.gofresh I/totalQTY: 4
2019-07-03 10:11:57.337 17011-17011/com.example.gofresh I/totalQTY: 5
2019-07-03 10:11:58.545 17011-17011/com.example.gofresh I/totalQTY: 6
2019-07-03 10:11:59.902 17011-17011/com.example.gofresh I/totalQTY: 7
2019-07-03 10:12:01.352 17011-17011/com.example.gofresh I/totalQTY: 8
那是因為下面的代碼:
@Override
public void onBindViewHolder(final @NonNull ProductViewHolder prouctViewHolder, int i) {
final Product product = productList.get(i);
prouctViewHolder.pdtTitle.setText(product.getTitle());
...
// here is the problem.
totalQTY = Integer.parseInt(prouctViewHolder.counter.getText().toString());
...
}
當您向下滾動 RecyclerView 時,適配器將嘗試為該項目創建另一個子視圖。 因此totalQTY
始終設置為 0,這是您的counter
TextView 的默認文本值。 當您的 RecyclerView 嘗試重用和回收您的子視圖項時,也會發生這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.