我正在做什么的基本概念:
我正在我的“类别”片段中设置回收站视图,该片段显示产品图像,标题,价格和计数器,单击该计数器可更改该特定项目的数量。我在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>
下面是类别片段中的方法,该方法从ProductRecyclerViewAdapter的onBindViewHolder方法获取totalQTY
public void setCheckoutToolbarText(int totalQTY, double totalPrice){
Log.i("totalQTY",Integer.toString(totalQTY));
}
当前,我已经在我的productList中添加了6个产品,并且在屏幕上显示了4个,向下滚动时显示了剩余的2个。我所做的调试工作是,我分别单击了屏幕上4个产品中每个产品的+计数器,因此日志中的totalQTY从1增加到了4。
但是,当我向下滚动并单击第5个产品时,onBindViewHolder中的totalQTY重置为0,单击后得到更新,并将1发送回上述方法,而不是发送5。 然后,我单击第6个图像,totalQTY变为2。然后再次滚动并单击第1个4个产品,totalQTY变为3、4、5、6。然后,当我再次向下滚动时,这次totalQTY在单击最后2个产品时正确显示了7,8。
因此,只有在我第一次向下滚动时,我所面临的问题才会发生。
Log.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
答案 0 :(得分:1)
那是因为以下代码:
@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尝试重用并回收子视图项目时,也会发生这种情况。