ViewPager刷卡用2个手指手指、ViewPager

2023-09-05 23:40:46 作者:痴情负。

我想实现一个ViewPager刷卡用两个手指。 我试图执行的ViewPager覆盖的onTouchEvent并通过该方法仅如果触摸由2个手指作出超类sublcass。 但有一个问题:刷卡动画还与1手指! 我想我必须重写一些其他的方法......

这是我ViewPager类:

 公共类MyViewPager扩展ViewPager {
公共MyViewPager(上下文的背景下){
    超(上下文);
}

公共MyViewPager(上下文的背景下,AttributeSet中的AttributeSet){
    超(背景下,AttributeSet中的);
}

@覆盖
公共布尔的onTouchEvent(MotionEvent EV){
    INT N = ev.getPointerCount(); //手指的数量
    如果(N == 2)
        返回super.onTouchEvent(EV);
    否则返回false;
}
}
 

解决方案

重写 onInterceptTouchEvent()应该做的伎俩。根据在 ViewPager 来源的,它的存在在那里作出决定是否滚动应该启动与否。

为你的应用加速 安卓优化指南

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;
}
}

解决方案

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.