recyclerview怎么获取拖动的position

2025-01-02 23:31:30
推荐回答(1个)
回答1:

  获取 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; }