[英]Android switch item position programmatically in a RelativeLayout
[英]Android Recylcerview set Position of Item programmatically
我為myChats創建了recylcerview。 在此Recyclerview中,我可以看到一個拇指,最后一條消息和名稱。 如果我發送或接收新消息,則該項目應轉到第一個位置,例如在whatsapp中。 為接收新消息,我創建了以下方法:
private void getLastMSG(final String userId, final TextView lastMSG){
String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child(userid).child("connections").child("matches").child(userId);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
String lastMsg = dataSnapshot.child("lastMsg").getValue().toString();
lastMSG.setText(lastMsg);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
現在我想將此項目設置為第一個位置,但我不知道如何
您需要致電:
您的recyclerview
適配器中的notifyItemMoved(oldPos, newPos)
。 請注意,您需要在此之前更新數據模型,以便在onBindViewHolder
方法中綁定正確的視圖。
請參閱適配器文檔 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.