public static void moveToPosition(RecyclerView rv, int position) {
if (rv != null && rv.getLayoutManager() instanceof LinearLayoutManager) {
LinearLayoutManager layoutManager = (LinearLayoutManager) rv.getLayoutManager();
int topPosition = layoutManager.findFirstVisibleItemPosition();
int bottomPosition = layoutManager.findLastVisibleItemPosition();
int itemHeight = rv.getChildAt(topPosition).getHeight();
int minVisibleViewCount = rv.getHeight() / itemHeight;
if (position < topPosition) {
rv.smoothScrollBy(0, -(topPosition - position) * itemHeight);
} else if (position > bottomPosition) {
rv.smoothScrollBy(0, (position - bottomPosition + minVisibleViewCount - 1) * itemHeight);
} else {
int movePosition = position - topPosition;
int top = rv.getChildAt(movePosition).getTop();
rv.smoothScrollBy(0, top);
}
}
}
目前需求,所以只写了这个 后续有时间会更新不同Item指定滑动到指定位置...
网上写的千篇一律,用起来 都多少会有问题..全程自己写的