RecyclerView的position==0位置上的View高度为0时,canScrollVertically方法无法判断是否已经滑动到顶部?
看View中canScrollVertically方法的调用:
/** * Check if this view can be scrolled vertically in a certain direction. * *@paramdirection Negative to check scrolling up, positive to check scrolling down. *@returntrue if this view can be scrolled in the specified direction, false otherwise. */publicbooleancanScrollVertically(intdirection){finalintoffset = computeVerticalScrollOffset();finalintrange = computeVerticalScrollRange() - computeVerticalScrollExtent();if(range ==0)returnfalse;if(direction <0) {returnoffset >0; }else{returnoffset < range -1; } }
这里边主要调用了三个方法,分别是computeVerticalScrollOffset()表示纵向滑动的距离,如果距离为0,表示已经滑动到了顶部,computeVerticalScrollRange()指的是整体高度,包括所显示区域和屏幕区域外的高度,computeVerticalScrollExtent()表示的是显示区域的高度。
问题出现的原因
当滑动到顶部的时候,computeVerticalScrollOffset()返回的高度一直是大于0的数,那么canScrollVertically就一致返回true,表示还没有滑动到顶部。接下来我们RecyclerView中方法computeVerticalScrollOffset()的实现。�RecyclerView这个方法的实现是直接调用LayoutManager的方法computeVerticalScrollOffset,我使用的LinearLayoutManager,所以直接看这个里边方法的实现。代码如下:
@OverridepublicintcomputeVerticalScrollOffset(RecyclerView.State state){returncomputeScrollOffset(state); }
这里又直接调用的computeScrollOffset方法
privateintcomputeScrollOffset(RecyclerView.State state){if(getChildCount() ==0) {return0; } ensureLayoutState();returnScrollbarHelper.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. * *@paramcompletelyVisible Whether child should be completely visible or not *@returnThe first visible child closest to start of the layout from user's perspective. */privateViewfindFirstVisibleChildClosestToStart(booleancompletelyVisible,booleanacceptPartiallyVisible){if(mShouldReverseLayout) {returnfindOneVisibleChild(getChildCount() -1, -1, completelyVisible, acceptPartiallyVisible); }else{returnfindOneVisibleChild(0, getChildCount(), completelyVisible, acceptPartiallyVisible); } }
下边是查找第一个可见childView的详细代码,看完下边的代码就发现,如果第一个childView的高度为0的时候,会继续往下查找,找到第一个不为0的childView并返回。
ViewfindOneVisibleChild(intfromIndex,inttoIndex,booleancompletelyVisible,booleanacceptPartiallyVisible){ ensureLayoutState();finalintstart = mOrientationHelper.getStartAfterPadding();finalintend = mOrientationHelper.getEndAfterPadding();finalintnext = toIndex > fromIndex ?1: -1; View partiallyVisible =null;for(inti = fromIndex; i != toIndex; i += next) {finalView child = getChildAt(i);finalintchildStart = mOrientationHelper.getDecoratedStart(child);finalintchildEnd = mOrientationHelper.getDecoratedEnd(child);//如果第一个childView高度为0,childStart和childEnd都等于0;下边的条件不成立,会继续往下边寻找。if(childStart < end && childEnd > start) {if(completelyVisible) {if(childStart >= start && childEnd <= end) {returnchild; }elseif(acceptPartiallyVisible && partiallyVisible ==null) { partiallyVisible = child; } }else{returnchild; } } }returnpartiallyVisible; }
computeVerticalScrollOffset()的最终返回值来自这里
/** *@paramstartChild View closest to start of the list. (top or left) *@paramendChild View closest to end of the list (bottom or right) */staticintcomputeScrollOffset(RecyclerView.State state, OrientationHelper orientation, View startChild, View endChild, RecyclerView.LayoutManager lm,booleansmoothScrollbarEnabled,booleanreverseLayout){if(lm.getChildCount() ==0|| state.getItemCount() ==0|| startChild ==null|| endChild ==null) {return0; }//获取第一个高度不为0的childView的position,如果RecylerView的第一个item高度为0,那么minPosition的值肯定大于0finalintminPosition = Math.min(lm.getPosition(startChild), lm.getPosition(endChild));finalintmaxPosition = Math.max(lm.getPosition(startChild), lm.getPosition(endChild));//reverseLayout默认是false,所以itemBefore基本上就是等于minPosition的值finalintitemsBefore = reverseLayout ? Math.max(0, state.getItemCount() - maxPosition -1) : Math.max(0, minPosition);if(!smoothScrollbarEnabled) {returnitemsBefore; }finalintlaidOutArea = Math.abs(orientation.getDecoratedEnd(endChild) - orientation.getDecoratedStart(startChild));finalintitemRange = Math.abs(lm.getPosition(startChild) - lm.getPosition(endChild)) +1;finalfloatavgSizePerRow = (float) laidOutArea / itemRange;//通过返回的值,如果minPosition != 0,返回值都大于0。returnMath.round(itemsBefore * avgSizePerRow + (orientation.getStartAfterPadding() - orientation.getDecoratedStart(startChild))); }
解决方案
要解决这个这个问题,需要将第一个item的高度设置为1像素就可以啦。
作者:哦就行
链接:https://www.jianshu.com/p/475b3df04505
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。