Recyclerview Onscrolllistener Not Working When Setnestedscrollingenabled To False
Solution 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
}
Solution 2:
Do add setOnScrollChangeListner to your NestedScrollView
nestedScrollview.setOnScrollChangeListener(newNestedScrollView.OnScrollChangeListener() {
@OverridepublicvoidonScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
if(loading)
onClick();
loading=false;
}
}
});
after loading data from server set boolean loading=true.
Solution 3:
If you are recyclerView is embedded in any of the NestedScrollView, then you are supposed to attach the onScrollListener to NestedScrollView.
This will work!
if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
finalGridLayoutManagergridLayoutManager= (GridLayoutManager) recyclerView.getLayoutManager();
nestedScrollView.setOnScrollChangeListener(newNestedScrollView.OnScrollChangeListener() {
@OverridepublicvoidonScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
totalItemCount = gridLayoutManager.getItemCount();
lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
if (!loading
&& totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached// Do somethingif (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
}
});
}
Solution 4:
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.
Solution 5:
Step 1 : Create EndlessRecyclerOnScrollListener
publicabstractclassEndlessRecyclerOnScrollListenerextendsRecyclerView.OnScrollListener {
publicstaticStringTAG= EndlessRecyclerOnScrollListener.class.getSimpleName();
// use your LayoutManager insteadprivate LinearLayoutManager llm;
publicEndlessRecyclerOnScrollListener(LinearLayoutManager sglm) {
this.llm = llm;
}
@OverridepublicvoidonScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!recyclerView.canScrollVertically(1)) {
onScrolledToEnd();
}
}
publicabstractvoidonScrolledToEnd();
}
Step 2: Apply scroll listener to recycler view.
recyclerview.addOnScrollListener(newEndlessRecyclerOnScrollListener(mLayoutManager) {
@OverridepublicvoidonScrolledToEnd() {
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
}
}
});
Post a Comment for "Recyclerview Onscrolllistener Not Working When Setnestedscrollingenabled To False"