开发者

ViewFlipper doesn't receive touch events

I am working on an Android project in which I use a ViewFlipper for flipping childs. For this I used a customized flipper. In this I override the onTouchEvent() method. The flipper contain images in each child but when I try to flip it doesn't receive any touch event. When I override the ViewGroup method onInterceptTouchEvent() and return true, then flipping is done but due to this my onClickEvent() on each image is not received.

I am not getting where the problem is. When I return false from onInterceptTouchEvent() then the click event is being received but not the touch event.

What am I missing?

Code for the 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.开发者_Python百科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. 
    }

}


I had a similar problem. Here is what i did. I had a listview in my view flipper and i override the setOnTouchListener() of my listview and animated the view flipper from there. It worked for me.

Try overriding the setOnTouchListener() of ImageView in your case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜