Search code examples
android-listviewandroid-viewandroid-overscoll

Default overScrollMode value in Android View


Per Android documentation, the default value for overScrollMode is OVER_SCROLL_ALWAYS.

But my ListView did not seem to follow this behavior. It only shows overscrolling behavior when there is enough content to scroll in the list.

I tried to look into the Android code and found that the overScrollMode is set to OVER_SCROLL_IF_CONTENT_SCROLLS in View.java ref

I also checked the code for ListView and AbsListView to check if the overscrollmode was being set anywhere but I couldn't find anything. The only place where overScrollMode is set is in View.java.

Does this mean that the Android documentation for this is incorrect? Do I have to explicitly set overScrollMode to 'always' in my list view?


Solution

  • Yes, the Android documentation is in fact incorrect. I confirmed it. As you can see in the below code from Android source, overscrolling is set to if_content_scrolls by default.

    /**
     * Simple constructor to use when creating a view from code.
     *
     * @param context The Context the view is running in, through which it can
     *        access the current theme, resources, etc.
     */
    public View(Context context) {
        mContext = context;
        mResources = context != null ? context.getResources() : null;
        mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
        // Set some flags defaults
        mPrivateFlags2 =
            (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
            (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
            (PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT) |
            (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
            (PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT) |
            (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
        mUserPaddingStart = UNDEFINED_PADDING;
        mUserPaddingEnd = UNDEFINED_PADDING;
    
        if (!sCompatibilityDone && context != null) {
            final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
    
            // Older apps may need this compatibility hack for measurement.
            sUseBrokenMakeMeasureSpec = targetSdkVersion <= JELLY_BEAN_MR1;
    
            // Older apps expect onMeasure() to always be called on a layout pass, regardless
            // of whether a layout was requested on that View.
            sIgnoreMeasureCache = targetSdkVersion < KITKAT;
    
            sCompatibilityDone = true;
        }
    }