Prevent other views than textview to update/redraw when setting text of textview every second

你说的曾经没有我的故事 提交于 2019-12-22 18:46:43

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/9375205/prevent-other-views-than-textview-to-update-redraw-when-setting-text-of-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!