阅读背景:

RecyclerView选中Item滚动到指定位置_xxdw1992的博客_recyclerview滚动到指定位置

来源:互联网 

RecyclerView选中Item滚动自定义位置:

public class CenterLinearLayoutManager extends LinearLayoutManager {
    private int mItemCount = 0;

    public CenterLinearLayoutManager(Context context) {
        super(context);
    }

    public CenterLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

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

    public void setItemCount(int count) {
        mItemCount = count;
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        CenterLinearSmoothScroller smoothScroller = new CenterLinearSmoothScroller(recyclerView.getContext());
        smoothScroller.setTargetPosition(position);
        smoothScroller.setItemCount(mItemCount);
        startSmoothScroll(smoothScroller);
    }

    private static class CenterLinearSmoothScroller extends LinearSmoothScroller {
        private static final Log.Tag TAG = new Log.Tag("CenterLinearSmoothScroller");
        private PathInterpolator mBezierInterpolator = new PathInterpolator(0.15f, 0.25f, 0.15f, 1f);
        private int mCount = 0;

        CenterLinearSmoothScroller(Context context) {
            super(context);
        }

        public void setItemCount(int count) {
            mCount = count;
        }

        private int calculateDxToMakeCentral(View view) {
            RecyclerView.LayoutManager layoutManager = getLayoutManager();
            if (layoutManager == null || !layoutManager.canScrollHorizontally()) {
                return 0;
            }
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
            int left = layoutManager.getDecoratedLeft(view) - params.leftMargin;
            int right = layoutManager.getDecoratedRight(view) + params.rightMargin;
            int start = layoutManager.getPaddingLeft();
            int end = layoutManager.getWidth() - layoutManager.getPaddingRight();
            int childCenter = left + (int) ((right - left) / 2.0f);
            int containerCenter = (int) ((end - start) / 2.f);
            int viewWidth = right - left;
            int layoutWidth = end - start;
            int pos = getTargetPosition();
            int leftLength = pos * viewWidth + (int) (viewWidth / 2.0f);
            int rightLength = mCount * viewWidth - leftLength;
            int move = 0;
            Log.d(TAG,"leftLength:"+leftLength+" rightLength:"+rightLength+" containerCenter:"+containerCenter+" pos:"+pos);
            if (leftLength < containerCenter) {
                if (childCenter < leftLength) {
                    move = leftLength - childCenter;
                }
            } else if (rightLength < containerCenter) {
                if ((layoutWidth - childCenter) < rightLength) {
                    move = (layoutWidth - childCenter) - rightLength;
                }
            } else {
                move = containerCenter - childCenter;
            }
            return move;
        }

        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
            final int dx = calculateDxToMakeCentral(targetView);
            Log.d(TAG,"dx:"+dx);
            if (dx != 0) {
                action.update(-dx, 0, 300, mBezierInterpolator);
            }
        }
    }
}public class Cente



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: