需要实现对LinearLayoutManager的重写
中间显示类
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
public class CenterLayoutManager extends LinearLayoutManager {
static int lastPositon = 0;
static int targetPosion = 0;
public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
CenterSmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int lastpositon, int position) {
this.lastPositon = lastpositon;
this.targetPosion = position;
smoothScrollToPosition(recyclerView, state, position);
}
public static class CenterSmoothScroller extends LinearSmoothScroller {
private static float duration = 800f;//显示到中间的动画时长
public 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) {
float newDuration = duration / (Math.abs(targetPosion - lastPositon));//重新计算相近两个位置的滚动间隔
return newDuration / displayMetrics.densityDpi;
}
@Override
protected int calculateTimeForScrolling(int dx) {
return super.calculateTimeForScrolling(dx);
}
}
}
置顶显示类
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;
public class TopLinearLayoutManager extends LinearLayoutManager {
public TopLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private class TopSnappedSmoothScroller extends LinearSmoothScroller {
public TopSnappedSmoothScroller(Context context) {
super(context);
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return TopLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;
}
}
}
调用方法,按需采用
//初始化置顶
TopLinearLayoutManager topLinearLayoutManager = new TopLinearLayoutManager(getContext(),
LinearLayoutManager.VERTICAL, false);
//初始化中间竖向
CenterLayoutManager centerLayoutManager = new CenterLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
//初始化中间横向
CenterLayoutManager centerLayoutManager = new CenterLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
//绑定置顶
mRecyclerView.setLayoutManager(topLinearLayoutManager);
//绑定中间
mRecyclerView.setLayoutManager(centerLayoutManager );
//置顶显示调用
mRecyclerView.smoothScrollToPosition(position);
//中间显示调用 lastPostion是临时保存的目前处在中间的position
centerLayoutManager.smoothScrollToPosition(mRecyclerView, new RecyclerView.State(), lastPostion, position);
//记住当前选中的位置作为下一次选中的位置的上一次位置
if (lastPostion != position)
lastPostion = position;