RecyclerView jumps to the specified location

Keywords: Android

Since Android 5.0 launched RecyclerView, RecyclerView is more and more loved by programmers! We all know that the purpose of RecyclerView is to replace listview and ScrollView in the use of lists! So all the functions and methods of listview and ScrollView should have!

But many methods of RecyclerView are not encapsulated in RecyclerView. When we can't find the corresponding method in RecyclerView, we should think of its management class manager!
Most methods are encapsulated here!

Recently, a classmate used RecyclerView to jump to a designated location. Actually, it's very simple. Here I'll give my practice. If you have any suggestions or improvements, please put forward them. If you need them, please use them. Don't spray them ..

Method 1, directly use the current manager (similar to Alipay's more internal interaction).

  /**
     * RecyclerView Move to current position,
     *
     * @param manager  Set the manager corresponding to RecyclerView
     * @param n  Position to jump
     */
    public static void MoveToPosition(LinearLayoutManager manager, int n) {
        manager.scrollToPositionWithOffset(n, 0);
        manager.setStackFromEnd(true);
    }

Method 2: according to the current number of RecyclerView entries, this is relatively complex, but it can effectively avoid pointer crossing

/**
 * RecyclerView Move to current position,
 *
 * @param manager   Set the manager corresponding to RecyclerView
 * @param mRecyclerView  Current RecyclerView
 * @param n  Position to jump
 */
public static void MoveToPosition(LinearLayoutManager manager, RecyclerView mRecyclerView, int n) {


    int firstItem = manager.findFirstVisibleItemPosition();
    int lastItem = manager.findLastVisibleItemPosition();
    if (n <= firstItem) {
        mRecyclerView.scrollToPosition(n);
    } else if (n <= lastItem) {
        int top = mRecyclerView.getChildAt(n - firstItem).getTop();
        mRecyclerView.scrollBy(0, top);
    } else {
        mRecyclerView.scrollToPosition(n);
    }

}

Method 3: with rolling effect

  /**
     * Whether the target item is after the last visible item
     */
    private boolean mShouldScroll;
    /**
     * Record target item location
     */
    private int mToPosition;

    /**
     * Slide to specified position
     *
     * @param mRecyclerView
     * @param position
     */
    private void smoothMoveToPosition(RecyclerView mRecyclerView, final int position) {
        // First visible position
        int firstItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(0));
        // Last visible position
        int lastItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(mRecyclerView.getChildCount() - 1));

        if (position < firstItem) {
            // If the jump position is before the first visible position, smoothScrollToPosition can jump directly
            mRecyclerView.smoothScrollToPosition(position);
        } else if (position <= lastItem) {
            // Jump position after the first visible item and before the last visible item
            // smoothScrollToPosition does not move at all. In this case, call smoothScrollBy to slide to the specified position
            int movePosition = position - firstItem;
            if (movePosition >= 0 && movePosition < mRecyclerView.getChildCount()) {
                int top = mRecyclerView.getChildAt(movePosition).getTop();
                mRecyclerView.smoothScrollBy(0, top);
            }
        } else {
            // If the position to jump is after the last visible item, first call smoothScrollToPosition to scroll the position to jump to the visible position
            // Call smoothMoveToPosition again through onScrollStateChanged control to execute the method in the previous judgment
            mRecyclerView.smoothScrollToPosition(position);
            mToPosition = position;
            mShouldScroll = true;
        }
    }
 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (mShouldScroll) {
                    mShouldScroll = false;
                    smoothMoveToPosition(mRecyclerView, mToPosition);
                }
            }
        });

OK, that's OK. If you don't believe me, try .

Posted by alconebay on Mon, 06 Jan 2020 13:40:12 -0800