You need to make a svayp on the bottom of the screen for LinearLayout with child view. In the example below, LinearLayout ll occupies the bottom half of the screen. But svayp works on the entire screen thanks to the dispatchTouchEvent . I can not understand how to do it so that the svip works only on the ll and child view . Without the dispatchTouchEvent method, the svip does not work on the child's LinearLayout view .

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = (LinearLayout) findViewById(R.id.ll); lSwipeDetector = new GestureDetectorCompat(this, new MyGestureListener()); ll.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return lSwipeDetector.onTouchEvent(event); } }); } private class MyGestureListener extends GestureDetector.SimpleOnGestureListener{ @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){ if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) // вертикальный свайп return false; // слева направо <- if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(Picker.this, "<-",Toast.LENGTH_SHORT).show(); } // справа налево -> else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Toast.makeText(Picker.this, "->",Toast.LENGTH_SHORT).show(); } return false; } } @Override public boolean dispatchTouchEvent(MotionEvent ev){ super.dispatchTouchEvent(ev); return lSwipeDetector.onTouchEvent(ev); } 

    2 answers 2

    On the right view

     view.setOnTouchListener(this); 

    and method

     public boolean onTouch(View v, MotionEvent event) { float x = event.getX(); float y = event.getY(); String sDown = ""; String sMove = ""; String sUp = ""; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // нажатие sDown = "Down: " + x + "," + y; sMove = ""; sUp = ""; break; case MotionEvent.ACTION_MOVE: // движение sMove = "Move: " + x + "," + y; break; case MotionEvent.ACTION_UP: // отпускание case MotionEvent.ACTION_CANCEL: sMove = ""; sUp = "Up: " + x + "," + y; break; } return true; } 

    Now you just play with x and y. For example: if you have less pressure to let go and the difference between them is more say 120 then this is a svayp down. Well, I think the meaning is understood.

    • This method works on the parent. I have a LinearLayout filled with pictures that have an onClick method assigned. If you assign onTouch to linearLayout, then it works only on it, and it does not work on child pictures. If the pictures and assign view.setOnTouchListener (this); then the onClick click handler stops working. How to make friends? - Termit___
    • Thank. You really helped. - Termit___

    It turned out to make friends onTouchListner and onClickListener. It is necessary in the onTouch method, if the distance of the svayp is less than some value (130), then return false. Then OnTouch will allow you to transfer the action to onClickListener.

     public boolean onTouch(View v, MotionEvent event) { float x1=0,x2=0; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // нажатие x1 = event.getX(); case MotionEvent.ACTION_UP: // отпускание x2 = event.getX(); if (x1 - x2 > 130) { // код для перемещения вправо } else if (x2 - x1 > 130) { //код для перемещения влево } else { // если это не движение(а нажатие) return false; // то возвращаем false, что позволит отработать другим обработчикам } case MotionEvent.ACTION_CANCEL: break; } return true; } 
    • it is better not to use the numbers "from the ceiling" instead of 130 use say some part of the width / height of the screen - Flippy
    • Good advice, thank you. I will use it. - Termit___