獲取 RecyclerView 的滑動距離:
滑動到一定程度後清零是因為 getChildAt(0) 獲得的是第一個可見view 用LinearLayoutManager的recyclerview測試了一下確實如此。
如果LayoutManager用的是LinearLayoutManager 可以用下面的辦法,還能向下滑動多少 * * @return */ private int getDistance() { LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager(); View firstVisibleItem = this.getChildAt(0); int firstItemPosition = layoutManager.findFirstVisibleItemPosition(); int itemCount = layoutManager.getItemCount(); int recyclerviewHeight = this.getHeight(); int itemHeight = firstVisibleItem.getHeight(); int firstItemBottom = layoutManager.getDecoratedBottom(firstVisibleItem); return (itemCount - firstItemPosition - 1) * itemHeight - recyclerviewHeight + firstItemBottom; }
已滑動的距離:
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; }
獲取 RecyclerView 的滑動距離:
滑動到一定程度後清零是因為 getChildAt(0) 獲得的是第一個可見view 用LinearLayoutManager的recyclerview測試了一下確實如此。
如果LayoutManager用的是LinearLayoutManager 可以用下面的辦法,還能向下滑動多少 * * @return */ private int getDistance() { LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager(); View firstVisibleItem = this.getChildAt(0); int firstItemPosition = layoutManager.findFirstVisibleItemPosition(); int itemCount = layoutManager.getItemCount(); int recyclerviewHeight = this.getHeight(); int itemHeight = firstVisibleItem.getHeight(); int firstItemBottom = layoutManager.getDecoratedBottom(firstVisibleItem); return (itemCount - firstItemPosition - 1) * itemHeight - recyclerviewHeight + firstItemBottom; }
已滑動的距離:
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; }