我已经实现了一个RecyclerView。我为它编写了一个自定义适配器。它呈现带有一些 TextView 和按钮的卡片列表。我在每张卡内有两个按钮,一个加号按钮和一个减号按钮,我想根据单击这些按钮来更改 textViews 内的值。它确实工作正常,但是当我们向下滚动时,随着单击的卡一起,另一张卡的 textView 也会更新。我知道它正在发生,因为对象被回收并且我在代码中使用了该位置。如何阻止它更新其他卡?这是代码:
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final Product product = productList.get(position);
holder.productName.setText(product.getName());
holder.productPrice.setText("$ " + product.getPrice().toString());
holder.productImage.setImageUrl(product.getImage(), holder.imageLoader);
//load image and actions for buttons
array = new int[productList.size()];
holder.plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
productList.get(position).setTotal_on_hand(1);
holder.productQty.setText(product.getTotal_on_hand());
this.notify();
Product pr = productList.get(position);
int pos;
if (passList.contains(pr)) {
pos = passList.indexOf(pr);
array[pos]++;
priceTotal = priceTotal + pr.getPrice().floatValue();
qtyTotal++;
holder.productQty.setText(Integer.toString(array[pos]));
} else {
passList.add(pr);
pos = passList.indexOf(pr);
array[pos] = 1;
qtyTotal++;
priceTotal = priceTotal + pr.getPrice().floatValue();
holder.productQty.setText(Integer.toString(array[pos]));
}
callback.deletePressed();
}
});
最佳答案
在 onBindViewHolder(ViewHolderholder,intposition) 方法上尝试一下
@Override
public void onBindViewHolder(final Helper holder, int position) {
holder.setIsRecyclable(false);
}
关于java - Android RecyclerView,基于OnClick改变 View 。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38288100/