vlayout icon indicating copy to clipboard operation
vlayout copied to clipboard

使用VLayout后,当RecyclerView滚动到底部后,调用canScrollVertically返回的值不正确

Open peerless2012 opened this issue 6 years ago • 1 comments

使用VLayout后,当当RecyclerView滚动到底部后,再次上拉,这时候上拉加载更多会判断View是否能够竖向滚动,会调用View.canScrollVertically(1);,在未使用VLayout的RecyclerView中这个方法会返回false,使用VLayout后返回的值不确定。

经过跟踪代码发现,View.canScrollVertically(1);的代码如下:

public boolean canScrollVertically(int direction) {
        final int offset = computeVerticalScrollOffset();
        final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
        if (range == 0) return false;
        if (direction < 0) {
            return offset > 0;
        } else {
            return offset < range - 1;
        }
    }

而上述方法中,三个computeXXX方法会回调到LinearLayoutManager中的

  1. computeVerticalScrollOffset(RecyclerView.State state)
  2. computeVerticalScrollRange(RecyclerView.State state)
  3. computeVerticalScrollExtent(RecyclerView.State state)

正常情况下, 在这个时候computeVerticalScrollOffset() == computeVerticalScrollRange() - computeVerticalScrollExtent(),但是使用VLayout后,却是不相等的。从而导致判断错误。

peerless2012 avatar Aug 02 '18 06:08 peerless2012

你解决了吗,我也遇到这问题了,取值不准确

wangdroid avatar Jul 01 '21 09:07 wangdroid