/**
* 右屏,列表滚动置顶
* @param currPos 实际下标
* @param pos 用于上一条 翻上一页置顶的假下标,实际下标另外高亮
* @param isClearOffset 是否置顶
*/
private void scrollTop(int pos, boolean isClearOffset) {
RecyclerView.SmoothScroller smoothScroller = getScrollStyle(isClearOffset);
if (pos >= 0) {
smoothScroller.setTargetPosition(pos);
} else {
smoothScroller.setTargetPosition(currPos);
}
LinearLayoutManager mLayoutManager = (LinearLayoutManager) mListview.getLayoutManager();
mLayoutManager.startSmoothScroll(smoothScroller);
mAdapter.setCurrPos(currPos);
mAdapter.notifyDataSetChanged();
}
/**
* 是否滚动置顶
*
* @param isClearOffset
* @return
*/
private RecyclerView.SmoothScroller getScrollStyle(boolean isClearOffset) {
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(mContext) {
@Override
protected int getVerticalSnapPreference() {
return isClearOffset ? LinearSmoothScroller.SNAP_TO_START : LinearSmoothScroller.SNAP_TO_ANY;
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return 150f / displayMetrics.densityDpi;
}
};
return smoothScroller;
}
本文介绍了一种在右屏列表中实现平滑滚动至指定位置的方法。通过自定义`SmoothScroller`,根据参数判断是否滚动到顶部,并调整滚动速度以达到平滑效果。
1万+

被折叠的 条评论
为什么被折叠?



