2019-06-06 RecyclerView canScrollVertically(int direction)

關于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;
        }
    }
  1. 正確理解@param direction Negative to check scrolling up, positive to check scrolling down.
    實驗證明direction傳入負值表示View能否向下移動,與官方注釋相反。官方表達的意思應該是check scrolling at up,傳入負值表示View的頂部可否移動。

  2. RecyclerView第一項是:空布局、Gone、高度為0等情況,都會導致canScrollVertically(int)方法始終返回true。
    解決方法:設置第一項最小高度大于0dp

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容