Implementing Swipe action on ViewFlipper with multiple GridViews

前端 未结 3 517
Happy的楠姐
Happy的楠姐 2021-02-06 19:49

I have the following code to listen to Swipe action to navigate between GridViews in my ViewFlipper. However, it detects the Swipe inconsistently. I added the Gesture Listener t

相关标签:
3条回答
  • 2021-02-06 19:56

    I have a couple of questions/observations here:

    Q: Can you elaborate on what you mean by inconsistently? Is there any pattern to when the swipe is not detected? Does it work at all? If so, when?

    Q: In your main Activity, in the over-riden onTouchEvent(MotionEvent me) method do you return myGestureDetector.onTouchEvent(me) like this?

    @Override    
    public boolean onTouchEvent(MotionEvent me) {
         return myGestureDetector.onTouchEvent(me);    
    }
    

    here myGestureDetector is an object of your MyGestureDetector class and this basically attaches your GestureDetector to the touch event

    Observation: Can you please return true as soon as your gesture processing is done, that tells android that the gesture capture is done, for example

            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                flipper.setInAnimation(slideLeftIn);
                flipper.setOutAnimation(slideLeftOut);
                flipper.showNext();
                //return true if the gesture event is consumed
                return true; 
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                flipper.setInAnimation(slideRightIn);
                flipper.setOutAnimation(slideRightOut);
                flipper.showPrevious();
                //return true if the gesture event is consumed
                return true;
            }
    
    0 讨论(0)
  • 2021-02-06 20:02

    1) have u added all the gridviews in viewflipper.

    viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
                slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
                slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
                slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
                slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
    
                gestureDetector = new GestureDetector(new MyGestureDetector());
                gestureListener = new View.OnTouchListener() 
                    {
                        public boolean onTouch(View v, MotionEvent event) 
                        {
                            if (gestureDetector.onTouchEvent(event)) 
                            {
                                    return true;
                            }
                            else{
                                    return false;}
                            }
                    };
    

    I have added these in my code it is working fine.just check this let me know

    0 讨论(0)
  • 2021-02-06 20:10

    I keep seeing the same swipe gesture detection code all over StackOverflow, so I have attempted to clean it up a bit. This is a generic listener which can fire events for right or left swipes (easily extended to other

    public class SwipeListener extends GestureDetector.SimpleOnGestureListener {
    
        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    
        private SwipeHandler handler;
    
        public SwipeHandler getHandler() {
            return handler;
        }
    
        public void setHandler(SwipeHandler handler) {
            this.handler = handler;
        }
    
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
    
            // left
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
    
                if (handler != null) {
                    handler.onLeft();
    
                    return true;
                }
    
            // right
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
    
                if (handler != null) {
                    handler.onRight();
    
                    return true;
                }
            }
    
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    
        public static abstract class SwipeHandler{
            public void onLeft() {}
            public void onRight() {}
        }
    }
    

    And then to use this anywhere, just create an instance, setup your swipe event handlers, and attach it:

        SwipeListener swipeListener = new SwipeListener();
        swipeListener.setHandler(new SwipeListener.SwipeHandler() {
            @Override
            public void onRight() {
                doSomething();
            }
        });
    
        someView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });
    
    0 讨论(0)
提交回复
热议问题