ViewFlipper不会收到触摸事件

时间:2011-08-17 07:03:24

标签: android viewflipper

我正在开发一个Android项目,我在其中使用ViewFlipper来翻转孩子。为此,我使用了一个定制的鳍状肢。在此我覆盖了onTouchEvent()方法。 鳍状肢包含每个孩子的图像,但是当我尝试翻转时,它不会接收任何触摸事件。当我覆盖ViewGroup方法onInterceptTouchEvent()并返回true时,则会完成翻转但由于此而未收到每张图片上的onClickEvent()

我无法解决问题所在。当我从false返回onInterceptTouchEvent()时,则会收到点击事件,但不会触及触摸事件。

我错过了什么?

ViewFlipper的代码:

public class MyViewFlipper extends ViewFlipper {

    static final String logTag = "ViewFlipper";
    static final int MIN_DISTANCE = 30;
    private float downX, downY, upX, upY;
    Animation slideLeftIn;
    Animation slideLeftOut;
    Animation slideRightIn;
    Animation slideRightOut;
    Context context;
    ViewFlipper viewFlipper;

    public MyViewFlipper(Context context) {
        super(context);
        viewFlipper=this;
        this.context=context;
        System.out.println("I am in MyFlipper() counstructor...");
    }

    public MyViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        viewFlipper=this;
        System.out.println("I am in MyFlipper() counstructor...");
        slideLeftIn =
            AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
        slideLeftOut =
            AnimationUtils.loadAnimation(context, R.anim.slide_left_out);
        slideRightIn =
            AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
        slideRightOut =
            AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                downX = event.getX();
                downY = event.getY();
                return true;
            }
            case MotionEvent.ACTION_UP: {
                upX = event.getX();
                upY = event.getY();

                float deltaX = downX - upX;
                float deltaY = downY - upY;

                // swipe horizontal?
                if (Math.abs(deltaX) > MIN_DISTANCE) {
                    // left or right
                    if (deltaX < 0) {
                        this.onLeftToRightSwipe();
                        return true;
                    }
                    if (deltaX > 0) {
                        this.onRightToLeftSwipe();
                        return true;
                    }
                } else {
                    if(Math.abs(deltaX)<15){
                        onClickEvent();
                    }
                    //Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
                    //+ " long, need at least " + MIN_DISTANCE);
                }
                // swipe vertical?
                if (Math.abs(deltaY) > MIN_DISTANCE) {
                    // top or down
                    if (deltaY < 0) {
                        this.onTopToBottomSwipe();
                        return true;
                    }
                    if (deltaY > 0) {
                        this.onBottomToTopSwipe();
                        return true;
                    }
                } else {
                    Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
                        + " long, need at least " + MIN_DISTANCE);
                }

                return true;
            }
        }
        return false;

    }

    public void onRightToLeftSwipe() {

        viewFlipper.setInAnimation(slideLeftIn);
        viewFlipper.setOutAnimation(slideLeftOut);
        viewFlipper.showNext();
    }

    public void onLeftToRightSwipe() {

        viewFlipper.setInAnimation(slideRightIn);
        viewFlipper.setOutAnimation(slideRightOut); 
        viewFlipper.showPrevious();
    }

    public void onTopToBottomSwipe() {
        Log.i(logTag, "onTopToBottomSwipe!");
        // activity.doSomething();
    }

    public void onBottomToTopSwipe() {
        Log.i(logTag, "onBottomToTopSwipe!");
        // activity.doSomething();
    }

    public void onClickEvent(){
        Toast.makeText(context, "Click",Toast.LENGTH_SHORT);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;    // Here if true then Flipping done.
                        // And if false then click event done. 
    }

}

1 个答案:

答案 0 :(得分:4)

我有类似的问题。 这就是我做的。我在我的视图鳍状肢中有一个列表视图,我覆盖了我的列表视图的setOnTouchListener()并从那里动画了视图鳍状肢。它对我有用。

尝试覆盖案例中setOnTouchListener()的{​​{1}}。