将 fling/scroll 上固定数量的项目滚动到 recyclerview

Scroll fixed number of items on fling/scroll to recyclerview

我有一个简单的 activity,它有一个根 layout.The 根布局包含一个 recyclerview。 Recyclerview 将包含 20 个从 items.xml 创建的项目, 其中包含 3 个文本 views.Here items.xml 表示回收视图中的单个项目。

回收站视图可以双向滚动,它是 1 到 20 之间项目的循环。

我只需要平滑滚动 4 个项目,而不考虑 swipe/fling 的速度。

我尝试了很多方法但直到现在都没有成功的方法。如果有人可以提供任何建议,那将非常有帮助。

  1. SnapHelper - 这似乎只捕捉位于中心的项目。

  2. 使用手势检测器 - 检测到所有动作的手势,但我无法阻止 recyclerview 的默认滚动行为。

主要问题是由于用户滚动和滑动的速度不同导致回收站视图过度滚动。

这是我对手势检测器的设置。

public myRecyclerTouchListner(final Context context, final RecyclerView recycleView,
                                                     final LinearLayoutManager linearLayoutManager,
                                                     final ClickListener clicklistener){


            final ViewConfiguration vc = ViewConfiguration.get(context);
            final int swipeMinDistance = vc.getScaledPagingTouchSlop();
            final int swipeThresholdVelocity = vc.getScaledMinimumFlingVelocity();
            final int swipeMaxOffPath = vc.getScaledTouchSlop();

            this.clicklistener=clicklistener;
            simpleGestureDetector =new GestureDetector(context,new GestureDetector.SimpleOnGestureListener( ){
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    Log.d("try", "on Fling called");
                    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE &&
                            Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        //From Right to Left
                        recycleView.smoothScrollToPosition(linearLayoutManager.
                                findLastCompletelyVisibleItemPosition() + Constants.spanLength);
                        return true;
                    }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE &&
                            Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        //From Left to Right
                        recycleView.smoothScrollToPosition(linearLayoutManager.
                                findFirstCompletelyVisibleItemPosition() - Constants.spanLength);
                        return true;
                    }

                    return true;
                }

                @Override
                public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {


                    return true;
                }
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return false;
                }

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

                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    return true;
                }
                @Override
                public void onLongPress(MotionEvent e) {
                    Log.d("Try", "Long press gesture");

                    View child=recycleView.findChildViewUnder(e.getX(),e.getY());
                    if(child!=null && clicklistener!=null){
                        clicklistener.onLongClick(child,recycleView.getChildAdapterPosition(child));
                    }
                }
            });

我认为你的问题是滚动是从检测到 Fling 的最后一个可见位置开始的,这把事情搞砸了。

尝试使用全局变量,例如 int lastScrolledPosition = 0;。

OnFlingrecycleView.smoothScrollToPosition(lastScrolledPosition + Constants.spanLength);

我认为这应该可以解决问题。如果 lastScrolledPosition < Recyclerites 计数,你应该改变。

事实证明,我必须使用 Snaphelper 来实现自定义滚动行为,以防我需要控制编程滚动和 user-scroll(fling)。

这正是我要找的:

希望对您有所帮助:)