我有一个带有SnapHelper的RecyclerView,可以像ViewPager一样使用RecyclerView来逐一浏览项目(如PagerView)。
如何检测用户何时滑动到下一个RecyclerView项目?
我尝试使用onScrollStateChanged和onScrolled进行addOnScrollListener,但是当用户从一个项目滚动到下一个项目时,它们调用了很多次。
请问我如何能检测到用户何时滑动到下一个RecyclerView项目。
谢谢
SnapHelper类:
public class SnapHelperOneByOne extends LinearSnapHelper {
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager
layoutManager, int velocityX, int velocityY) {
if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
return RecyclerView.NO_POSITION;
}
final View currentView = findSnapView(layoutManager);
if (currentView == null) {
return RecyclerView.NO_POSITION;
}
LinearLayoutManager myLayoutManager = (LinearLayoutManager)
layoutManager;
int position1 = myLayoutManager.findFirstVisibleItemPosition();
int position2 = myLayoutManager.findLastVisibleItemPosition();
int currentPosition = layoutManager.getPosition(currentView);
if (velocityX > 400) {
currentPosition = position2;
} else if (velocityX < 400) {
currentPosition = position1;
}
if (currentPosition == RecyclerView.NO_POSITION) {
return RecyclerView.NO_POSITION;
}
return currentPosition;
}
}