问题
I would to implement a ViewPager swipe with two fingers. I tried to implement a sublcass of ViewPager overriding the onTouchEvent and passing the method to superclass only if the touch is made by 2 fingers. But there is a problem: the swipe animation also works with 1 finger! I think I have to override some other method...
This is my ViewPager class:
public class MyViewPager extends ViewPager{
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context,AttributeSet attributeSet) {
super(context,attributeSet);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int n = ev.getPointerCount(); //number of fingers
if (n == 2)
return super.onTouchEvent(ev);
else return false;
}
}
回答1:
Overriding onInterceptTouchEvent()
should do the trick. According to the comments in the ViewPager sources, it's there where the decision is made whether scrolling should start or not.
回答2:
im writing this code and its working for two finger .
boolean freeTwo = false;
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
int counter = ev.getPointerCount() ;
if(counter == 1 && (action== MotionEvent.ACTION_DOWN || action== MotionEvent.ACTION_MOVE) && !freeTwo)
{
if(action == MotionEvent.ACTION_MOVE)
freeTwo = true;
return super.onTouchEvent(ev);
}
if(counter == 2 && (action== MotionEvent.ACTION_POINTER_DOWN|| action== MotionEvent.ACTION_MOVE || action== MotionEvent.ACTION_POINTER_UP) && freeTwo)
{
if(action== MotionEvent.ACTION_POINTER_UP)
freeTwo = false;
return super.onTouchEvent(ev);
}
ev.setAction(MotionEvent.ACTION_UP);
return super.onTouchEvent(ev);
}
来源:https://stackoverflow.com/questions/23806602/viewpager-swipe-with-2-fingers