记录:recyclerview滑动到指定的下标处,快速导航

       private boolean isRecyclerScroll = false;//recyclerview是否正在滑动

       LinearSmoothScroller linearSmoothScroller= new LinearSmoothScroller(this) {
            // 这里考虑的是垂直列表
            @Override
            protected int getVerticalSnapPreference() {
                // 固定返回顶对齐方式
                return SNAP_TO_START;
            }
        };

      recyclerView.setOnTouchListener((v, event) -> {
            //当滑动由recyclerView触发时,isRecyclerScroll 置true
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                isRecyclerScroll = true;
            }
            return false;
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (isRecyclerScroll) {
                    //处理业务逻辑
                    //第一个可见的view的位置,即tablayou需定位的位置
                    //int position = linearLayoutManager.findFirstVisibleItemPosition();
                    //if (lastPos != position) {
                    //    tabLayout.setScrollPosition(position, 0, true);
                    //}
                    //lastPos = position;
                }
            }
        });


      //调用代码,滑动到指定位置
      // 设置滚动目标 position
      linearSmoothScroller.setTargetPosition(tab.getPosition());
      // 主动触发滚动
      linearLayoutManager.startSmoothScroll(linearSmoothScroller);

你可能感兴趣的:(记录:recyclerview滑动到指定的下标处,快速导航)