动态测量自定义列表视图项中的文本,OnGlobalLayoutListener仅调用一次

时间:2015-02-12 15:39:04

标签: android listview android-listview android-adapter

我在我的应用中使用自定义项目显示ListView。自从我使用47deg here android-swipelistview 以来,自定义项目已经做了很多工作。

所以"前面"列表项的一部分是一个LinearLayout,其中嵌入了各种位 - 左侧和右侧的图标以及中间3个不同的TextView - 名称,地址和注释。每个TextView都有不同的文本大小,但整个列表项的高度是固定的,以便列表看起来相当统一,

中间项目 - 地址 - 给我带来了一些麻烦。基本上我想确保它适合并且看起来很好。我已经设置了足够的空间以便能够占用2条线,然后将其椭圆化。然而,如果地址非常短,那么它全部适合一行,并且在音符线看起来很糟糕之前我有一个相当大的空间。

所以我想我会对文本进行一些分析 - 如果它比一行短,我会在最后一个逗号后打破它(我的地址总是有逗号)所以我填写两行。

我的适配器中有一些这样的代码

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仍然存在,当项目重新出现时它也不会第二次调用,所以文本显示在一行?

有人有任何想法吗?

编辑:令人讨厌的是,如果某些内容掩盖了列表(我有另一个窗口从侧面打开并覆盖一些项目)可见项目重绘...我甚至可以在它们被覆盖之前看到它......

1 个答案:

答案 0 :(得分:1)

感谢@ Daniel-Benedykt指出我正确的方向。

最后我将TextView子类化。我找到的唯一一个可以覆盖的地方,每次显示视图时都可以使用onDraw ... so:

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);
}


}

希望这有助于某人。