關于RecyclerView canScrollVertically方法,該方法為View的方法,對RecyclerView來講存在以下bug
/**
* Check if this view can be scrolled vertically in a certain direction.
* 檢查當前view在某個確定的方向能否垂直滾動
* @param direction Negative to check scrolling up, positive to check scrolling down.
* direction參數(shù):負值表示向上滾動,正值表示向下滾動
* @return true if this view can be scrolled in the specified direction, false otherwise.
* 返回值:true 表示在特定方向可以滾動
*/
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;
}
}
正確理解
@param direction Negative to check scrolling up, positive to check scrolling down.
實驗證明direction傳入負值表示View能否向下移動,與官方注釋相反。官方表達的意思應該是check scrolling at up,傳入負值表示View的頂部可否移動。當
RecyclerView第一項是:空布局、Gone、高度為0等情況,都會導致canScrollVertically(int)方法始終返回true。
解決方法:設置第一項最小高度大于0dp