问题
开发中,经常会用到TextView作为计时用,通过handle或者其他线程,每隔1s刷新一下时间。看一下简单的代码实现:
根布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".tmp.TempActivity">
<com.sollian.sourcestudy.tmp.MyRecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MyRecyclerView
public class MyRecyclerView extends RecyclerView {
public MyRecyclerView(@NonNull Context context) {
super(context);
}
public MyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
Log.d("MyRecyclerView", "onMeasure");
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
Log.d("MyRecyclerView", "onLayout");
}
}
很简单,在每次测量、布局时,打个log。
item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
</LinearLayout>
在item中通过一个TextView来计数。在代码中,通过handle每隔1s使TextView的计数加1。此时看一下log输出:
2019-01-28 15:04:55.481 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onMeasure
2019-01-28 15:04:55.535 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onMeasure
2019-01-28 15:04:55.536 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onLayout
2019-01-28 15:04:55.657 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onMeasure
2019-01-28 15:04:55.657 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onLayout
2019-01-28 15:04:56.656 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onMeasure
2019-01-28 15:04:56.658 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onLayout
2019-01-28 15:04:57.656 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onMeasure
2019-01-28 15:04:57.658 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onLayout
2019-01-28 15:04:58.655 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onMeasure
2019-01-28 15:04:58.656 8025-8025/com.sollian.sourcestudy D/MyRecyclerView: onLayout
...
可以看到,TextView没刷新一次,都会引起RecyclerView重新测量、布局。
现在修改一下,将TextView的宽度固定为100dp。再看日志:
2019-01-28 15:05:48.257 8134-8134/com.sollian.sourcestudy D/MyRecyclerView: onMeasure
2019-01-28 15:05:48.294 8134-8134/com.sollian.sourcestudy D/MyRecyclerView: onMeasure
2019-01-28 15:05:48.295 8134-8134/com.sollian.sourcestudy D/MyRecyclerView: onLayout
可以看到,现在不会再引起RecyclerView的频繁测量、布局了。
原因
为什么会造成这种差异呢?我们来追一下setText
方法的源码。
api28 TextView源码:
private void setText(CharSequence text, BufferType type,
boolean notifyBefore, int oldlen) {
...
if (mLayout != null) {
checkForRelayout();
}
...
}
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) {
autoSizeText();
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)) {
autoSizeText();
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();
}
}
注意看checkForRelayout
方法,在宽度固定,并且mEllipsize != TextUtils.TruncateAt.MARQUEE
时,只调用invalidate()
进行重绘;否则会先调用requestLayout()
进行重新布局,然后再调用invalidate()
重绘。
结论
在需要频繁刷新TextView时,尽量不要使用wrapt_content
来设置宽度,避免频繁引起测量和布局。
网友评论