Recyclerview onscrolllistener not working when setNestedScrollingEnabled to false

Adnan Ali picture Adnan Ali · Jul 4, 2016 · Viewed 17.8k times · Source

I want to implement pagination with recyclerView, for this I add addOnScrollListener to the recyclerView but I am having trouble with RecyclerView.OnScrollListener not working when I set rvGridExplore.setNestedScrollingEnabled(false);

But when I remove rvGridExplore.setNestedScrollingEnabled(false); it is working fine, I don't know how to handle this.

Here is code:

rvGridExplore = (RecyclerView) view.findViewById(R.id.rvGridExplore);
        final GridLayoutManager glm = new GridLayoutManager(context,2);
       // rvGridExplore.setNestedScrollingEnabled(false);
        rvGridExplore.setLayoutManager(glm);

       // final int visibleItemCount,totalCount,pastVisibleItems;
        rvGridExplore.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.v("scrollll","state changed");
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                    int totalCount = glm.getItemCount();
                    int visibleItemCount = glm.getChildCount();
                    int pastVisibleItems = glm.findFirstVisibleItemPosition();
                    if (loading) {
                        if ((visibleItemCount + pastVisibleItems) >= totalCount) {
                            Log.v("scroll","scrolled"+pastVisibleItems);
                        }
                    }

                }
            }
        });

Answer

m.n Aswin picture m.n Aswin · Mar 28, 2017

This question may be old, but to help others who stumbled upon this problem, i would like to share what i did. I had to implement onScroll Listener to recyclerview to load data from server and to make some UI changes. And also needed swipeRefresh Layout for refreshing data.

This was my xml file structure,

-RelativeLayout

 -SwipeRefreshLayout

  -NestedScrollView

   -LinearLayout(Vertical)

    -Multiple views required

After this, to detect up and down scrolling i implemented setOnScrollListener to the NestedScrollView.

Normal usage of SwipeRefreshLayout to refresh data.

And to load more data i implemented the logic inside onScrollListener of NestedScrollingView.

if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()))  {

    // Load More Data
}