1

我的活动中有自定义 ListView,它使用 ConvertView 模式和 ViewHolder。一切正常,但有时项目中的文本会被截断。这在屏幕截图中清晰可见:

截屏

看起来它重用了旧视图并且不更新文本长度。这是适配器代码的一部分:

if (convertView == null) {
     viewHolder = new ViewHolderItemBool();

     LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
     if (changeable) {
         convertView = inflater.inflate(R.layout.sensor_bool_e, null, true);
     } else {
         convertView = inflater.inflate(R.layout.sensor_bool, null, true);
     }

     viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
     viewHolder.txtDesc = (TextView) convertView.findViewById(R.id.txtDescript);
     viewHolder.imgState = (ImageView) convertView.findViewById(R.id.img);
     convertView.setTag(viewHolder);
} else {
     viewHolder = (ViewHolderItemBool) convertView.getTag();
}
viewHolder.txtName.setText(name);
if (isImportant()) viewHolder.txtName.setTypeface(null, Typeface.BOLD);
//some code to change description field and picture
return convertView;

这是项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sensor_bool"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:background="@drawable/sensor_background"
    android:longClickable="true">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/circle_green"
        android:padding="10dp"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txtName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/sensor_text_size"
            android:singleLine="true"
            android:textColor="@color/sensor_name_color"/>

        <TextView
            android:id="@+id/txtDescript"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingStart="2dp"
            android:textSize="@dimen/sensor_desc_size"
            android:textColor="@android:color/secondary_text_light"/>
    </LinearLayout>
</LinearLayout>

如果我每次都不使用 ConverView 并膨胀布局,它看起来还不错。有什么想法可以解决这个问题吗?

4

2 回答 2

0

您需要使用 BaseAdapter 的一些方法来显示不同的列表视图项:

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    if (changeable) {
        return 0;
    } else {
        return 1;
    }
}
于 2014-06-27T09:33:00.220 回答
0

我做到了!这只是一些愚蠢的错误。

第一:我变了

android:layout_width="fill_parent"

在 txtName 中

android:layout_width="wrap_content"

这解决了文本宽度的问题。我不明白为什么它以前不起作用?

第二:

if (isImportant()) viewHolder.txtName.setTypeface(null, Typeface.BOLD);

if (isImportant()) {
    viewHolder.txtName.setTypeface(null, Typeface.BOLD);
} else {
    viewHolder.txtName.setTypeface(null, Typeface.NORMAL);
}

这解决了粗体文本的问题。(问题没有在问题中描述,但在屏幕上可以看到。)

感谢大家的帮助!

于 2014-06-27T10:06:09.677 回答