如何让Android ScrollView的淡入淡出边缘始终可见?

发布于 2024-11-27 07:55:58 字数 115 浏览 0 评论 0原文

默认情况下,滚动视图的淡入淡出边缘仅在可以沿该方向滚动时才可见。如何才能使其始终可见?

我不想在上面放任何可绘制的东西或类似的东西。我想使用内置的淡入淡出边缘来完成它,可能是通过覆盖一些滚动视图函数。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2024-12-04 07:55:58

是的,扩展 ScrollView 并重写这些方法(基于 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;
}

为了进行比较,这是原始代码,当您接近列表末尾时,它会缩短淡入淡出的边缘:

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

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;
}
苍景流年 2024-12-04 07:55:58

您还可以使用以下代码:

public class TopFadeEdgeScrollView extends ScrollView {

public TopFadeEdgeScrollView(Context context) {
    super(context);
}

public TopFadeEdgeScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TopFadeEdgeScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected float getBottomFadingEdgeStrength() {
    return 0.0f;
}
}

You can also use the following code:

public class TopFadeEdgeScrollView extends ScrollView {

public TopFadeEdgeScrollView(Context context) {
    super(context);
}

public TopFadeEdgeScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TopFadeEdgeScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected float getBottomFadingEdgeStrength() {
    return 0.0f;
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文