【问题标题】:Scroll fixed number of items on fling/scroll to recyclerview滚动固定数量的项目在投掷/滚动到recyclerview
【发布时间】:2018-01-14 18:43:11
【问题描述】:

我有一个简单的活动,它有一个根布局。根布局包含一个 recyclerview。 Recyclerview 将包含从 items.xml 创建的 20 个项目, 它由 3 个文本视图组成。这里的 items.xml 代表 recyclerview 中的单个项目。

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

我只需要平滑滚动 4 个项目,无论滑动/甩动的速度如何。

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

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

【问题讨论】:

    标签: android android-recyclerview smooth-scrolling gesturedetector fling


    【解决方案1】:

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

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

    还有OnFlingrecycleView.smoothScrollToPosition(lastScrolledPosition + Constants.spanLength);

    我认为这应该可以解决问题。如果 lastScrolledPosition

    【讨论】:

    • 嗨 MeLine,感谢您的回答,实际上我已经实现了与您建议的相同的逻辑,但问题是回收视图由于滑动速度的变化而过度滚动,我想更改这种行为。
    【解决方案2】:

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

    这正是我想要的: How to snap RecyclerView items so that every X items would be considered like a single unit to snap to?

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多