Android RecyclerView locates the specified item and pins it to the top

In specific project development, there will be such a requirement: after entering a list (including RecyclerView) page, locate a specified item and display this item at the top.

When it comes to item positioning of RecyclerView, we may think of the following two methods first:

scrollToPosition(int position);
smoothScrollToPosition(int position);

The first method scrollToPosition(position) is to locate the specified item, which is instantly displayed on the page and visible to the user. The location is not fixed.
The second method is different from the first in that it is smoothed to the item you specify, while scrollToPosition is positioned to the specified position instantly.

Although these two methods locate the specified item, they do not display the target item at the top.

At this time, we can use the LinearSmoothScroller class. The function of LinearSmoothScroller is to allow the item specified by RecyclerView to slide within the visible range of the page.

1. Implementation principle: Rewrite the calculateSpeedPerPixel() method of LinearSmoothScroller to reset the sliding speed.

When creating a new LinearSmoothScroller object, the scrolling speed has been calculated. The scrolling speed is related to the pixel density. The larger the MILLISECONDS_PER_PX, the slower the scrolling.

public class LinearTopSmoothScroller extends LinearSmoothScroller {

    /**
     * MILLISECONDS_PER_INCH 值越大滚动越慢
     */
    private float MILLISECONDS_PER_INCH = 0.03f;
    private final Context context;

    /**
     * @param context  context
     * @param needFast 是否需要快速滑动
     */
    public LinearTopSmoothScroller(Context context, boolean needFast) {
        super(context);
        this.context = context;
        if (needFast) {
            setScrollFast();
        } else {
            setScrollSlowly();
        }
    }

    @Override
    protected int getVerticalSnapPreference() {
        return SNAP_TO_START;
    }

    @Override
    protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
        //return super.calculateSpeedPerPixel(displayMetrics);
        setScrollSlowly();
        return MILLISECONDS_PER_INCH / displayMetrics.density;
    }

    public void setScrollSlowly() {
        //建议不同分辨率设备上的滑动速度相同
        //0.3f可以根据不同自己的需求进行更改
        MILLISECONDS_PER_INCH = context.getResources().getDisplayMetrics().density * 0.3f;
    }

    public void setScrollFast() {
        MILLISECONDS_PER_INCH = context.getResources().getDisplayMetrics().density * 0.03f;
    }

}

2. How to call:

    /**
     * 指定item并置顶
     *
     * @param position item索引
     */
    public void scrollItemToTop(int position) {
        LinearTopSmoothScroller smoothScroller = new   LinearTopSmoothScroller(mContext,false);
        smoothScroller.setTargetPosition(position);
        mLayoutManager.startSmoothScroll(smoothScroller);
    }
具体到需求代码中:
// 渐渐上滑定位到评论区域,如果页面是网络请求的数据,则可以等页面展示结束再滑动。
new Handler().postDelayed(new Runnable() {
   @Override
   public void run() {
       // position根据自己的需求传入即可
       binding.baseRecyclerView.scrollItemToTop(position);
   }
}, 200);

Guess you like

Origin blog.csdn.net/u010231454/article/details/122875371