我假设我没有正确理解RecyclerView,但是在删除项目后我遇到了问题。
我的视图包含一个带有一些文字和两个图像视图的卡片布局。
堆叠两个图像视图,当选择卡时,翻转第一个视图,然后翻转第二个视图并设置为可见。选择项目后,用户可以编辑或删除它。
当它被删除,然后通过撤消操作返回时,图像视图将保持翻转状态。 (即使我删除它,我在删除之前将其翻转)。我已经列举了一个例子。
此外,如果我一次添加多个新条目,其中一些将具有翻转的imageview。那么我做错了什么?
适配器
public void unSelect(List<Message> messages) {
for (Message message : messages) {
if (message.isSelected()) {
int position = mMessages.indexOf(message);
Message m = mMessages.get(position);
m.setSelected(false);
m.setWasSelected(true);
notifyItemChanged(position);
}
}
}
public int add(Message message) {
int position = 0;
if (message != null) {
message.save();
mMessages.add(message);
Collections.sort(mMessages);
Collections.reverse(mMessages);
position = mMessages.indexOf(message);
notifyItemInserted(position);
}
return position;
}
public void delete(Message message) {
int position = mMessages.indexOf(message);
if (position != -1) {
message.setSelected(false);
message.setWasSelected(false);
message.delete();
mMessages.remove(message);
notifyItemRemoved(position);
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Message message = mMessages.get(position);
if (message.isSelected()) {
holder.iconReverse.setVisibility(View.VISIBLE);
holder.selected = true;
} else if (message.wasSelected()) {
holder.iconReverse.setVisibility(View.VISIBLE);
holder.selected = false;
holder.animator.reset(true);
mMessages.set(position, message);
}
}
观众
ImageView icon, iconReverse, repeat;
TextView recipient, date, message, recipientNum;
IconAnimator animator;
boolean hasExtraRecipient;
boolean selected;
public ViewHolder(View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.icon);
iconReverse = (ImageView) itemView.findViewById(R.id.icon_reverse);
recipient = (TextView) itemView.findViewById(R.id.contact);
recipientNum = (TextView) itemView.findViewById(R.id.recipient_num);
animator = new IconAnimator(mContext, icon, iconReverse);
}
@Override
public void onClick(final View v) {
recipientNum.setVisibility(View.INVISIBLE);
animator.start(selected);
mMessages.get(getAdapterPosition()).setSelected(selected = !selected);
if (mListener != null) {
mListener.cardSelected(v, mMessages.get(getAdapterPosition()));
}
}
答案 0 :(得分:1)
如果未选择该项目(message.isSelected() == false
)且未选择该项目(message.wasSelected() == false
),则忘记处理该案例。否则RecyclerView
的回收机制将导致此问题。
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Message message = mMessages.get(position);
if (message.isSelected()) {
holder.iconReverse.setVisibility(View.VISIBLE);
holder.selected = true;
} else if (message.wasSelected()) {
holder.iconReverse.setVisibility(View.VISIBLE);
holder.selected = false;
holder.animator.reset(true);
mMessages.set(position, message);
} else {
// add the necessary stuff
hereholder.selected = false;
....
}
}