Android ScrollView fading edge

2019-01-25 11:50发布

问题:

by default scrollview's fading edge is visible only if it is possible to scroll in that direction. How can I make it visible at all times?

I don't want to put any drawables on top or something like that. I want to accomplish it using the builtin fading edge, probably by overriding some scrollview functions.

Thanks in advance

回答1:

Yes, extend ScrollView and override these methods (based on Donut-release2):

@Override
protected float getTopFadingEdgeStrength() {
    if (getChildCount() == 0) {
        return 0.0f;
    }
    return 1.0f;
}

@Override
protected float getBottomFadingEdgeStrength() {
    if (getChildCount() == 0) {
        return 0.0f;
    }
    return 1.0f;
}

For comparison's sake, this is the original code, which shortens the fading edge as you get close to the end of the list:

@Override
protected float getTopFadingEdgeStrength() {
    if (getChildCount() == 0) {
        return 0.0f;
    }

    final int length = getVerticalFadingEdgeLength();
    if (mScrollY < length) {
        return mScrollY / (float) length;
    }

    return 1.0f;
}

@Override
protected float getBottomFadingEdgeStrength() {
    if (getChildCount() == 0) {
        return 0.0f;
    }

    final int length = getVerticalFadingEdgeLength();
    final int bottomEdge = getHeight() - mPaddingBottom;
    final int span = getChildAt(0).getBottom() - mScrollY - bottomEdge;
    if (span < length) {
        return span / (float) length;
    }

    return 1.0f;
}