gpt4 book ai didi

java - 更改 Firebase Recycler Adapter 中的项目位置

转载 作者:搜寻专家 更新时间:2023-11-01 09:20:46 24 4
gpt4 key购买 nike

我在我的应用程序中使用 Firebase Recycler Adapter 来显示我的数据库中的项目,但我想要一种情况,即较新的项目将位于 fragment 的顶部,所以我尝试使用 orderByChild()像这样:

查询 conversationQuery = mConvDatabase.orderByChild("timestamp");

但这并没有解决问题。

我真正想做的是在用户收到新消息时更新聊天 fragment ,以便将新对话移动到顶部,就像所有其他 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();
}

有没有一种方法可以操纵发送到数据库的时间戳,以便以后可以以适当的方式检索它?或者我可以在加载数据时在我的代码中本地更改排列吗?

最佳答案

你可以这样做。

        //Display
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
firebaseRecyclerAdapter.notifyDataSetChanged();

关于java - 更改 Firebase Recycler Adapter 中的项目位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55879882/

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