我有一个稍微复杂的布局,包含几个自定义视图,每个自定义视图计算他们需要多少空间和诸如此类的东西。在这个布局的顶部,我有一个TextView,应该从说... 02:04:20(hh:mm:ss)倒计时,并每秒更新。
我的问题是没有更新文本,问题是当我更新文本时,我的布局中的所有视图都被重新绘制..而且,我在这个布局中使用了一个库,所以在交互时在更新文本时使用图库使图库立即切换到您选择的位置(即使您正在切换到图库中的新项目)。
那么..如何在不重新绘制其他视图的情况下更新textview?
答案 0 :(得分:7)
如果视图的宽度是固定的并且视图的高度不会改变,则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();
}
}