2

I have been searching for a while but can't find a solution to detect end of scroll of recycler view with grid layout manager. Using code below actually work, but it is for linear layout manager not for grid layout manager.

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
                final int treeshold = 0;
                try {
                    if (scrollState == RecyclerView.SCROLL_STATE_IDLE) {
                        if (((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition() >= yourData.size()
                                - 1 - treeshold) {
                            //your load more logic
                        }
                    }
                } catch (Exception e) {
                }
            }

The idea is that i want to implement load more function to my application, so i need to detect end of scroll.

Edit : maybe the problem not the grid view itself. I used com.tonicartos.superslim library to get sticky header view. I wonder that it might be the problem

HendraWD
  • 2,984
  • 2
  • 31
  • 45

3 Answers3

2

Add a scroll listener to your RecyclerView like below:

int pastVisiblesItems, visibleItemCount, totalItemCount;  
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrolled(RecyclerView recyclerView,
                                               int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);
                            visibleItemCount = layoutManager.getChildCount();
                            totalItemCount = layoutManager.getItemCount();
                            pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();

                           if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                                //bottom of recyclerview
                          }
                        }
    });

layoutmanager is your GridLayoutManager

Malek Hijazi
  • 4,112
  • 1
  • 26
  • 31
2
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
    public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();

    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of data to load.
    private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
    int firstVisibleItem, visibleItemCount, totalItemCount;

    private int currentPage = 1;

    private RecyclerView.LayoutManager mLayoutManager;
    private boolean isUseLinearLayoutManager;
    private boolean isUseGridLayoutManager;

    public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
        this.mLayoutManager = linearLayoutManager;
        isUseLinearLayoutManager = true;

    }

    public EndlessRecyclerOnScrollListener(GridLayoutManager gridLayoutManager) {
        this.mLayoutManager = gridLayoutManager;
        isUseGridLayoutManager = true;

    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = mLayoutManager.getItemCount();


        if(isUseLinearLayoutManager && mLayoutManager instanceof LinearLayoutManager){
            firstVisibleItem = ((LinearLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
        }

        if(isUseGridLayoutManager && mLayoutManager instanceof GridLayoutManager){
            firstVisibleItem = ((GridLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
        }

        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount)
                <= (firstVisibleItem + visibleThreshold)) {
            // End has been reached

            // Do something
            currentPage++;

            onLoadMore(currentPage);

            loading = true;
        }
    }

    public abstract void onLoadMore(int currentPage);
S.Prapagorn
  • 621
  • 6
  • 14
0

I just sent an email directly to Mr. Artos about this problem and got a reply: "It likely won't work properly until the next version is finished. I wrote about it on my blog at http://www.tonicartos.com/2015/12/superslim-milestone-1_23.html?m=1"

HendraWD
  • 2,984
  • 2
  • 31
  • 45