RecyclerView调用smoothScrollToPosition() 控制滑动速度

RecyclerView 滑动 到指定的Item 有两个 常用的方法 ScrollToPosition(int position)smoothScrollToPosition(int position)

	/**
     * Starts a smooth scroll to an adapter position.
     * <p>
     * To support smooth scrolling, you must override
     * {@link LayoutManager#smoothScrollToPosition(RecyclerView, State, int)} and create a
     * {@link SmoothScroller}.
     * <p>
     * {@link LayoutManager} is responsible for creating the actual scroll action. If you want to
     * provide a custom smooth scroll logic, override
     * {@link LayoutManager#smoothScrollToPosition(RecyclerView, State, int)} in your
     * LayoutManager.
     *
     * @param position The adapter position to scroll to
     * @see LayoutManager#smoothScrollToPosition(RecyclerView, State, int)
     * 平滑滑动
     */
    public void smoothScrollToPosition(int position) {
        if (mLayoutFrozen) {
            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); //调用LayoutManager设置滑动
    }
    
   /**
     * Convenience method to scroll to a certain position.
     *
     * RecyclerView does not implement scrolling logic, rather forwards the call to
     * {@link android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition(int)}
     * @param position Scroll to this adapter position
     * @see android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition(int)
     * 滑动方法 没有平滑视觉
     */
    public void scrollToPosition(int position) {
        if (mLayoutFrozen) {
            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); //调用LayoutManager设置滑动
        awakenScrollBars();
    }

自定义LayoutManager

接下来我们可以自定一个LayoutManager,然后复写LayoutManager中的smoothScrollToPosition(this, mState, position) 方法,来达到控制滑动速度的目的。以LinearLayoutManager为例子,代码如下:

	/**
	 * 控制滑动速度的LinearLayoutManager
	 */
	public class ScrollSpeedLinearLayoutManger extends LinearLayoutManager {
	    private float MILLISECONDS_PER_INCH = 0.03f;
	    private Context contxt;
	
	    public ScrollSpeedLinearLayoutManger(Context context) {
	        super(context);
	        this.contxt = context;
	    }
	
	    @Override
	    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
	        LinearSmoothScroller linearSmoothScroller =
	                new LinearSmoothScroller(recyclerView.getContext()) {
	                    @Override
	                    public PointF computeScrollVectorForPosition(int targetPosition) {
	                        return ScrollSpeedLinearLayoutManger.this
	                                .computeScrollVectorForPosition(targetPosition);
	                    }
	
	                    //This returns the milliseconds it takes to
	                    //scroll one pixel.
	                    @Override
	                    protected float calculateSpeedPerPixel
	                    (DisplayMetrics displayMetrics) {
	                        return MILLISECONDS_PER_INCH / displayMetrics.density;
	                        //返回滑动一个pixel需要多少毫秒
	                    }
	
	                };
	        linearSmoothScroller.setTargetPosition(position);
	        startSmoothScroll(linearSmoothScroller);
	    }
	
	
	    public void setSpeedSlow() {
	        //自己在这里用density去乘,希望不同分辨率设备上滑动速度相同
	        //0.3f是自己估摸的一个值,可以根据不同需求自己修改
	        MILLISECONDS_PER_INCH = contxt.getResources().getDisplayMetrics().density * 0.3f;
	    }
	
	    public void setSpeedFast() {
	        MILLISECONDS_PER_INCH = contxt.getResources().getDisplayMetrics().density * 0.03f;
	    }
	}

转:RecyclerView调用smoothScrollToPosition() 控制滑动速度

猜你喜欢

转载自blog.csdn.net/silencezmz/article/details/86639735