我在我的应用程序中使用Firebase Recycler Adapter来显示数据库中的项目,但是我希望出现这样一种情况,即较新的项目将位于片段的顶部,因此我尝试使用orderByChild()
,如下所示:>
Query conversationQuery = mConvDatabase.orderByChild("timestamp");
但这并不能解决问题。
我真正想做的是,每当用户收到一条新消息时,就更新聊天片段,以将新对话移到顶部,就像其他所有Messenger应用一样。
这是我的ChatFragment
@Override
public void onStart() {
super.onStart();
Query conversationQuery = mConvDatabase.orderByChild("timestamp");
FirebaseRecyclerAdapter<Conv, ConvViewHolder> firebaseConvAdapter = new FirebaseRecyclerAdapter<Conv, ConvViewHolder>(
Conv.class,
R.layout.users_layout,
ConvViewHolder.class,
conversationQuery
) {
@Override
protected void populateViewHolder(final ConvViewHolder convViewHolder, final Conv conv, int i) {
final String list_user_id = getRef(i).getKey();
// Query MessageQuery = mMessageDatabase.child(list_user_id);
Query lastMessageQuery = mMessageDatabase.child(list_user_id).limitToLast(1);
lastMessageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String data = dataSnapshot.child("message").getValue().toString();
String type_image = dataSnapshot.child("type").getValue().toString();
boolean seen = Boolean.parseBoolean(dataSnapshot.getKey());
if (type_image.equals("image")){
convViewHolder.setMessage("image",conv.isSeen());
}else {
convViewHolder.setMessage(data, conv.isSeen());
}}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
String userThumb = dataSnapshot.child("thumb_image").getValue().toString();
if(dataSnapshot.hasChild("online")) {
String userOnline = dataSnapshot.child("online").getValue().toString();
convViewHolder.setUserOnline(userOnline, getActivity());
}
mConvList.scrollToPosition(0);
convViewHolder.setName(userName);
convViewHolder.setUserImage(userThumb, getContext());
convViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent chatIntent = new Intent(getContext(), ChatActivity.class);
chatIntent.putExtra("user_id", list_user_id);
chatIntent.putExtra("user_name", userName);
startActivity(chatIntent);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mConvList.setAdapter(firebaseConvAdapter);
firebaseConvAdapter.notifyDataSetChanged();
}
有没有一种方法可以处理发送到数据库的时间戳,以便以后可以以适当的方式进行检索?还是可以在加载数据时在本地代码中更改本地排列?
答案 0 :(得分:2)
您可以执行以下操作。
//Display
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
firebaseRecyclerAdapter.notifyDataSetChanged();