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参数:负值表示向上滚动,正值表示向下滚动
     * @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

你可能感兴趣的:(2019-06-06 RecyclerView canScrollVertically(int direction))