I'm assuming I am just not understanding the RecyclerView properly, however I am having a problem with adding an item after it's been deleted.
My view consists of a card layout with some text and two image views.
The two image views are stacked, and when the card is selected, the first view is flipped then, the second is flipped in and set to visible. When an item is selected the user can edit or delete it.
When it is deleted, and then brought back with an undo action, the imageview remains flipped. (Even though when I delete it I flip it back before removing). I have included an example.
Also if I add multiple new entries at once, some of them will have the flipped imageview. So what am I doing wrong?
The Adapter
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);
}
}
The Viewholder
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()));
}
}