gpt4 book ai didi

java - RecyclerView notificationItemMoved() 复制移动的 View

转载 作者:行者123 更新时间:2023-12-02 03:39:46 25 4
gpt4 key购买 nike

我正在将回收器 View 中的选定 View 从其位置移动到位置 0(回收器 View 的顶部)。但问题是,当项目移动时,它会重复自身,这意味着它位于顶部以及之前的位置,如何摆脱它?

我尝试使用scrollToPosiotion()函数滚动itemview,但它不起作用。现在形成notifyItemMoved()它确实移动到顶部但重复,当我关闭并重新打开应用程序时,重复的 View 删除仅显示在顶部。

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) {

}
});

我希望该项目在不关闭并重新打开应用程序的情况下不会重复自身。

最佳答案

您不仅可以将项目移动到回收器 View 中,还可以移动到列表中。

试试这个

yourList.remove(holder.getAdapterPosition());
yourList.add(0, yourData);
adapter.notifyItemMoved(holder.getAdapterPosition(),0);

关于java - RecyclerView notificationItemMoved() 复制移动的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56842418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com