ViewPager检测当用户试图刷出界 [英] ViewPager detect when user is trying to swipe out of bounds
本文介绍了ViewPager检测当用户试图刷出界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的包括6页的ViewPager显示一些数据。我希望能够当用户在位置0,并试图向右滑动(向后),或5位,并试图刷卡向左(向前)来调用一个方法,即使这些没有更多的网页存在方向。有什么办法,我可以听这些情况?
I am using the ViewPager consisting of 6 pages to display some data. I want to be able to call a method when the user is at position 0 and tries to swipe to the right (backwards), or at position 5 and tries to swipe to the left (forward), even though no more pages exist for these directions. Is there any way I can listen for these scenarios?
推荐答案
扩展ViewPager并覆盖 onInterceptTouchEvent()
是这样的:
Extend ViewPager and override onInterceptTouchEvent()
like this:
public class CustomViewPager extends ViewPager {
float mStartDragX;
OnSwipeOutListener mListener;
public void setOnSwipeOutListener(OnSwipeOutListener listener) {
mListener = listener;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
float x = ev.getX();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mStartDragX = x;
break;
case MotionEvent.ACTION_MOVE:
if (mStartDragX < x && getCurrentItem() == 0) {
mListener.onSwipeOutAtStart();
} else if (mStartDragX > x && getCurrentItem() == getAdapter().getCount() - 1) {
mListener.onSwipeOutAtEnd();
}
break;
}
return super.onInterceptTouchEvent(ev);
}
public interface OnSwipeOutListener {
public void onSwipeOutAtStart();
public void onSwipeOutAtEnd();
}
}
这篇关于ViewPager检测当用户试图刷出界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文