本文主要是介绍RecyclerView选中项居中(横向、竖向),指定位置置顶(竖向),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需要实现对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);}@Overridepublic 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);}@Overridepublic int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);}@Overrideprotected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {float newDuration = duration / (Math.abs(targetPosion - lastPositon));//重新计算相近两个位置的滚动间隔return newDuration / displayMetrics.densityDpi;}@Overrideprotected 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);}@Overridepublic 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);}@Overridepublic PointF computeScrollVectorForPosition(int targetPosition) {return TopLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);}@Overrideprotected 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;
这篇关于RecyclerView选中项居中(横向、竖向),指定位置置顶(竖向)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!