android - 在启动器应用程序上向上和向下滑动手势

标签 android kotlin android-recyclerview gesture android-launcher

我正在开发 Android 启动器应用程序,但我无法在主屏幕上使用向上和向下滑动手势。我正在使用 RecyclerView 在网格布局中显示应用程序。

在主屏幕上,如果我向上滑动,它应该打开“所有应用程序 fragment ”,在所有应用程序屏幕上,如果我向下滑动,它应该关闭它。我无法让任何监听器实现此行为。

我想在整个屏幕上而不是在 recyclerView 上实现此行为。因此,无论 recyclerView 如何,滑动手势都可以工作的解决方案。

我尝试过 onTouchListner,但由于 RecyclerView,它不起作用。

I need such flow

滑动手势应该适用于整个主屏幕。

最佳答案

要实现此行为,您可以做的是创建一个实现 View.OnTouchListener 的新类,添加 GestureDetector 并重写 onFling 方法来检测向上滑动和向下滑动手势。之后,您可以将这个新的 touchListener 添加到 onCreate 中的 fragment View 中,并根据需要重写 onSwipeUp() 和 onSwipeDows() 方法。 touchListener 类应该如下所示

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() {
    }
}

然后将其添加到 fragment 的 onCreate 方法中的 View

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;
    }

关于android - 在启动器应用程序上向上和向下滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76882374/

相关文章:

android - 显示对话框将调用哪个 Activity 生命周期函数?

kotlin - 将字符串解析为 Kotlin 中的数组

java - 无法让RecyclerView水平显示Android Java

android - ProgressBar 不以 fragment 为中心

android - cpuminer Android 中的 Curl 库

android - 如何检查 sendBroadcast 是否完成了他的工作

android - 如何为 API 21/Lollipop 以下的设备实现 setZ()?

kotlin - 如何为 Kotlin 中的扩展函数抛出的异常提供自定义异常消息?

java - Kotlin 使用可变数量的参数调用错误的重载 java 方法

Android - 每次旋转设备时,RecyclerView 都会倍增