如何使用Firebase实时数据库在聊天应用程序中创建分页? [英] how to create pagination in chat app using firebase real time database?
问题描述
从文档中得知,分页适配器不适用于聊天应用程序,因此我尝试在swipeTorefreshLayout中进行分页
from documentation that says the paging adapter is not appropriate for a chat application so i tried to make pagination in swipeTorefreshLayout
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mItemPosition=0;
int difference =(int)size-messagesList.size();
if(difference>10){
page_size =10;
}else {
page_size=difference+1;
}
if(difference>0){
fetchMoreMessages();
}else {
swipeRefreshLayout.setRefreshing(false);
}
}
});
我还使用了limitToLast方法来获取fetchMesages方法中的10条最新消息
i used also limitToLast method to fetch the 10 newest messages in fetchMesages method
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
if (snapshot.exists()) {
Messages messages = snapshot.getValue(messages.class)
mItemPosition++;
if(mItemPosition==1){
mLastKey =snapshot.getKey();
mPrevKey = snapshot.getKey();
}
messagesList.add(messages);
adapter.notifyDataSetChanged();
mRecyclerView.smoothScrollToPosition(messagesList.size() - 1);
swipeRefreshLayout.setRefreshing(false);
}
messageQuery = pairRef.orderByKey().limitToLast(10);
messageQuery.addChildEventListener(mChildEventListener);
fetchMoreMessages方法
fetchMoreMessages method
if(moreChildEventListener ==null){
moreChildEventListener = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
if (snapshot.exists()) {
Messages messages = snapshot.getValue(messages.class)
if(!messageKey.equals(mPrevKey)){
messagesList.add(mItemPosition++,messages);
}else {
mPrevKey = mLastKey;
}
}
if(mItemPosition==1) {
mLastKey = snapshot.getKey();
}
adapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
当我添加新消息时,它会在最后一条消息之后的正确位置支付两次,而另一条消息将显示两条消息,这是我之前在limitToLast method()中显示的消息,并在其下面的新消息中.
the problem that when I add a new message it dispay twice one in the right place after last message and the other it will display two messages the message that i show earlier in the limitToLast method() and below it new message
messag10
messag2
messag3
new message
message10
new message
如果我重新开始活动,它将显示为应有的状态(一条消息)这也用于删除我认为这个问题是由于使用limitTolast()我花了很多天来找到解决方案并创建分页,所以该怎么做呢?或者还有另一种添加分页的最佳方法吗?感谢任何帮助
and if i restart activity it will show it as it should be(one message) this also for delete i think this problem because of using limitTolast() i spent a lot of days to find solution and create pagination so how to do this or is there another best way to add pagination ?any help appreciated
推荐答案
u可以尝试此方法
private void myRecyclerUpdater() {
mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { //not important
super.onScrolled(recyclerView, dx, dy);
}
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//try (2) instead of ACTION_DOWN if it didnt work
//and we check for scroll state here 0 means IDLE
if (!recyclerView.canScrollVertically(MotionEvent.ACTION_DOWN) && newState == 0) {
//make your data request
//update your recycler list and notify changes
}
}
});
}
这篇关于如何使用Firebase实时数据库在聊天应用程序中创建分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!