End up activity on swipe right?

What you are trying to do is basically default behavior in Android Wear and its is consider as standard practices in Android Watches to exit an app. In Android wear DismissOverlayView does all heavy lifting for you.

Smartphones have back button while Wear rely on long press or swipe dismiss pattern for to exit screen. You should dismiss Activity on back press, mixing wear pattern in Android Smartphones will make user confused. At Least show a warning dialog to avoid accidental exit.

Solution

As I see this question is tagged with Android Activity I would suggest you to make an Base Activity which will take care of swipe gesture and finish() itself on left to right swipe.

The base activity class should look like this :-

   public abstract class SwipeDismissBaseActivity extends AppCompatActivity {
    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 GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gestureDetector = new GestureDetector(new SwipeDetector());
    }

    private class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            // Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH,
            // then dismiss the swipe.
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;

            // Swipe from left to right.
            // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
            // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
            if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                finish();
                return true;
            }

            return false;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TouchEvent dispatcher.
        if (gestureDetector != null) {
            if (gestureDetector.onTouchEvent(ev))
                // If the gestureDetector handles the event, a swipe has been
                // executed and no more needs to be done.
                return true;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
}

Now you can make other activities extend this base Activity and they will Inheritance will automatically make them adopt swipe to dismiss behavior .

public class SomeActivity extends SwipeDismissBaseActivity {

Advantages of this way

  • Purely OOPS approach
  • Clean code - no need to write swipe listener in each type of layout used in project (Relative,Linear etc)
  • Work perfectly in ScrollView

I believe the issue you have with the RecyclerView and ScrollView has to do with the child elements obtaining the focus before the parents. you can try to set the android:descendantFocusability="beforeDescendants" for the Recycler/Scroll views.


Try this I am using Function for swipe like that

View to swipe...

 yourview.setOnTouchListener(new SimpleGestureFilter(this)); // yourview is layout or container to swipe

SimpleGestureFilter class

  public class SimpleGestureFilter implements View.OnTouchListener {

        static final String logTag = "ActivitySwipeDetector";
        private Context activity;
        static final int MIN_DISTANCE = 100;// TODO change this runtime based on screen resolution. for 1920x1080 is to small the 100 distance
        private float downX, downY, upX, upY;

        // private NDAAgreementActivity mMainActivity;

        public SimpleGestureFilter(Context mainActivity) {
            activity = mainActivity;
        }

        public void onRightToLeftSwipe() {


          //do your code to right to left



        }

        public void onLeftToRightSwipe() {
            //do your code to left to  right
        }

        public void onTopToBottomSwipe() {

        }

        public void onBottomToTopSwipe() {

        }

        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();
                            return true;
                        }
                        if (deltaX > 0) {
                            this.onRightToLeftSwipe();
                            return true;
                        }
                    } else {
                        Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long horizontally, need at least " + MIN_DISTANCE);
                        // return false; // We don't consume the event
                    }

                    // swipe vertical?
                    if (Math.abs(deltaY) > MIN_DISTANCE) {
                        // top or down
                        if (deltaY < 0) {
                            this.onTopToBottomSwipe();
                            return true;
                        }
                        if (deltaY > 0) {
                            this.onBottomToTopSwipe();
                            return true;
                        }
                    } else {
                        Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long vertically, need at least " + MIN_DISTANCE);
                        // return false; // We don't consume the event
                    }

                    return false; // no swipe horizontally and no swipe vertically
                }// case MotionEvent.ACTION_UP:
            }
            return false;
        }
    }

I have faced the same problem regarding the Activity Overlay. This worked for me

1) Define you SwipeListener

public class SwipeListener implements View.OnTouchListener {

private SwipeListenerInterface activity;
private float downX, downY, upX, upY;

public SwipeListener(SwipeListenerInterface activity) {
    this.activity = activity;
}

public void onRightToLeftSwipe(View v) {
    Log.i(logTag, "RightToLeftSwipe!");
    activity.onRightToLeftSwipe(v);
}

public void onLeftToRightSwipe(View v) {
    Log.i(logTag, "LeftToRightSwipe!");
    activity.onLeftToRightSwipe(v);
}

public void onTopToBottomSwipe(View v) {
    Log.i(logTag, "TopToBottomSwipe!");
    activity.onTopToBottomSwipe(v);
}

public void onBottomToTopSwipe(View v) {
    Log.i(logTag, "BottomToTopSwipe!");
    activity.onBottomToTopSwipe(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;

                if (deltaX < 0 ) {
                    this.onLeftToRightSwipe(v);
                    return true;
                }
                if (deltaX > 0 ) {
                    this.onRightToLeftSwipe(v);
                    return true;
                }

                if (deltaY < 0) {
                    this.onTopToBottomSwipe(v);
                    return true;
                }
                if (deltaY > 0) {
                    this.onBottomToTopSwipe(v);
                    return true;
                }

        }
    }
    return false;
}

public void setSwipeRestrictions(int swipeRestrictionX, int swipeRestrictionY) {
    this.swipeRestrictionX = swipeRestrictionX;
    this.swipeRestrictionY = swipeRestrictionY;
}

2) With the following referred interface

    public interface SwipeListenerInterface {

    void onRightToLeftSwipe(View v);

    void onLeftToRightSwipe(View v);

    void onTopToBottomSwipe(View v);

    void onBottomToTopSwipe(View v);
}

3) Create the object and bind it to your overlayView (make sure to adjust the interface to the overLay view so it can receive the callbacks)

    sl = new SwipeListener(this);
    overlayView.setOnTouchListener(sl);