Swipe Up & Down gesture on Launcher Application

252 Views Asked by At

I am working on an Android Launcher Application, but I am stuck with the Swipe Up & Down Gesture on the Home Screen. I am using RecyclerView to show the applications in Grid-Layout.

On Home Screen if I Swipe Up it should open the "All Application Fragment" and on All Application Screen if I Swipe down it should close it. I am not able to get any listener to achieve this behavior.

I want to implement this behavior on the whole screen not on recyclerView. So kindly the solution where swipe gesture works irrespective of the recyclerView.

I have tried onTouchListner but due to the RecyclerView, it is not working.

I need such flow

Swipe gestures should work on the whole home screen.

1

There are 1 best solutions below

0
missedSemicolon On

To achieve this behaviour, what you can do is make a new class which implements View.OnTouchListener, add GestureDetector and override the onFling method to detect swipe-up and swipe-down gestures. After that, you can add this new touchListener to your fragment view in onCreate and override the onSwipeUp() and onSwipeDows() methods as per your need. The touchListener class should look like this

public class OnSwipeTouchListener implements View.OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(@NonNull MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();

            if (Math.abs(diffY) > Math.abs(diffX)) {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeDown();
                    } else {
                        onSwipeUp();
                    }
                    return true;
                }
            }
            return false;
        }
    }

    public void onSwipeDown() {
    }

    public void onSwipeUp() {
    }
}

and then add this to your view in the onCreate method of the fragments

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        // Initialize your app dock here

        view.setOnTouchListener(new OnSwipeTouchListener(requireContext()) {
            @Override
            public void onSwipeUp() {
                openAppListFragment();
            }
        });

        return view;
    }