I'm using PagerSnapHelper in a horizontal RecyclerView to achieve a view pager like behaviour.
final PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
pagerSnapHelper.attachToRecyclerView(recyclerView);
It works great, but I want to be able to get callbacks for when the user changes the page in either direction. So something like, onSwipeLeft / onSwipeRight callbacks.
I tried using findTargetSnapPosition in PagerSnapHelper, but that only gives me the targetIndex and not the current index. I tried something like this, but it doesn't really work all the time.
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
final int targetPos = super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
final View currentView = findSnapView(layoutManager);
final int currentPos = layoutManager.getPosition(currentView);
if (currentPos < targetPos) {
callback.onSwipeRight();
} else if (currentPos > targetPos) {
callback.onSwipeLeft();
}
return targetPos;
}
Is there a better way to achieve this which always works? Thanks!