如何为许多图像视图执行一个OnTouchListener

时间:2015-04-07 08:03:19

标签: android

我正在做一个项目,我需要使用一个OnTouchListene处理大约25个ImageViews,我已经完成了OnClickListener,但我在OnTouchListener中遇到了问题。

以下是OnTouchListener代码:

请阅读代码中的评论,了解我需要帮助的更多详情。

public View.OnTouchListener imgSwipe = new OnSwipeTouchListener(){

//I have added (View v) to handle the touched ImageView, But not working.

    public boolean onSwipeTop(View v) {
        if(v == m1)){
           //Do something
        }
        else if(v == m2){
     //Do something
        }

     // Here to add the remains ImageView such as m3, m4 ... m25.

        return true;
    }

    public boolean onSwipeRight() {
        Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
        return true;
    }

    public boolean onSwipeLeft() {
        Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
        return true;
    }

    public boolean onSwipeBottom() {
        Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show();
        return true;
    }

};

OnSwipeTouchListener()类代码是:

公共类OnSwipeTouchListener实现了View.OnTouchListener {

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 GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;


    @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) {
                        result = onSwipeRight();
                    } else {
                        result = onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        result = onSwipeBottom();
                    } else {
                        result = onSwipeTop();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public boolean onSwipeRight() {
    return false;
}

public boolean onSwipeLeft() {
    return false;
}

public boolean onSwipeTop() {
    return false;
}

public boolean onSwipeBottom() {
    return false;
}

}

关于如何在一个OnTouchListener中处理所有ImageViews触摸的任何想法?

感谢。

0 个答案:

没有答案