3

我的 ListView 有 ArrayAdapter。它有 TextView 和箭头图像。如果 TextView 有 3 行或更多行,我必须显示箭头图像,但如果行数 < 3 必须隐藏箭头。但实际上,在绘制 TextView 之前,Adapter 并没有他们的行数。有任何想法吗?我是否需要显示带有箭头的项目,取决于行数。

此代码不起作用(TextView 仅在绘制后接收行数)

if(holder.text.getLineCount() < 3)
{
        holder.arrow.setVisibility(View.GONE);
}
else
{
        holder.arrow.setVisibility(View.VISIBLE);
}
4

1 回答 1

0

您需要将代码的执行延迟到文本更新之后。尝试为此使用TextView::addTextChangedListener()事件:

holder.text.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable target) {

        if(holder.text.getLineCount() < 3) {
             holder.arrow.setVisibility(View.GONE);
        }
        else {
             holder.arrow.setVisibility(View.VISIBLE);
        }

    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
});
于 2012-05-01T12:44:13.187 回答