Fling gesture detection on grid layout

前端 未结 18 1599
遥遥无期
遥遥无期 2020-11-21 04:38

I want to get fling gesture detection working in my Android application.

What I have is a GridLayout that contains 9 ImageView

18条回答
  •  滥情空心
    2020-11-21 05:22

    I slightly modified and repaired solution from Thomas Fankhauser

    Whole system consists from two files, SwipeInterface and ActivitySwipeDetector


    SwipeInterface.java

    import android.view.View;
    
    public interface SwipeInterface {
    
        public void bottom2top(View v);
    
        public void left2right(View v);
    
        public void right2left(View v);
    
        public void top2bottom(View v);
    
    }
    

    Detector

    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    
    public class ActivitySwipeDetector implements View.OnTouchListener {
    
        static final String logTag = "ActivitySwipeDetector";
        private SwipeInterface activity;
        static final int MIN_DISTANCE = 100;
        private float downX, downY, upX, upY;
    
        public ActivitySwipeDetector(SwipeInterface activity){
            this.activity = activity;
        }
    
        public void onRightToLeftSwipe(View v){
            Log.i(logTag, "RightToLeftSwipe!");
            activity.right2left(v);
        }
    
        public void onLeftToRightSwipe(View v){
            Log.i(logTag, "LeftToRightSwipe!");
            activity.left2right(v);
        }
    
        public void onTopToBottomSwipe(View v){
            Log.i(logTag, "onTopToBottomSwipe!");
            activity.top2bottom(v);
        }
    
        public void onBottomToTopSwipe(View v){
            Log.i(logTag, "onBottomToTopSwipe!");
            activity.bottom2top(v);
        }
    
        public boolean onTouch(View v, 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(v); return true; }
                    if(deltaX > 0) { this.onRightToLeftSwipe(v); return true; }
                }
                else {
                    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(v); return true; }
                    if(deltaY > 0) { this.onBottomToTopSwipe(v); return true; }
                }
                else {
                    Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                    v.performClick();
                }
            }
            }
            return false;
        }
    
    }
    

    it is used like this:

    ActivitySwipeDetector swipe = new ActivitySwipeDetector(this);
    LinearLayout swipe_layout = (LinearLayout) findViewById(R.id.swipe_layout);
    swipe_layout.setOnTouchListener(swipe);
    

    And in implementing Activity you need to implement methods from SwipeInterface, and you can find out on which View the Swipe Event was called.

    @Override
    public void left2right(View v) {
        switch(v.getId()){
            case R.id.swipe_layout:
                // do your stuff here
            break;
        }       
    }
    

提交回复
热议问题