问题
I am sure that there are some posts about it, and I skimmed through them before, but now I cannot find them.
I want to detect the action when the user swipes the viewpager from edge of a page (such as left edge). I want to do some special handling for this kind of swipe, such as showing menu.
Is there any ViewPager built-in(?) support for it? I vaguely remembered it is. Otherwise, I have to implement my own logic to detect those action.
Can anyone point me some info?
回答1:
I think, you should override few methods of the container of your ViewPager. For example if it is LinearLayout
public class Liner extends LinearLayout {
public Liner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
//Remember start points
float mInitX;
float mInitY;
// TouchSlop value
float mSomeValue=20F;
//
boolean mCatched=false;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mInitX=ev.getX();
mInitY=ev.getY();
break;
default:
break;
}
mCatched=mInitX<10 && Math.abs(ev.getX()-mInitX)>mSomeValue;
return mCatched;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(mCatched){
//Do all you want here
}
return true;
}
}
If you combine with TeRRo anrwer (put his detector inside onTouchEvent()) you may have good result. But best way to use DrawerLayout.
Good luck!
回答2:
Simply check for edge mask
public void onTouchEvent(MotionEvent event){
if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() == MotionEvent.EDGE_LEFT) {
}
}
Same logic will work if you put the snippet inside onInterceptTouchEvent(MotionEvent event)
override.
回答3:
With this example you can control all types of gestures in your ViewPager, Now only you need add some condition for handle the edge in onSwipeLeft()
and onSwipeRight()
public class OnSwipeTouchListener implements OnTouchListener {
@SuppressWarnings("deprecation")
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
public boolean onTouch(final View v, final MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
onTouch(e);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
// onTouch(e);
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onTouch(MotionEvent e) {
}
public void onSwipeRight() {
//call this only if your condition was set
}
public void onSwipeLeft() {
//nothing, this means,swipes to left will be ignored
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
Set you custom OnTouchListener in your viewpager object:
viewPager.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
if(your conditionIsMet){
// move your pager view to the right
}
}
});
I hope that this help you.
来源:https://stackoverflow.com/questions/27346030/detect-when-user-swipes-the-viewpager-from-edge-of-a-page