每秒设置textview的文本时,防止除textview之外的其他视图更新/重绘

发布于 2025-01-07 05:25:38 字数 253 浏览 3 评论 0原文

我有一个稍微复杂的布局,包含几个自定义视图,每个视图计算它们需要多少空间等等。在这个布局的顶部,我有一个 TextView,它应该从 02:04:20 (hh:mm:ss) 开始倒计时,并每秒更新一次。

我的问题不是更新文本,问题是当我更新文本时,布局中的所有视图都会重新绘制。此外,我在此布局中使用画廊,因此在与画廊交互时当文本更新时,图库会立即切换到您选择的位置(即使您正在切换到图库中的新项目)。

那么..如何更新textview而不使其他视图重新绘制?

I've got a slightly complicated layout, containing several custom-views that each calculate how much space they need and whatnot. At the top of this layout, I've got a TextView which is supposed to count down from say.. 02:04:20 (hh:mm:ss), and update every second.

My problem is not updating the text, the problem is that when I DO update the text, ALL of the views in my layout gets re-drawn.. Also, I'm using a gallery in this layout, so when interacting with the gallery while the text is updated makes the gallery switch INSTANTLY to the place you've selected (even though you're in the middle of switching to a new item in the gallery).

So.. How to update the textview without making other views re-draw?

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

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

发布评论

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

评论(1

高冷爸爸 2025-01-14 05:25:38

如果视图的宽度是固定的并且视图的高度不会改变,那么 TextView 不会使 setText() 上的整个布局无效。将布局参数 android:layout_width"wrap_content" 更改为固定宽度,例如 "fill_parent""100sp"

来源

/**
 * Check whether entirely new text requires a new view layout
 * or merely a new text layout.
 */

private void checkForRelayout() {
    // If we have a fixed width, we can just swap in a new text layout
    // if the text height stays the same or if the view height is fixed.

    if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT ||
            (mMaxWidthMode == mMinWidthMode && mMaxWidth == mMinWidth)) &&
            (mHint == null || mHintLayout != null) &&
            (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight() > 0)) {
        // Static width, so try making a new text layout.

        int oldht = mLayout.getHeight();
        int want = mLayout.getWidth();
        int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth();

        /*
         * No need to bring the text into view, since the size is not
         * changing (unless we do the requestLayout(), in which case it
         * will happen at measure).
         */
        makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING,
                      mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(),
                      false);

        if (mEllipsize != TextUtils.TruncateAt.MARQUEE) {
            // In a fixed-height view, so use our new text layout.
            if (mLayoutParams.height != LayoutParams.WRAP_CONTENT &&
                mLayoutParams.height != LayoutParams.MATCH_PARENT) {
                invalidate();
                return;
            }

            // Dynamic height, but height has stayed the same,
            // so use our new text layout.
            if (mLayout.getHeight() == oldht &&
                (mHintLayout == null || mHintLayout.getHeight() == oldht)) {
                invalidate();
                return;
            }
        }

        // We lose: the height has changed and we have a dynamic height.
        // Request a new view layout using our new text layout.
        requestLayout();
        invalidate();
    } else {
        // Dynamic width, so we have no choice but to request a new
        // view layout with a new text layout.

        nullLayouts();
        requestLayout();
        invalidate();
    }
}

The TextView won't invalidate the entire layout on setText() if the view's width is fixed and the height of the view won't change. Change the layout parameter android:layout_width from "wrap_content" to a fixed width like "fill_parent" or "100sp".

Source:

/**
 * Check whether entirely new text requires a new view layout
 * or merely a new text layout.
 */

private void checkForRelayout() {
    // If we have a fixed width, we can just swap in a new text layout
    // if the text height stays the same or if the view height is fixed.

    if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT ||
            (mMaxWidthMode == mMinWidthMode && mMaxWidth == mMinWidth)) &&
            (mHint == null || mHintLayout != null) &&
            (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight() > 0)) {
        // Static width, so try making a new text layout.

        int oldht = mLayout.getHeight();
        int want = mLayout.getWidth();
        int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth();

        /*
         * No need to bring the text into view, since the size is not
         * changing (unless we do the requestLayout(), in which case it
         * will happen at measure).
         */
        makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING,
                      mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(),
                      false);

        if (mEllipsize != TextUtils.TruncateAt.MARQUEE) {
            // In a fixed-height view, so use our new text layout.
            if (mLayoutParams.height != LayoutParams.WRAP_CONTENT &&
                mLayoutParams.height != LayoutParams.MATCH_PARENT) {
                invalidate();
                return;
            }

            // Dynamic height, but height has stayed the same,
            // so use our new text layout.
            if (mLayout.getHeight() == oldht &&
                (mHintLayout == null || mHintLayout.getHeight() == oldht)) {
                invalidate();
                return;
            }
        }

        // We lose: the height has changed and we have a dynamic height.
        // Request a new view layout using our new text layout.
        requestLayout();
        invalidate();
    } else {
        // Dynamic width, so we have no choice but to request a new
        // view layout with a new text layout.

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