0

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

Hamdy
  • 25
  • 8
  • *documentation that says the paging adapter is not appropriate for a chat application* doesn't make any sense. What if you only want to display the last 10 messages? Of course pagination can make sense for any kind of lengthy list. I think clarification is needed – Jay Feb 19 '21 at 18:13
  • @Jay the problem in using paging adapter that when adding new message , it is not displayed unless i restart activity so i waste real time feature – Hamdy Feb 19 '21 at 19:05
  • First please update your questions and provide more code some vars not so clearly used so we may can help. Secondly swipe refresh layout in this case not a good UX so u may try onScroll on your Recycleview adapter – Hossam Ali Feb 20 '21 at 07:43
  • @HossamAli i've tried onScroll but it keep scroll to bottom ,if you have some code to implement onscroll and add more messages while scrolling to top to get more messages please provide it, i've tried a lot but without any result – Hamdy Feb 20 '21 at 08:00

2 Answers2

0

I personally dont use the adapters when utilising Pagination, I simply send the current List size to my service layer, when the user reaches the bottom of a ListView. Seems like a less intensive and more efficient process, then you just need to implement SKIP and TAKE to the db

private boolean reachedEndOfList(int position)
    {
        return position == this.getCount() - 1;
    }
Dean Beckerton
  • 196
  • 1
  • 9
  • can you give more details and some code about your idea and how to apply this to firebase? @Dean Beckerton – Hamdy Feb 18 '21 at 18:31
0

u can try this method

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

}