Android-RecyclerView系列 RecyclerView滚动指定位置到屏幕中间
Posted 彭老希
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android-RecyclerView系列 RecyclerView滚动指定位置到屏幕中间相关的知识,希望对你有一定的参考价值。
一、思路
- 自定义SmoothScroller继承LinearSmoothScroller
- 继承LinearLayoutManager重写smoothScrollToPosition调用自定义SmoothScroller
- 搞清楚calculateDtToFit方法中int viewStart, int viewEnd, int boxStart, int boxEnd的含义计算滚动位置
部分LinearSmoothScroller的源码
//LinearSmoothScroller源码中调用calculateDtToFit方法的入参
//top = item view的顶部
final int top = layoutManager.getDecoratedTop(view) - params.topMargin;
//bottom = item view的底部
final int bottom = layoutManager.getDecoratedBottom(view) + params.bottomMargin;
//start = RecyclerView 的顶部
final int start = layoutManager.getPaddingTop();
//end = RecyclerView 的顶部
final int end = layoutManager.getHeight() - layoutManager.getPaddingBottom();
return calculateDtToFit(top, bottom, start, end, snapPreference);
二、实现
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.AttributeSet;
import android.util.DisplayMetrics;
public class CenterLinearLayoutManager extends LinearLayoutManager {
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);
}
@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 {
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) {
return 100f / displayMetrics.densityDpi;
}
}
}
三、使用
切换RecyclerView的LayoutManager即可
mListView.setLayoutManager(new CenterLinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
以上是关于Android-RecyclerView系列 RecyclerView滚动指定位置到屏幕中间的主要内容,如果未能解决你的问题,请参考以下文章
Android-RecyclerView系列 Item自动吸顶
Android-RecyclerView系列 RecyclerView滚动指定位置到屏幕中间
Android-RecyclerView系列 notifyItemChanged() - 实现单选选中状态更新
Android-RecyclerView系列 RecyclerView滑动后数据显示错乱