如何获取 RecyclerView 的滑动距离

2025-01-06 03:13:08
推荐回答(1个)
回答1:

已滑动的距离:
private int getScrolledDistance() {
LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
View firstVisibleItem = this.getChildAt(0);
int firstItemPosition = layoutManager.findFirstVisibleItemPosition();
int itemHeight = firstVisibleItem.getHeight();
int firstItemBottom = layoutManager.getDecoratedBottom(firstVisibleItem);
return (firstItemPosition + 1) * itemHeight - firstItemBottom;
}