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

/**     * Starts a smooth scroll to an adapter position.     * 

* To support smooth scrolling, you must override * {@link LayoutManager#smoothScrollToPosition(RecyclerView, State, int)} and create a * {@link SmoothScroller}. *

* {@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() 控制滑动速度

更多相关文章

  1. Android(安卓)Wifi模块分析(三)
  2. Android中dispatchDraw分析
  3. Android四大基本组件介绍与生命周期
  4. Android(安卓)Service AIDL
  5. Android调用天气预报的WebService简单例子
  6. android打电话发短信
  7. android 拨打紧急号码,通话时开启免提功能实现
  8. Android调用.NET Webservice报org.ksoap2.serialization.SoapPri
  9. android 自定义view

随机推荐

  1. 用maven创建android的项目。
  2. Android之获取Android唯一ID
  3. Android百度地图基础实现(标记+GPS)
  4. android shape记录
  5. Android:Loader
  6. Centos 安装 android sdk
  7. android studio gradle编译时的jar冲突错
  8. android邮件发送几种方式
  9. Android(安卓)input keyevent
  10. android textview 部分文字加颜色并可点