recyclerview onscrolllistener not working when setNestedScrollingEnabled to false

匿名 (未验证) 提交于 2019-12-03 03:08:02

问题:

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

回答1:

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 } 


回答2:

Step 1 : Create EndlessRecyclerOnScrollListener

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {      public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();      // use your LayoutManager instead     private LinearLayoutManager llm;      public EndlessRecyclerOnScrollListener(LinearLayoutManager sglm) {         this.llm = llm;     }      @Override     public void onScrolled(RecyclerView recyclerView, int dx, int dy) {         super.onScrolled(recyclerView, dx, dy);          if (!recyclerView.canScrollVertically(1)) {             onScrolledToEnd();         }     }      public abstract void onScrolledToEnd(); } 

Step 2: Apply scroll listener to recycler view.

recyclerview.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {             @Override             public void onScrolledToEnd() {                 Log.e("Position", "Last item reached");                 if (loadMore == true) {                     // put your Load more code                     // add 10 by 10 to tempList then notify changing in data                 }             }         }); 


回答3:

You said in a comment to your question "
it is under NestedScrollView which is under coordinator layout, if i remove this, Toolbar is not scrolling up". This is a mistake.

I have found that you cannot have it both ways, the CoordinatorLayout behaviour breaks when you have a RecyclerView inside a NestedScrollView to which you've added the behaviour. You need to use one or the other.

When you have a RecyclerView inside a NestedScrollView it will work as long as you set RecyclerView.setNestedScrollingEnabled(false), but as you found out this means that the OnScrollListener is not called.

The only way for all components to work correctly is to remove the NestedScrollView, make sure you do not set nesting scroll to false and work from there. Otherwise the RecyclerView.OnScrollListener events will not fire correctly.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!