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);
}
}
});
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 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);
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
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