RecyclerView scrollToPosition不会触发scrollListener [英] RecyclerView scrollToPosition not trigger scrollListener
问题描述
我正在使用RecyclerView和ScrollListener:
I'm using RecyclerView, with ScrollListener:
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
super.onScrolled(recyclerView, dx, dy);
// Do my logic
}
});
当我用手指滚动时,滚动侦听器触发得很好.
When I scroll with the finger, the scroll listener triggered fine.
但是当我按比例滚动时,像这样:
But when I scroll progrematically, like that:
mRecyclerView.scrollToPosition(LAST_POSITION);
滚动侦听器未触发.
推荐答案
这是一个已知问题.这是由于RecyclerView不知道LayoutManager如何处理滚动或根本不处理滚动引起的.
This is a known issue. It is caused by the fact that RecyclerView does not know how LayoutManager will handle the scroll or if it will handle it at all.
在下一个版本中,如果在布局之后第一个和/或最后一个子位置发生变化(通常是调用滚动到位置的结果),您将收到对onScrolled
的调用.
In the next release, you'll receive a call to onScrolled
if first and or last child position changes after a layout (which is generally the result of calling scroll to position).
不幸的是,由于RecyclerView并不真正知道滚动了多少布局管理器来处理scrollTo
请求,因此dx和dy将为0.另外,您也可以使用ViewTreeObserver的滚动回调.
Unfortunately, dx and dy will be 0 because RecyclerView does not really know how much layout manager did scroll to handle scrollTo
request. Alternatively, you can also use the the ViewTreeObserver's scroll callback.
这篇关于RecyclerView scrollToPosition不会触发scrollListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!