Reputation: 45
I am moving a selected view in recycler view from whatever its position to position 0 (top of recyclerview). But the problem is when item is moves it duplicates itself means it is on top as well as on its previous location how to get rid of that?
I tries scrolling the itemview with scrollToPosiotion() function but it doesnot work. Now form notifyItemMoved() It does moves to the top but duplicates and when i close and reopen the application, The duplicated view removes only shows on top.
MssgRef = FirebaseDatabase.getInstance().getReference().child("Messages").child(currentUserID).child(userIDs);
MssgRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot==null)return;
int unread = 0;
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Messages messages = snapshot.getValue(Messages.class);
if (messages.getReceiver().equals(currentUserID)&& messages.getSender().equals(userIDs) && !messages.isIsseen()){
unread++;
}
}
if (unread != 0){
holder.unread.setVisibility(View.VISIBLE);
//move to the top
notifyItemMoved(holder.getAdapterPosition(),0);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
I want that the item does not duplicate itself without closing and reopening the app.
Upvotes: 0
Views: 279
Reputation: 1032
You do not just move item in the recyclerview but also into the list.
Try this
yourList.remove(holder.getAdapterPosition());
yourList.add(0, yourData);
adapter.notifyItemMoved(holder.getAdapterPosition(),0);
Upvotes: 0