Fling gesture detection on grid layout

前端 未结 18 1930
遥遥无期
遥遥无期 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:24

    If you dont like to create a separate class or make code complex,
    You can just create a GestureDetector variable inside OnTouchListener and make your code more easier

    namVyuVar can be any name of the View on which you need to set the listner

    namVyuVar.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View view, MotionEvent MsnEvtPsgVal)
        {
            flingActionVar.onTouchEvent(MsnEvtPsgVal);
            return true;
        }
    
        GestureDetector flingActionVar = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener()
        {
            private static final int flingActionMinDstVac = 120;
            private static final int flingActionMinSpdVac = 200;
    
            @Override
            public boolean onFling(MotionEvent fstMsnEvtPsgVal, MotionEvent lstMsnEvtPsgVal, float flingActionXcoSpdPsgVal, float flingActionYcoSpdPsgVal)
            {
                if(fstMsnEvtPsgVal.getX() - lstMsnEvtPsgVal.getX() > flingActionMinDstVac && Math.abs(flingActionXcoSpdPsgVal) > flingActionMinSpdVac)
                {
                    // TskTdo :=> On Right to Left fling
    
                    return false;
                }
                else if (lstMsnEvtPsgVal.getX() - fstMsnEvtPsgVal.getX() > flingActionMinDstVac && Math.abs(flingActionXcoSpdPsgVal) > flingActionMinSpdVac)
                {
                    // TskTdo :=> On Left to Right fling
    
                    return false;
                }
    
                if(fstMsnEvtPsgVal.getY() - lstMsnEvtPsgVal.getY() > flingActionMinDstVac && Math.abs(flingActionYcoSpdPsgVal) > flingActionMinSpdVac)
                {
                    // TskTdo :=> On Bottom to Top fling
    
                    return false;
                }
                else if (lstMsnEvtPsgVal.getY() - fstMsnEvtPsgVal.getY() > flingActionMinDstVac && Math.abs(flingActionYcoSpdPsgVal) > flingActionMinSpdVac)
                {
                    // TskTdo :=> On Top to Bottom fling
    
                    return false;
                }
                return false;
            }
        });
    });
    

提交回复
热议问题