自定义列表视图项中的动态度量文本,OnGlobalLayoutListener 仅调用一次
Dynamic measure text in a a custom listview item, OnGlobalLayoutListener only called once
我在我的应用程序中显示一个带有自定义项目的 ListView。自从我使用 android-swipelistview from 47deg found here.
以来,自定义项目已经做了很多工作
因此列表项的 "front" 部分是一个 LinearLayout,其中嵌套了各种位 - 左侧和右侧的图标以及中间的 3 个不同的 TextView - 名称、地址和注释。每个TextView都有不同的文本大小,但是整个列表项的高度是固定的,所以列表看起来相当统一,
中间的一项——地址——给我带来了一些麻烦。基本上我想确保它适合并且看起来不错。我已经设置 space 足以让它能够占用 2 行,然后将其椭圆化。但是,如果地址很短,那么它就全部放在一行中,我在注释行之前有一个相当大的 space 看起来很糟糕。
所以我想我会对文本做一些分析 - 如果它比一行短,我会在最后一个逗号之后将其断开(我的地址总是有逗号)所以我填满了两行。
我的适配器中有一些这样的代码
private class MyViewHolder {
ImageView defaultLogo;
TextView name;
TextView address;
TextView notes;
ImageView otherLogo;
}
.
.
.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
MyViewHolder holder = null;
if (convertView == null) {
.
.
holder = new MyViewHolder();
.
.
.
holder.address = (TextView) convertView.findViewById(R.id.street_address);
.
.
.
else {
holder = (MyViewHolder) convertView.getTag();
}
.
.
.
MyItem item = (MyItem) getItem(position);
// Set address:
doAfterLayout(holder.address, fixLines);
holder.address.setText(item.getAddress());
.
.
.
}
.
.
.
/**
* Runs a piece of code after the layout run
*/
public static void doAfterLayout(final View view, final FixLinesRunnable runnable) {
final OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
runnable.view = view;
runnable.run();
}
};
view.getViewTreeObserver().addOnGlobalLayoutListener(listener);
}
private class FixLinesRunnable implements Runnable {
public View view;
@Override
public void run() {
// try to get the width of this control and the text
TextView tv = (TextView) view;
int lineCount = tv.getLineCount();
if (0 == lineCount) {
return;FixLinesRunnable
} else if (1 < lineCount) {
//lineCount over 2 means we leave text as it is
return;
}
//if we got here we have only one line text
//want to try to force it to be 2 lines by breaking at last comma
String text = tv.getText().toString();
int lastCommaPos = text.lastIndexOf(", ");
if (lastCommaPos > text.length() - 1) {
//comma is right at the end
lastCommaPos = text.lastIndexOf(", ",lastCommaPos);
}
if (0 < lastCommaPos) {
String secondLine = text.substring(lastCommaPos + 2);
text = text.substring(0, lastCommaPos + 1) + "\n" + secondLine;
}
tv.setText(text);
}
这真的很有效。第一次显示列表时它工作正常 - 太短的地址被推入 2 行,在最后一个逗号处断开。但是,如果我将该项目从视图中滚动出来,然后再滚动到视图中,它就不起作用了……可能出了什么问题?
原先的doAfterLayout函数会在第一次调整后去掉OnGlobalLayoutListener,但即使OnGlobalLayoutListener还在,item重新出现时也不会调用第二次,所以文本显示在一行?
有人有什么想法吗?
编辑:烦人的是,如果有什么东西覆盖了列表(我有另一个 window 从侧面拉开并覆盖了一些项目)可见项目重绘......我什至可以在它们被重绘之前看到它涵盖...
感谢@Daniel-Benedykt 为我指明了正确的方向。
最后我继承了TextView。我发现我唯一可以覆盖的地方是每次显示视图时都有效的地方是 onDraw ...所以:
public class AddressTextView extends TextView {
public AddressTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw (Canvas canvas) {
int lineCount = getLineCount();
if (0 == lineCount) {
super.onDraw(canvas);
return;
} else if (1 < lineCount) {
//lineCount over 2 means we leave text as it is
super.onDraw(canvas);
return;
}
//if we got here we have only one line text
//want to try to force it to be 2 lines by breaking at last comma
String text = getText().toString();
int lastCommaPos = text.lastIndexOf(", ");
if (lastCommaPos > text.length() - 1) {
//comma is right at the end
lastCommaPos = text.lastIndexOf(", ",lastCommaPos);
}
if (0 < lastCommaPos) {
String secondLine = text.substring(lastCommaPos + 2);
text = text.substring(0, lastCommaPos + 1) + "\n" + secondLine;
}
setText(text);
super.onDraw(canvas);
}
}
希望这对某人有所帮助。
我在我的应用程序中显示一个带有自定义项目的 ListView。自从我使用 android-swipelistview from 47deg found here.
以来,自定义项目已经做了很多工作因此列表项的 "front" 部分是一个 LinearLayout,其中嵌套了各种位 - 左侧和右侧的图标以及中间的 3 个不同的 TextView - 名称、地址和注释。每个TextView都有不同的文本大小,但是整个列表项的高度是固定的,所以列表看起来相当统一,
中间的一项——地址——给我带来了一些麻烦。基本上我想确保它适合并且看起来不错。我已经设置 space 足以让它能够占用 2 行,然后将其椭圆化。但是,如果地址很短,那么它就全部放在一行中,我在注释行之前有一个相当大的 space 看起来很糟糕。
所以我想我会对文本做一些分析 - 如果它比一行短,我会在最后一个逗号之后将其断开(我的地址总是有逗号)所以我填满了两行。
我的适配器中有一些这样的代码
private class MyViewHolder {
ImageView defaultLogo;
TextView name;
TextView address;
TextView notes;
ImageView otherLogo;
}
.
.
.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
MyViewHolder holder = null;
if (convertView == null) {
.
.
holder = new MyViewHolder();
.
.
.
holder.address = (TextView) convertView.findViewById(R.id.street_address);
.
.
.
else {
holder = (MyViewHolder) convertView.getTag();
}
.
.
.
MyItem item = (MyItem) getItem(position);
// Set address:
doAfterLayout(holder.address, fixLines);
holder.address.setText(item.getAddress());
.
.
.
}
.
.
.
/**
* Runs a piece of code after the layout run
*/
public static void doAfterLayout(final View view, final FixLinesRunnable runnable) {
final OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
runnable.view = view;
runnable.run();
}
};
view.getViewTreeObserver().addOnGlobalLayoutListener(listener);
}
private class FixLinesRunnable implements Runnable {
public View view;
@Override
public void run() {
// try to get the width of this control and the text
TextView tv = (TextView) view;
int lineCount = tv.getLineCount();
if (0 == lineCount) {
return;FixLinesRunnable
} else if (1 < lineCount) {
//lineCount over 2 means we leave text as it is
return;
}
//if we got here we have only one line text
//want to try to force it to be 2 lines by breaking at last comma
String text = tv.getText().toString();
int lastCommaPos = text.lastIndexOf(", ");
if (lastCommaPos > text.length() - 1) {
//comma is right at the end
lastCommaPos = text.lastIndexOf(", ",lastCommaPos);
}
if (0 < lastCommaPos) {
String secondLine = text.substring(lastCommaPos + 2);
text = text.substring(0, lastCommaPos + 1) + "\n" + secondLine;
}
tv.setText(text);
}
这真的很有效。第一次显示列表时它工作正常 - 太短的地址被推入 2 行,在最后一个逗号处断开。但是,如果我将该项目从视图中滚动出来,然后再滚动到视图中,它就不起作用了……可能出了什么问题?
原先的doAfterLayout函数会在第一次调整后去掉OnGlobalLayoutListener,但即使OnGlobalLayoutListener还在,item重新出现时也不会调用第二次,所以文本显示在一行?
有人有什么想法吗?
编辑:烦人的是,如果有什么东西覆盖了列表(我有另一个 window 从侧面拉开并覆盖了一些项目)可见项目重绘......我什至可以在它们被重绘之前看到它涵盖...
感谢@Daniel-Benedykt 为我指明了正确的方向。
最后我继承了TextView。我发现我唯一可以覆盖的地方是每次显示视图时都有效的地方是 onDraw ...所以:
public class AddressTextView extends TextView {
public AddressTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw (Canvas canvas) {
int lineCount = getLineCount();
if (0 == lineCount) {
super.onDraw(canvas);
return;
} else if (1 < lineCount) {
//lineCount over 2 means we leave text as it is
super.onDraw(canvas);
return;
}
//if we got here we have only one line text
//want to try to force it to be 2 lines by breaking at last comma
String text = getText().toString();
int lastCommaPos = text.lastIndexOf(", ");
if (lastCommaPos > text.length() - 1) {
//comma is right at the end
lastCommaPos = text.lastIndexOf(", ",lastCommaPos);
}
if (0 < lastCommaPos) {
String secondLine = text.substring(lastCommaPos + 2);
text = text.substring(0, lastCommaPos + 1) + "\n" + secondLine;
}
setText(text);
super.onDraw(canvas);
}
}
希望这对某人有所帮助。