RecyclerView的position==0位置上的View高度为0时,canScrollVertically方法无法判断是否已经滑动到顶部?
看View中 canScrollVertically
方法的调用:
/**
* Check if this view can be scrolled vertically in a certain direction.
*
* @param direction Negative to check scrolling up, positive to check scrolling down.
* @return true if this view can be scrolled in the specified direction, false otherwise.
*/
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;
}
}
这里边主要调用了三个方法,分别是computeVerticalScrollOffset()
表示纵向滑动的距离,如果距离为0,表示已经滑动到了顶部,computeVerticalScrollRange()
指的是整体高度,包括所显示区域和屏幕区域外的高度,computeVerticalScrollExtent()
表示的是显示区域的高度。
问题出现的原因
当滑动到顶部的时候,computeVerticalScrollOffset()
返回的高度一直是大于0的数,那么canScrollVertically
就一致返回true,表示还没有滑动到顶部。接下来我们RecyclerView中方法computeVerticalScrollOffset()
的实现。�RecyclerView这个方法的实现是直接调用LayoutManager的方法computeVerticalScrollOffset
,我使用的LinearLayoutManager,所以直接看这个里边方法的实现。代码如下:
@Override
public int computeVerticalScrollOffset(RecyclerView.State state) {
return computeScrollOffset(state);
}
这里又直接调用的computeScrollOffset
方法
private int computeScrollOffset(RecyclerView.State state) {
if (getChildCount() == 0) {
return 0;
}
ensureLayoutState();
return ScrollbarHelper.computeScrollOffset(state, mOrientationHelper,
findFirstVisibleChildClosestToStart(!mSmoothScrollbarEnabled, true),
findFirstVisibleChildClosestToEnd(!mSmoothScrollbarEnabled, true),
this, mSmoothScrollbarEnabled, mShouldReverseLayout);
}
这里在主要看一下findFirstVisibleChildClosestToStart
方法,这个方法主要是用来获取屏幕中第一个可见的childView,找到并返回这个childView。
/**
* Convenience method to find the visible child closes to start. Caller should check if it has
* enough children.
*
* @param completelyVisible Whether child should be completely visible or not
* @return The first visible child closest to start of the layout from user's perspective.
*/
private View findFirstVisibleChildClosestToStart(boolean completelyVisible,
boolean acceptPartiallyVisible) {
if (mShouldReverseLayout) {
return findOneVisibleChild(getChildCount() - 1, -1, completelyVisible,
acceptPartiallyVisible);
} else {
return findOneVisibleChild(0, getChildCount(), completelyVisible,
acceptPartiallyVisible);
}
}
下边是查找第一个可见childView的详细代码,看完下边的代码就发现,如果第一个childView的高度为0的时候,会继续往下查找,找到第一个不为0的childView并返回。
View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
boolean acceptPartiallyVisible) {
ensureLayoutState();
final int start = mOrientationHelper.getStartAfterPadding();
final int end = mOrientationHelper.getEndAfterPadding();
final int next = toIndex > fromIndex ? 1 : -1;
View partiallyVisible = null;
for (int i = fromIndex; i != toIndex; i += next) {
final View child = getChildAt(i);
final int childStart = mOrientationHelper.getDecoratedStart(child);
final int childEnd = mOrientationHelper.getDecoratedEnd(child);
//如果第一个childView高度为0,childStart和childEnd都等于0;下边的条件不成立,会继续往下边寻找。
if (childStart < end && childEnd > start) {
if (completelyVisible) {
if (childStart >= start && childEnd <= end) {
return child;
} else if (acceptPartiallyVisible && partiallyVisible == null) {
partiallyVisible = child;
}
} else {
return child;
}
}
}
return partiallyVisible;
}
computeVerticalScrollOffset()
的最终返回值来自这里
/**
* @param startChild View closest to start of the list. (top or left)
* @param endChild View closest to end of the list (bottom or right)
*/
static int computeScrollOffset(RecyclerView.State state, OrientationHelper orientation,
View startChild, View endChild, RecyclerView.LayoutManager lm,
boolean smoothScrollbarEnabled, boolean reverseLayout) {
if (lm.getChildCount() == 0 || state.getItemCount() == 0 || startChild == null ||
endChild == null) {
return 0;
}
//获取第一个高度不为0的childView的position,如果RecylerView的第一个item高度为0,那么minPosition的值肯定大于0
final int minPosition = Math.min(lm.getPosition(startChild),
lm.getPosition(endChild));
final int maxPosition = Math.max(lm.getPosition(startChild),
lm.getPosition(endChild));
//reverseLayout默认是false,所以itemBefore基本上就是等于minPosition的值
final int itemsBefore = reverseLayout
? Math.max(0, state.getItemCount() - maxPosition - 1)
: Math.max(0, minPosition);
if (!smoothScrollbarEnabled) {
return itemsBefore;
}
final int laidOutArea = Math.abs(orientation.getDecoratedEnd(endChild) -
orientation.getDecoratedStart(startChild));
final int itemRange = Math.abs(lm.getPosition(startChild) -
lm.getPosition(endChild)) + 1;
final float avgSizePerRow = (float) laidOutArea / itemRange;
//通过返回的值,如果minPosition != 0,返回值都大于0。
return Math.round(itemsBefore * avgSizePerRow + (orientation.getStartAfterPadding()
- orientation.getDecoratedStart(startChild)));
}
解决方案
要解决这个这个问题,需要将第一个item的高度设置为1像素就可以啦。