package com.live.quanmin.autopicker;

import android.content.Context;
import android.graphics.PointF;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;

/**
 * Created by guo_hx on 2017/8/10 14:34.
 */

public class ScrollSpeedLinearLayoutManager extends LinearLayoutManager {

    private static final String TAG = ScrollSpeedLinearLayoutManager.class.getSimpleName();

    private double MILLISECONDS_PER_INCH = 0.3f;
    private Context context;

    public ScrollSpeedLinearLayoutManager(Context context) {
        super(context);
        this.context = context;
    }

    public ScrollSpeedLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
        this.context = context;
    }

    public ScrollSpeedLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        LinearSmoothScroller linearSmoothScroller =
                new LinearSmoothScroller(recyclerView.getContext()) {
                    @Override
                    public PointF computeScrollVectorForPosition(int targetPosition) {
                        return ScrollSpeedLinearLayoutManager.this
                                .computeScrollVectorForPosition(targetPosition);
                    }

                    //This returns the milliseconds it takes to
                    //scroll one pixel.
                    @Override
                    protected float calculateSpeedPerPixel
                    (DisplayMetrics displayMetrics) {
                        float v = (float) (MILLISECONDS_PER_INCH / displayMetrics.density);
                        Log.i(TAG , "V = " + v);
                        return v;
                        //返回滑动一个pixel需要多少毫秒
                    }

                };
        linearSmoothScroller.setTargetPosition(position);
        startSmoothScroll(linearSmoothScroller);
    }

    public void setSpeedSlow() {
        //自己在这里用density去乘，希望不同分辨率设备上滑动速度相同
        //0.3f是自己估摸的一个值，可以根据不同需求自己修改
        MILLISECONDS_PER_INCH = context.getResources().getDisplayMetrics().density * 0.3f;
    }

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

    public void setSpeed(double mill) {
        Log.i(TAG, "MILL = " + mill);
        MILLISECONDS_PER_INCH = mill;
    }
}

@@ -609,24 +609,39 @@
 INCH = 0.3f;
+//default speed
 %0A    private
@@ -1963,17 +1963,14 @@
     
-float v =
+return
  (fl
@@ -2028,91 +2028,8 @@
 y);%0A
-                        Log.i(TAG , %22V = %22 + v);%0A                        return v;%0A
     
@@ -2261,88 +2261,8 @@
 ) %7B%0A
-        //%E8%87%AA%E5%B7%B1%E5%9C%A8%E8%BF%99%E9%87%8C%E7%94%A8density%E5%8E%BB%E4%B9%98%EF%BC%8C%E5%B8%8C%E6%9C%9B%E4%B8%8D%E5%90%8C%E5%88%86%E8%BE%A8%E7%8E%87%E8%AE%BE%E5%A4%87%E4%B8%8A%E6%BB%91%E5%8A%A8%E9%80%9F%E5%BA%A6%E7%9B%B8%E5%90%8C%0A        //0.3f%E6%98%AF%E8%87%AA%E5%B7%B1%E4%BC%B0%E6%91%B8%E7%9A%84%E4%B8%80%E4%B8%AA%E5%80%BC%EF%BC%8C%E5%8F%AF%E4%BB%A5%E6%A0%B9%E6%8D%AE%E4%B8%8D%E5%90%8C%E9%9C%80%E6%B1%82%E8%87%AA%E5%B7%B1%E4%BF%AE%E6%94%B9%0A
     

format code