代码:
FirebaseRecyclerOptions<MessagesHelper> options =
new FirebaseRecyclerOptions.Builder<MessagesHelper>()
.setQuery(mFetchingMessages, MessagesHelper.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<MessagesHelper, Chat.MessagesViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final Chat.MessagesViewHolder holder, final int position, @NonNull final MessagesHelper model) {
holder.setMessage(model.getMessage());
final String userId = getRef(position).getKey();
final DatabaseReference mTimeReference = FirebaseDatabase.getInstance().getReference().child("Messages").child(MessageSenderId).child(MessageRecieverId);
Query messageQuery = mTimeReference.limitToLast(10);
messageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
final String from_user = model.getFrom();
final boolean Seen = model.isSeen();
MessagesHelper message = dataSnapshot.getValue(MessagesHelper.class);
itemPos++;
if(itemPos == 1){
String messageKey = dataSnapshot.getKey();
mLastKey = messageKey;
mPrevKey = messageKey;
}
if (from_user != null && from_user.equals(MessageSenderId)) {
holder.mDisplayMessage.setBackgroundColor(Color.BLUE);
holder.mDisplayMessage.setBackgroundColor(Color.WHITE);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mDisplayMessage.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.mDisplayMessage.setLayoutParams(params);
mTimeReference.keepSynced(true);
if (Seen == true) {
holder.mDisplayMessage.setBackgroundColor(Color.CYAN);
mTimeReference.keepSynced(true);
} else {
holder.mDisplayMessage.setBackgroundColor(Color.BLUE);
mTimeReference.keepSynced(true);
}
} else {
holder.mDisplayMessage.setBackgroundColor(Color.GREEN);
holder.mDisplayMessage.setTextColor(data);
mTimeReference.keepSynced(true);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mDisplayMessage.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.mDisplayMessage.setLayoutParams(params);
}
messagesList.add(message);
messageList.scrollToPosition(messagesList.size() - 1);
}
@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) {
}
});
}
@Override
public Chat.MessagesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_activity_chat, parent, false);
return new Chat.MessagesViewHolder(view);
}
};
messageList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
它获取的是完整数据,而不是10个,我不明白为什么。另外我在各处都使用了keepSenced,因为每次重新启动活动时,整个事情都会再次执行...就像
每次我重新启动活动时都会发生这种情况...那么如何使活动仅重新加载10个最新活动而不是整个数据
答案 0 :(得分:0)
那么我如何才能使活动仅重新加载10个最新活动而不是整个数据?
由于将FirebaseRecyclerOptions
对象传递给适配器,而该对象包含无限制的查询,因此,您收到的消息不仅是10条,而是所有消息。
DatabaseReference mFetchingMessages = FirebaseDatabase.getInstance().getReference()
.child("Messages")
.child(MessageSenderId)
.child(MessageRecieverId);
要解决此问题,您需要像这样添加.limitToLast(10)
调用:
DatabaseReference mFetchingMessages = FirebaseDatabase.getInstance().getReference()
.child("Messages")
.child(MessageSenderId)
.child(MessageRecieverId);
.limitToLast(10)