[英]recyclerview onScrollListener not working correctly
I've got a recyclerView
within a pull to refresh layout
. 我在pull to refresh layout
有一个recyclerView
。 I've added an onScrollListener
to keep track of the total vertical movement by adding/subtracting the delta Y (dy)
to a variable named totalScrolled
. 我添加了一个onScrollListener
,通过将delta Y (dy)
加到/减去名为totalScrolled
的变量来跟踪总垂直移动。 And if the recyclerview
is scrolled fully to the top totalScrolled
should be 0 and a view should be visible. 如果将recyclerview
完全滚动到顶部,则totalScrolled
应该为0,并且视图应该是可见的。
Problem only is that somehow there is a bug and the scroll listener
is not accurate in telling me how much the list has scrolled
. 问题只是在某种程度上存在一个错误并且scroll listener
器不能准确地告诉我列表scrolled
了多少。 After scrolling down and back up again through my many items list somehow the totalScrolled
doesn't return to 0. 向下滚动并通过我的多个项目列表再次备份后, totalScrolled
不会返回0。
Anyone ran into this issue as well and knows how to solve this? 任何人都遇到了这个问题并知道如何解决这个问题?
Instead of using the dy from the scrollListener's onScrolled callback to keep track of the total vertical scroll which is inaccurate I use a function from the recyclerView itself - RecyclerView.computeVerticalScrollOffset() - which accurately keeps track of how many pixels the recyclerView has scrolled. 而不是使用scrollListener的onScrolled回调中的dy来跟踪不准确的总垂直滚动,我使用了来自recyclerView本身的函数--RecyclerView.computeVerticalScrollOffset() - 它准确地跟踪了recyclerView滚动了多少像素。
My code looks something like this: 我的代码看起来像这样:
home_screen_recycler.addOnScrollListener(object: RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val scrollOffset = home_screen_recycler.computeVerticalScrollOffset()
presenter.onListScrolled(scrollOffset)
}
})
In my project I use this logic. 在我的项目中,我使用这个逻辑。
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (arrayList.size() == 0)
return;
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
int itemPosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
if (itemPosition == (arrayList.size() - 1)) {
// here you can fetch new data from server.
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.