转自:https://blog.csdn.net/x466911254/article/details/78525980
RecyclerView滚动的方法有
smoothScrollBy();
smoothScrollToPosition()
scrollTo();
scrollBy();
其中带有smooth的方法为带有滚动动画效果的方法。我们发现满足我们要求的就只有smoothScrollToPosition()方法。但是smoothScrollToPosition()只能将我指定的position的item显示在界面上,但不能将其置顶。我们查看RecyclerView的smoothScrollToPosition()方法源码如下:
public void smoothScrollToPosition(int position) {
if (mLayoutFrozen) {
return;
}
if (mLayout == null) {
Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. "
+ "Call setLayoutManager with a non-null argument.");
return;
}
mLayout.smoothScrollToPosition(this, mState, position);
}
我们可以看到RecyclerView的滑动实际是调用了LayoutManager的滑动方法。
我们查看LinearLayoutManager的smoothScrollToPosition()方法:
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
LinearSmoothScroller linearSmoothScroller =
new LinearSmoothScroller(recyclerView.getContext());
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
发现LinearLayoutManager的smoothScrollToPosition()方法中new 了一个LinearSmoothScroller 的东西来控制其滑动,我们重写LinearSmoothScroller :
class AdvertiseLinearSmoothScroller extends LinearSmoothScroller{
public AdvertiseLinearSmoothScroller(Context context) {
super(context);
}
/**
*
* @param viewStart RecyclerView的top位置
* @param viewEnd RecyclerView的Bottom位置
* @param boxStart item的top位置
* @param boxEnd item的bottom位置
* @param snapPreference 滑动方向的识别
* @return
*/
@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return boxStart-viewStart;//返回的就是我们item置顶需要的偏移量
}
/**
* 此方法返回滚动每1px需要的时间,可以用来控制滚动速度
* 即如果返回2ms,则每滚动1000px,需要2秒钟
* @param displayMetrics
* @return
*/
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return super.calculateSpeedPerPixel(displayMetrics);
}
}
我们重写calculateDtToFit()方法,即可实现smoothScrollToPosition()使item自动置顶功能.
使用AdvertiseLinearSmoothScroller 重写LinearLayoutManager
class AdvertiseLinearSmoothScroller extends LinearSmoothScroller { public AdvertiseLinearSmoothScroller(Context context) { super(context); } /** * * @param viewStart RecyclerView的top位置 * @param viewEnd RecyclerView的Bottom位置 * @param boxStart item的top位置 * @param boxEnd item的bottom位置 * @param snapPreference 滑动方向的识别 * @return */ @Override public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) { System.out.println("!###boxStart:"+boxStart+"!###viewStart:"+viewStart); return boxStart-viewStart+100;//返回的就是我们item置顶需要的偏移量 } /** * 此方法返回滚动每1px需要的时间,可以用来控制滚动速度 * 即如果返回2ms,则每滚动1000px,需要2秒钟 * @param displayMetrics * @return */ @Override protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { return super.calculateSpeedPerPixel(displayMetrics); } }