Fling gesture detection on grid layout

前端 未结 18 1591
遥遥无期
遥遥无期 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 do it a little different, and wrote an extra detector class that implements the View.onTouchListener

    onCreateis simply add it to the lowest layout like this:

    ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
    lowestLayout = (RelativeLayout)this.findViewById(R.id.lowestLayout);
    lowestLayout.setOnTouchListener(activitySwipeDetector);
    

    where id.lowestLayout is the id.xxx for the view lowest in the layout hierarchy and lowestLayout is declared as a RelativeLayout

    And then there is the actual activity swipe detector class:

    public class ActivitySwipeDetector implements View.OnTouchListener {
    
    static final String logTag = "ActivitySwipeDetector";
    private Activity activity;
    static final int MIN_DISTANCE = 100;
    private float downX, downY, upX, upY;
    
    public ActivitySwipeDetector(Activity activity){
        this.activity = activity;
    }
    
    public void onRightSwipe(){
        Log.i(logTag, "RightToLeftSwipe!");
        activity.doSomething();
    }
    
    public void onLeftSwipe(){
        Log.i(logTag, "LeftToRightSwipe!");
        activity.doSomething();
    }
    
    public void onDownSwipe(){
        Log.i(logTag, "onTopToBottomSwipe!");
        activity.doSomething();
    }
    
    public void onUpSwipe(){
        Log.i(logTag, "onBottomToTopSwipe!");
        activity.doSomething();
    }
    
    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) > Math.abs(deltaY))
            {
                if(Math.abs(deltaX) > MIN_DISTANCE){
                    // left or right
                    if(deltaX > 0) { this.onRightSwipe(); return true; }
                    if(deltaX < 0) { this.onLeftSwipe(); return true; }
                }
                else {
                        Log.i(logTag, "Horizontal Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                        return false; // We don't consume the event
                }
            }
            // swipe vertical?
            else 
            {
                if(Math.abs(deltaY) > MIN_DISTANCE){
                    // top or down
                    if(deltaY < 0) { this.onDownSwipe(); return true; }
                    if(deltaY > 0) { this.onUpSwipe(); return true; }
                }
                else {
                        Log.i(logTag, "Vertical Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                        return false; // We don't consume the event
                }
            }
    
                return true;
            }
        }
        return false;
    }
    
    }
    

    Works really good for me!

提交回复
热议问题