0
我想,當點擊刪除項目但是它刪除一次,然後我得到了一個錯誤:RecyclerView上單擊刪除項目它
ERROR
Process: com.example.cardgame, PID: 3345
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 10(offset:10).state:11
CODE
public class HandViewAdapter extends RecyclerView.Adapter<HandViewAdapter.ViewHolder>{
private ArrayList<Card> cards;
private Context context;
public HandViewAdapter(ArrayList<Card> cards, Context context) {
this.cards = cards;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View cardView = inflater.inflate(R.layout.card, parent, false);
return new ViewHolder(cardView);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
Card card = this.cards.get(position);
if (position == 0) {
card.setStatus(CardStatus.down);
} else {
card.setStatus(CardStatus.up);
}
// Here i put the listener since i have tried other methods but i have no success to run them
holder.cardLayoutView.getCardView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(position);
}
});
}
@Override
public int getItemCount() {
return this.cards.size();
}
public Card remove(int index) {
cards.remove(index);
notifyItemChanged(index);
notifyItemRangeChanged(index, cards.size());
return null;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements CardBehavior {
public CardLayoutView cardLayoutView;
public ViewHolder(View view) {
super(view);
this.cardLayoutView = new CardLayoutView(view);
}
@Override
public void faceDown() {
this.cardLayoutView.faceDown();
}
@Override
public void faceUp(Card card, Context context) {
this.cardLayoutView.faceUp(card, context);
}
}
}
據我所知,它刪除第一個項目後不更新位置,它應該更新他們對嗎?但它不,我如何解決它?我將不勝感激任何幫助,謝謝!
感謝它,這樣的工作是什麼樣的區別呢? – kobbycoder
@ kobbycoder更新瞭解答鏈接 –
感謝兄弟,我感謝您的幫助。 – kobbycoder