Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用LinearLayoutManager正常滚动实现OnItemTouchListener的RecyclerView_Android_Android Recyclerview_Ontouchlistener_Ontouch - Fatal编程技术网

Android 使用LinearLayoutManager正常滚动实现OnItemTouchListener的RecyclerView

Android 使用LinearLayoutManager正常滚动实现OnItemTouchListener的RecyclerView,android,android-recyclerview,ontouchlistener,ontouch,Android,Android Recyclerview,Ontouchlistener,Ontouch,我正在尝试实现一个用例,在这个用例中,当用户以低速滚动时,我需要滚动RecyclerView(通常是Android LayoutManager滚动),当用户以一定的速度滚动时,我需要滚动一定的额外量 我实施了一个线性布局管理器,如下所示: public class ScrollLayoutManager extends LinearLayoutManager { // constructors @Override public void onAttachedToWind

我正在尝试实现一个用例,在这个用例中,当用户以低速滚动时,我需要滚动RecyclerView(通常是Android LayoutManager滚动),当用户以一定的速度滚动时,我需要滚动一定的额外量

我实施了一个线性布局管理器,如下所示:

public class ScrollLayoutManager extends LinearLayoutManager {
    // constructors

    @Override
    public void onAttachedToWindow(RecyclerView recyclerView) {
        Log.d(TAG, "onAttachedToWindow");
        super.onAttachedToWindow(recyclerView);
        mRecyclerView = recyclerView;
        mRecyclerView.addOnItemTouchListener(new ThresholdDetector());
    }

    private final class ThresholdDetector implements RecyclerView.OnItemTouchListener {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent e) {
            // some calculations
            return true;
        }

        @Override
        public void onTouchEvent(final RecyclerView view, final MotionEvent e) {
            final int action = e.getActionMasked();
            mVelocityTracker.addMovement(e);
            if(action == MotionEvent.ACTION_MOVE) {
                if (thresholdMet) {
                    // Custom scroll
                    mRecyclerView.scrollBy(calculatedAmount)
                } else {
                    // Scroll normally as per Android LinearLayoutManager
                }
            }
        }
    }
}
如果不满足阈值,我需要Android来处理滚动,否则我无法平滑滚动。当未达到阈值时,我在else中尝试了以下方法,但效果并不理想

                    mRecyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            mRecyclerView.smoothScrollBy(-(int)getDeltaX(motionEvent), 0);
                        }
                    });

如果看不到ThresholdDetector类的全部内容,就不可能确定最佳解决方案是什么。也就是说,我相信问题在于你总是从onInterceptTouchEvent返回true

在有关onInterceptTouchEvent返回值的文档中:

返回:如果此OnItemTouchListener希望开始拦截触摸事件,则返回true;如果返回false,则继续当前行为并继续观察手势中的未来事件

在onTouchEvent的描述中:

将触摸事件作为手势的一部分进行处理,该手势是通过上次调用onInterceptTouchEvent返回true来声明的


换句话说,只有当您从onInterceptTouchEvent为给定事件返回true时,才会调用onTouchEvent。如果希望RecyclerView执行其默认行为,只需在滚动速度低于阈值时从onInterceptTouchEvent返回false即可。

我的错误印象是,如果从onInterceptTouchEvent返回一次true,则所有后续手势和事件都将定向到onTouchEvent。但返回true只会限制特定的手势。