pallavi richhariya
pallavi richhariya

Reputation: 413

recyclerview and pagination scrolling issue

I am using recycler view pagination to show my data. I am loading one page api data by default, for next page i am loading data on the scroll basis of the recycler view. But scroll is not getting called as i can see only 1 page. If i put the condition in on scroll for increasing page no. then on scroll method its not stopping & its calling api till last page in background. Meaning on scroll is calling again and again that I don't want. I want to scroll on basis of current visible items.

  live_grid.addOnScrollListener(new PaginationScrollListener(gridLayoutManager) {

      @Override
      protected void loadMoreItems() {
      // TODO: 07/11/16 check if totalPage > 1 before triggering next load
      Log.d(TAG, "live_grid loadMoreItems.......");

            int visibleItemCount = gridLayoutManager.getChildCount();
            int totalItemCount = gridLayoutManager.getItemCount();
            int firstVisibleItemPosition = gridLayoutManager.findFirstVisibleItemPosition();

            Log.d(TAG, "visibleItemCount: "+ visibleItemCount);
            Log.d(TAG, "totalItemCount: "+totalItemCount);
            Log.d(TAG, "firstVisibleItemPosition: "+firstVisibleItemPosition);
            Log.d(TAG, "currentPage: "+currentPage);
            Log.d(TAG, "PAGE_SIZE: "+PAGE_SIZE);

            if (!isLoading && !isLastPage) {      
              // if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount  && firstVisibleItemPosition >= 0) {

                 if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0 && currentPage < (PAGE_SIZE)) {  
                 //&& totalItemCount >= PAGE_SIZE
                 firstVisibleItemPosition = visibleItemCount;
                 loadMoreItems1();

                  }
             }
                  // presenter.setIsLoading(true);
                  //  presenter.setCurrentPage(presenter.getCurrentPage() + 1);
                 //  presenter.loadNextPlaylist();

      }

Upvotes: 2

Views: 2433

Answers (1)

user4571931
user4571931

Reputation:

Try this code..

    protected int pastVisibleItems, visibleItemCount, totalItemCount; // define class level..
protected void addScrollListener() {
    rvData.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (dy > 0) //check for scroll down
            {
                visibleItemCount = linearLayoutManager.getChildCount();
                totalItemCount = linearLayoutManager.getItemCount();
                pastVisibleItems = linearLayoutManager.findFirstVisibleItemPosition();

                    if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                            pageNumber++; // increase your page
                            getMessage(); //  call api with new Page.
                    }
            }
        }
    });
}

this method used after recyclerview define and bind adapter.

GridLayoutManger..

 RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        int totalItrem = mLlManager.getItemCount();
        int lastViisble = mLlManager.findLastVisibleItemPosition();
        Log.d("item", "total item" + totalItrem);
        Log.d("visible", "last " + lastViisble);
        if (!isLoading && totalItrem == lastViisble + 1 && total_records != mEventList.size()) {
            Log.e("msg", "reached end");
            //Toast.makeText(mrecyclerview.getContext(), "reached at end", Toast.LENGTH_SHORT).show();
            isLoading = true;
            mEventList.add(null);
            mCurrentPage++;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    getData(mCurrentPage);
                }
            }, 2000);
        }
    }
};

Upvotes: 2

Related Questions