OnClick会在下面抑制OnTouch

时间:2012-10-11 16:10:51

标签: android ontouchlistener onclicklistener

我有一个可点击的多个ImageViews的应用。所有这些图像都在LinearLayout内。

我实施了OnTouchListener并将其设置为LinearLayout。当我滑过它时,我的OnTouchListener会调用一个函数。

如果我开始滑动没有ImageViews OnClickListener OnTouchListener工作的OnClickListener,那么当我开始扫描一张{{1}的图片时}},那么OnTouch函数不起作用({1}}的{​​{1}}也不正常。

我想知道是否有办法让我的OnClick ImageView将动作传递到下面的视图,如果用户滑动而不是点击,如果用户只是点击则不传递

提前致谢

1 个答案:

答案 0 :(得分:3)

我设法做到了,我找到的唯一方法就是使用SimpleOnGestureListener。如果有人遇到同样的问题,我会复制粘贴代码。

创建一个这样的类:

public abstract class MyGestureDetector extends SimpleOnGestureListener {

private static final int SWIPE_MIN_DISTANCE = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 50;
private GestureDetector gestureDetector;
private View.OnTouchListener gestureListener;
private Activity activity;

public MyGestureDetector(Activity activity) {
    this.activity = activity;
    gestureDetector = new GestureDetector(this);
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };
}

public abstract void onRightToLeftSwipe();

public abstract void onLeftToRightSwipe();

public abstract void onTopToBottomSwipe();

public abstract void onBottomToTopSwipe();

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velX,
        float velY) {
    DisplayMetrics dm = activity.getResources().getDisplayMetrics();
    int REL_SWIPE_MIN_DISTANCE = (int) (SWIPE_MIN_DISTANCE * dm.densityDpi / 160.0f);
    int REL_SWIPE_THRESHOLD_VELOCITY = (int) (SWIPE_THRESHOLD_VELOCITY
            * dm.densityDpi / 160.0f);
    float deltaX = e1.getX() - e2.getX();
    float deltaY = e1.getY() - e2.getY();
    try {
        // swipe horizontal?
        if ((Math.abs(deltaX) > REL_SWIPE_MIN_DISTANCE)
                && (Math.abs(velX) > REL_SWIPE_THRESHOLD_VELOCITY)
                && (Math.abs(deltaX) > Math.abs(deltaY))) {
            // left or right
            if (deltaX < 0) {
                this.onLeftToRightSwipe();
                return true;
            }
            if (deltaX > 0) {
                this.onRightToLeftSwipe();
                return true;
            }
        } else if ((Math.abs(deltaY) > REL_SWIPE_MIN_DISTANCE)
                && (Math.abs(velY) > REL_SWIPE_THRESHOLD_VELOCITY)) { // swipe
            // vertical
            // top or down
            if (deltaY < 0) {
                this.onTopToBottomSwipe();
                return true;
            }
            if (deltaY > 0) {
                this.onBottomToTopSwipe();
                return true;
            }
        } else {
            return false; // We don't consume the event
        }

    } catch (Exception e) {
        // nothing
    }
    return false;
}

public View.OnTouchListener getGestureListener() {
    return gestureListener;
}

public void setGestureListener(View.OnTouchListener gestureListener) {
    this.gestureListener = gestureListener;
}
}

然后你必须在你的Activity中执行此操作:

public void onCreate(Bundle savedInstanceState) {
     // Initializate your variables here
     // Using Gestures
     MyGestureDetector gestos = new MyGestureDetector(this) {

        @Override
        public void onTopToBottomSwipe() {
            // Do whatever

        }

        @Override
        public void onRightToLeftSwipe() {
            // Do whatever
        }

        @Override
        public void onLeftToRightSwipe() {
            // Do whatever
        }

        @Override
        public void onBottomToTopSwipe() {
            // Do whatever

        }
    };

最后,但并非最不重要的是,你必须将TouchListener放到你想要在其中滑动的布局中,并且每个视图都是可以点击的布局

layoutSwipe = (LinearLayout) findViewById(R.id.layoutSwipe);
layoutSwipe.setOnTouchListener(gestos.getGestureListener());
viewInsidells.setOnTouchListener(gestos.getGestureListener());
view2Insidells.setOnTouchListener(gestos.getGestureListener());

有了这个,你可以在你的布局中制作你的视图,如果你点击它只会执行你点击的视图的点击动作,如果你滑动它只会根据你的动作执行滑动动作。

我花了4个小时试图解决这个问题,希望我能节省一些时间到下一个。