/** * 右屏,列表滚动置顶 * @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; }