码迷,mamicode.com
首页 > 移动开发 > 详细

android textview settext卡顿深层次原因

时间:2015-05-04 23:47:21      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:

最近在公司项目里面发现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所耗时间变短,就是这样。

android textview settext卡顿深层次原因

标签:

原文地址:http://www.cnblogs.com/zmc/p/4477961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!