Android的滚动型衰退边缘边缘、Android

2023-09-06 02:01:18 作者:一天一日 一日一天

默认滚动视图的衰落边缘是可见的,只有当它是可能在该方向上滚动。我怎样才能使它在任何时候都可见?

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.

在此先感谢

推荐答案

是的,滚动型延伸并覆盖这些方法(基于甜甜圈-release2):

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;
}

有关比较的缘故,这是原来的code,从而缩短了衰退的边缘,你去接近列表的末尾:

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;
}