将setNestedScrollingEnabled设置为false时,Recyclerview onscrolllistener无法正常工作 [英] Recyclerview onscrolllistener not working when setNestedScrollingEnabled to false
问题描述
我想用recyclerView
实现分页,为此我将addOnScrollListener
添加到recyclerView
,但是在设置rvGridExplore.setNestedScrollingEnabled(false);
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);
但是当我删除rvGridExplore.setNestedScrollingEnabled(false);
时,它工作正常,
我不知道该如何处理.
But when I remove rvGridExplore.setNestedScrollingEnabled(false);
it is working fine,
I don't know how to handle this.
这是代码:
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);
}
}
}
}
});
推荐答案
这个问题可能很老,但是为了帮助偶然发现此问题的其他人,我想分享一下我所做的事情.我必须对recyclerview
实现onScroll Listener才能从服务器加载数据并进行一些UI更改.并且还需要swipeRefresh Layout来刷新数据.
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.
这是我的xml文件结构,
This was my xml file structure,
-RelativeLayout
-SwipeRefreshLayout
-NestedScrollView
-LinearLayout(Vertical)
-Multiple views required
此后,为了检测上下滚动,我将setOnScrollListener实现到NestedScrollView.
After this, to detect up and down scrolling i implemented setOnScrollListener to the NestedScrollView.
正常使用SwipeRefreshLayout刷新数据.
Normal usage of SwipeRefreshLayout to refresh data.
为了加载更多数据,我在NestedScrollingView的onScrollListener内部实现了逻辑.
And to load more data i implemented the logic inside onScrollListener of NestedScrollingView.
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
// Load More Data
}
这篇关于将setNestedScrollingEnabled设置为false时,Recyclerview onscrolllistener无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!