我正在開發一個聊天應用程序。在這個應用程序,我想排序聯繫人列表與最新的消息時間戳。Recyclerview沒有更新?
adapter = new ContactListAdapter(ob1, getActivity(), fusername);
if (!adapted) {
chatLists.setAdapter(adapter);
adapted = true;
}else {
adapter.updateList(ob1, getActivity(), fusername);
}
在這裏setAdapter工作正常,更新完整recyclerview與setAdapter工作正常。但它會重新載入全部視圖,包括圖像和用戶消息等所有數據。所以我只想刷新索引而不需要再次加載所有數據。
關於上面的代碼。適應是一個布爾值。它的初始值是錯誤的。並將適配器初始值更改爲true後。然後更新適配器,我調用updateList函數。
public class ContactListAdapter extends RecyclerView.Adapter<ContactListAdapter.viewHolder>{
com.google.firebase.database.DataSnapshot d;
Context c;
JsonParser parser = new JsonParser();
String username;
ArrayList<ChatroomLists> ob1 = new ArrayList<ChatroomLists>();
public ContactListAdapter(ArrayList<ChatroomLists> ob1, Context c, String fusername) {
this.c = c;
this.username = fusername;
this.ob1.addAll(ob1);
}
public void updateList(ArrayList<ChatroomLists> ob1, Context c, String fusername) {
this.ob1.clear();
this.ob1.addAll(ob1);
this.notifyDataSetChanged();
}
public class viewHolder extends RecyclerView.ViewHolder {
CircleImageView user_img;
ImageView message_stats;
TextView user_name;
TextView user_message;
TextView timestamp;
CircleImageView unreadbadge;
public viewHolder(View itemView) {
super(itemView);
user_img = (CircleImageView) itemView.findViewById (R.id.user_img_list);
user_name = (TextView) itemView.findViewById (R.id.contact_name);
user_message = (TextView) itemView.findViewById (R.id.user_message);
message_stats = (ImageView) itemView.findViewById (R.id.message_status);
timestamp = (TextView)itemView.findViewById(R.id.time);
unreadbadge = (CircleImageView) itemView.findViewById(R.id.unread);
}
}
@Override
public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from (parent.getContext()).inflate (R.layout.chat_list_style, parent, false);
return new viewHolder(v);
}
@Override
public void onBindViewHolder(viewHolder holder, int position) {
ChatroomLists chatroomLists = ob1.get(position);
Iterator<ChatroomLists> iter = ob1.iterator();
String id = chatroomLists.getId();
String time = chatroomLists.getTimestamp();
Handler h = new Handler();
FirebaseDatabase.getInstance().getReference().child("unread").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("chatrooms").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try{
if (dataSnapshot.hasChild(id)){
holder.unreadbadge.setVisibility(View.VISIBLE);
}else {
holder.unreadbadge.setVisibility(View.GONE);
}
}catch (Exception e){
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
try{
long mtime = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis())-TimeUnit.MILLISECONDS.toMinutes(Long.parseLong(time));
if (mtime<60){
holder.timestamp.setText(String.valueOf(mtime)+ " mins");
if (mtime==1)
holder.timestamp.setText(1+ " min");
else if (mtime<1){
holder.timestamp.setText("Just Now");
}
else
holder.timestamp.setText(mtime+ " mins");
}
else if (mtime>60 && mtime<1440){
if (mtime/60==1)
holder.timestamp.setText(String.valueOf(mtime/60)+ " hour");
else
holder.timestamp.setText(String.valueOf(mtime/60)+ " hours");
}
else if (mtime>=1440&&mtime<10080){
if (mtime/1440==1)
holder.timestamp.setText(String.valueOf(mtime/1440)+ " day");
else
holder.timestamp.setText(String.valueOf(mtime/1440)+ " days");
}else if(mtime>10080&&mtime<3679200){
if (mtime/10080==1)
holder.timestamp.setText(String.valueOf(mtime/10080)+ " week");
else
holder.timestamp.setText(String.valueOf(mtime/10080)+ " weeks");
}else if(mtime>3679200){
if (mtime/3679200==1)
holder.timestamp.setText(String.valueOf(mtime/3679200)+ " year");
else
holder.timestamp.setText(String.valueOf(mtime/3679200)+ " years");
}
}catch (Exception e){
}
h.postDelayed(this,60000);
}
},60000);
FirebaseDatabase.getInstance().getReferenceFromUrl("https://droidchatz.firebaseio.com/groupchat/" + id + "/chat").limitToLast(1).addValueEventListener(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
for (com.google.firebase.database.DataSnapshot d: dataSnapshot.getChildren()){
if (dataSnapshot.child(d.getKey()).child("type").getValue(String.class).equals("text"))
holder.user_message.setText (dataSnapshot.child(d.getKey()).child("message").getValue(String.class));
else if(dataSnapshot.child(d.getKey()).child("type").getValue(String.class).equals("video"))
holder.user_message.setText ("New Video Message");
else if(dataSnapshot.child(d.getKey()).child("type").getValue(String.class).equals("image"))
holder.user_message.setText ("New Image Message");
else if(dataSnapshot.child(d.getKey()).child("type").getValue(String.class).equals("gif"))
holder.user_message.setText ("New GIF Message");
else if(dataSnapshot.child(d.getKey()).child("type").getValue(String.class).equals("file"))
holder.user_message.setText ("New Document");
else {
holder.user_message.setText (dataSnapshot.child(d.getKey()).child("message").getValue(String.class));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
FirebaseDatabase.getInstance().getReferenceFromUrl("https://droidchatz.firebaseio.com/groups/"+id+"/image").addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
try{
Glide
.with(c)
.load(dataSnapshot.getValue(String.class))
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.dontAnimate()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap arg0, GlideAnimation<? super Bitmap> arg1) {
holder.user_img.setImageBitmap(arg0);
}});
}catch (Exception e){
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
holder.user_name.setText (id);
holder.itemView.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (c, ChatRoom.class);
intent.putExtra("username",username);
intent.putExtra ("group_name", id);
c.startActivity (intent);
}
});
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return ob1.size();
}
}
那麼我做錯了什麼?
我猜 - chnaging「ob1.clear() ;」到'this.ob1.clear();'將解決你的問題, –
試過但仍然沒有成功... @SanjeetAjnabee – Bucky