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);
}
}
}
}
RecyclerView选中Item滚动到列表正中间:
public class CenterLayoutManager extends LinearLayoutManager {
public CenterLayoutManager(Context context) {
super(context);
}
public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CenterLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private static class CenterSmoothScroller extends LinearSmoothScroller {
CenterSmoothScroller(Context context) {
super(context);
}
@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return 50f / (float) displayMetrics.densityDpi;
}
}
}
调用:
private CenterLayoutManager = new CenterLayoutManager( mContext, LinearLayoutManager.VERTICAL, false);
mFilterRecycleView.setLayoutManager(mNewLayoutManager);