0
我已經實現了一個RecyclerView。我爲它寫了一個自定義適配器。它使用一些textView和按鈕呈現卡片列表。我在每張卡片裏都有兩個按鈕,一個是plusButton和一個minusButton,我想根據點擊這些按鈕來更改textViews中的值。它確實工作正常,但是當我們向下滾動時,隨着被點擊的卡片,另一張卡片的textView也會被更新。我知道這是因爲這些對象被回收了,我在代碼中使用了這個位置。如何阻止它更新其他卡?下面是代碼:Android RecyclerView,基於OnClick的改變視圖。
@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();
}
});
它解決了這個問題,即它不能在回收視圖更新等證。但是,當我向下滾動並且卡片離開視圖,然後向上滾動時,它會恢復到原始狀態。 –
記錄你正在改變的事物。你可以使用一個新的數組列表來保存你根據位置而改變的東西。 –