android textview settext卡顿深层次原因

最近在公司项目里面发现listview里面的textview在调用settext函数的时候非常耗时,当时都有点不敢相信,这是因为如果你把textview设置成wrap_content,则每次调用settext之后会调用到

........40334034            if (mMovement != null) {4035                mMovement.initialize(this, (Spannable) text);40364037                /*4038                 * Initializing the movement method will have set the4039                 * selection, so reset mSelectionMoved to keep that from4040                 * interfering with the normal on-focus selection-setting.4041                 */4042                if (mEditor != null) mEditor.mSelectionMoved = false;4043            }4044        }40454046        if (mLayout != null) {
                //这个函数非常重要4047            checkForRelayout();4048        }40494050        sendOnTextChanged(text, 0, oldlen, textLength);4051        onTextChanged(text, 0, oldlen, textLength);40524053        notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT);4054
.........
checkForRelayout函数,这个函数根据文字的多少重新开始布局

 

 

6807    private void More ...checkForRelayout() {6808        // If we have a fixed width, we can just swap in a new text layout6809        // if the text height stays the same or if the view height is fixed.68106811        if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT ||6812                (mMaxWidthMode == mMinWidthMode && mMaxWidth == mMinWidth)) &&6813                (mHint == null || mHintLayout != null) &&6814                (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight() > 0)) {6815            // Static width, so try making a new text layout.68166817            int oldht = mLayout.getHeight();6818            int want = mLayout.getWidth();6819            int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth();68206821            /*6822             * No need to bring the text into view, since the size is not6823             * changing (unless we do the requestLayout(), in which case it6824             * will happen at measure).6825             */6826            makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING,6827                          mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(),6828                          false);68296830            if (mEllipsize != TextUtils.TruncateAt.MARQUEE) {6831                // In a fixed-height view, so use our new text layout.6832                if (mLayoutParams.height != LayoutParams.WRAP_CONTENT &&6833                    mLayoutParams.height != LayoutParams.MATCH_PARENT) {6834                    invalidate();6835                    return;6836                }68376838                // Dynamic height, but height has stayed the same,6839                // so use our new text layout.6840                if (mLayout.getHeight() == oldht &&6841                    (mHintLayout == null || mHintLayout.getHeight() == oldht)) {6842                    invalidate();6843                    return;6844                }6845            }68466847            // We lose: the height has changed and we have a dynamic height.6848            // Request a new view layout using our new text layout.6849            requestLayout();6850            invalidate();6851        } else {6852            // Dynamic width, so we have no choice but to request a new6853            // view layout with a new text layout.6854            nullLayouts();6855            requestLayout();6856            invalidate();6857        }6858    }

注释已经写的很明白了,所以宽度设定为一个数字或者match_parent能够使得settext所耗时间变短,就是这样。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。