因此,我已经实现了一些加载消息的方式,当用户向上滚动时,它可以工作,但它总是向上滚动,如果你稍微向上滚动,它会加载比预期更多的消息。
如果有人能帮我,那就太好了。
这就是我尝试过的:
这是我在活动的oncreate()方法中执行的第一件事:
mRootRef.keepSynced(true);
mChildEventListener = mRootRef.child("Messages").child(mMessageSenderID).child(mMessageReceiverID).limitToLast((int) limitation)
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s)
{
Messages messages = dataSnapshot.getValue(Messages.class);
mMessagesList.add(messages);
mMessageAdapter.notifyDataSetChanged();
mLinearLayoutManager.setStackFromEnd(true);
if(time_fired > 2 && !mSentMessage){
mNewMessage.setVisibility(View.VISIBLE);
mFab.show();
}
if(!mUserMessagesList.canScrollVertically(1)){
mNewMessage.setVisibility(View.GONE);
mFab.hide();
}
if(mSentMessage){
mSentMessage = false;
}
time_fired++;
}
@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)
{
}
});
之后,在相同的方法(oncreate)中,我使用此方法检查滚动:
//When user scrolls up new data gets added
mUserMessagesList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(!recyclerView.canScrollVertically(-1)){
if(!limitReached) {
limitation += 12;
DatabaseReference tempRef = FirebaseDatabase.getInstance().getReference().child("Messages");
tempRef.child(mMessageSenderID).child(mMessageReceiverID)
.limitToLast((int) limitation).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (limitation > snapshot.getChildrenCount()) {
limitation = num_of_messages;
limitReached = true;
DatabaseReference temp = FirebaseDatabase.getInstance().getReference().child("Messages");
temp.child(mMessageSenderID).child(mMessageReceiverID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
mMessagesList.clear();
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
Messages messages = snapshot1.getValue(Messages.class);
mMessagesList.add(messages);
}
mMessageAdapter.notifyItemRangeChanged(0, mMessageAdapter.getItemCount());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
mMessagesList.clear();
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
Messages messages = snapshot1.getValue(Messages.class);
mMessagesList.add(messages);
//mMessageAdapter.notifyDataSetChanged(); BEFORE
}
mMessageAdapter.notifyItemRangeChanged(0, mMessageAdapter.getItemCount());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy < -20){
mFab.show();
}
}
});
我有一些想法,比如保留以前的邮件列表,而从不清除它。但是下面的代码有极限,我不知道firebase的多少,也不知道如何在当前加载的消息之上只加载10条消息。。如果有人能帮助我,我将不胜感激。
我在这里读过一些类似的问题,但它们在我的情况下不起作用。
暂无答案!
目前还没有任何答案,快来回答吧!