【Android每日一问】RecycleView滚动定位不准确问题研究

1,663 阅读2分钟
  • 开发当中经常会遇到的产品需求,recycleview自动滚动到某一个位置。具体场景可能是如下:
  • 页面初始化之后自动自动跳转到某一个位置
  • 页面滚动之后要回到某一个位置
  • 为了展示完全需要recycleview做微小的位置偏移等

针对如上问题,我们先来看看recycleview都有什么方法提供给我们。

public void scrollToPosition(int position) {
        if (mLayoutSuppressed) {
            return;
        }
        stopScroll();
        if (mLayout == null) {
            Log.e(TAG, "Cannot scroll to position a LayoutManager set. "
                    + "Call setLayoutManager with a non-null argument.");
            return;
        }
        mLayout.scrollToPosition(position);
        awakenScrollBars();
    }

scrollToPosition(int position)方法,滑动到指定positionitem的顶部。

public void smoothScrollToPosition(int position) {
        if (mLayoutSuppressed) {
            return;
        }
        if (mLayout == null) {
            Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. "
                    + "Call setLayoutManager with a non-null argument.");
            return;
        }
        mLayout.smoothScrollToPosition(this, mState, position);
    }

smoothScrollToPosition(int position) 方法同上,但是会有平滑滑动的效果。

public void scrollBy(int x, int y) {
        if (mLayout == null) {
            Log.e(TAG, "Cannot scroll without a LayoutManager set. "
                    + "Call setLayoutManager with a non-null argument.");
            return;
        }
        if (mLayoutSuppressed) {
            return;
        }
        final boolean canScrollHorizontal = mLayout.canScrollHorizontally();
        final boolean canScrollVertical = mLayout.canScrollVertically();
        if (canScrollHorizontal || canScrollVertical) {
            scrollByInternal(canScrollHorizontal ? x : 0, canScrollVertical ? y : 0, null);
        }
    }

scrollBy(int x, int y)方法通过传入偏移量进行滑动。

public void startSmoothScroll(SmoothScroller smoothScroller) {
            if (mSmoothScroller != null && smoothScroller != mSmoothScroller
                    && mSmoothScroller.isRunning()) {
                mSmoothScroller.stop();
            }
            mSmoothScroller = smoothScroller;
            mSmoothScroller.start(mRecyclerView, this);
        }

startSmoothScroll(SmoothScroller smoothScroller)通过传入一个SmoothScroller来控制recycleview的移动。

针对题前所说的三种情况来说 这个需求会涉及到几种情况如下:

  • 目标item已经出现在屏幕当中的情况,这时候调用方法一,方法二是达不到recycleview进行位置的移动的。这时候可以选择调用方法三来达到我们要的效果。
            val smoothScroller: RecyclerView.SmoothScroller = object : LinearSmoothScroller(mContext) {
                override fun getVerticalSnapPreference(): Int {
                    return SNAP_TO_START
                }
            }

            smoothScroller.targetPosition = CIRCLE_POSITION + 1
            mVirtualLayoutManager.startSmoothScroll(smoothScroller)

当然方法四也可以达到我们所要的效果,这时候需要算出来的是我们目标item距离顶部所需的距离

rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
                           ......
                int toTop = rv.getChildAt(n).getTop();
                rvProduct.scrollBy(0, top);
                     ......
    }
});
  • 当目标item如果说并没有出现在屏幕中时,方法一和方法二就可以达到我们需要的效果。