Android listview滞后于复杂的布局

时间:2013-12-23 09:40:33

标签: android performance listview

就像标题所说,我创建列表视图,每个项目显示图像,三行文本和一个按钮,但视图是如此滞后,即使在三星galaxy mega双核心,这里我的项目视图xml

<RelativeLayout
    android:gravity="center_vertical"
    android:padding="5.0dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:listPreferredItemHeight"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:id="@+id/compositeListViewItemThumbnailImage"
        android:layout_width="60.0dip"
        android:layout_height="60.0dip"
        android:src="@drawable/ic_launcher"
        android:scaleType="fitCenter"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:contentDescription="@string/_empty_strings" />
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/compositeListViewItemFirstLinearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/compositeListViewItemThumbnailImage"
        android:layout_alignTop="@+id/compositeListViewItemThumbnailImage"
        android:layout_alignBottom="@+id/compositeListViewItemThumbnailImage"
        android:layout_alignParentRight="true">
        <TextView
            android:textSize="16.0sp"
            android:gravity="center_vertical"
            android:id="@+id/compositeListViewItemFirstLineText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:lines="1"
            android:inputType="textNoSuggestions" />
        <TextView
            android:textSize="12.0sp"
            android:id="@+id/compositeListViewItemSecondLineText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:lines="1"
            android:inputType="textNoSuggestions" />
        <TextView
            android:textSize="14.0sp"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:lines="1"
            android:layout_gravity="center_vertical"
            android:id="@+id/compositeListViewItemThirdLineText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            android:inputType="textNoSuggestions" />
    </LinearLayout>
    <Button
        android:id="@+id/compositeListViewItemActionButton"
        android:paddingLeft="5.0dip"
        android:paddingTop="5.0dip"
        android:paddingRight="5.0dip"
        android:paddingBottom="5.0dip"
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="30.0dip"
        android:layout_marginTop="30.0dip"
        android:layout_marginRight="0.0dip"
        android:text="@string/hello_world"
        android:layout_alignParentRight="true"
        style="?android:attr/buttonStyleSmall" />
</RelativeLayout>

因为它可能/可能不受适配器视图的影响,我也把它放在这里

public class SimpleAdapter extends ArrayAdapter implements Serializable {
    private ArrayList<?> items;
    private int itemLayout;
    private String[] itemFieldsName;
    private int[] itemElements;
    private Context ctx;

    public SimpleAdapter(Context ctx, ArrayList<?> items, int itemLayout, int[] itemElements, String[] itemFieldsName) {
        super(ctx, itemLayout, items);
        this.items = items;
        this.itemElements = itemElements;
        this.itemLayout = itemLayout;
        this.itemFieldsName = itemFieldsName;
        this.ctx = ctx;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if (itemView == null) {
            itemView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(this.itemLayout, null);
        }
        Object item = this.items.get(position);
        for (int i = 0; i < this.itemElements.length; i++) {
            Object value = getValue(item, Character.toUpperCase(this.itemFieldsName[i].charAt(0)) + this.itemFieldsName[i].substring(1));
            View elementView = null;
            if (itemView != null) {
                elementView = itemView.findViewById(this.itemElements[i]);
                if ((elementView instanceof TextView)) {
                    ((TextView) elementView).setText(value.toString());
                } else if ((elementView instanceof SmartImageView)) {
                    ((SmartImageView) elementView).setImageUrl(value.toString());
                } else if ((elementView instanceof Button)) {
                    ((Button) elementView).setText(value.toString());
                }
            }
        }
        return itemView;
    }

    private Object getValue(Object obj, String fieldName) {
        ArrayList<String> fieldsName = new ArrayList<String>();
        int length;
        String str = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
        Object value = new Object();
        try {
            value = obj.getClass().getMethod("get" + str, new Class[0]).invoke(obj);
        } catch (Exception ex) {
            value = fieldName;
        }
        return value;
    }

    private String trimString(String string, int length, boolean soft) {
        if(string == null || string.trim().isEmpty()){
            return string;
        }
        StringBuffer sb = new StringBuffer(string);
        int actualLength = length - 3;
        if(sb.length() > actualLength){
            if(!soft)
                return sb.insert(actualLength, "...").substring(0, actualLength+3);
            else {
                int endIndex = sb.indexOf(" ",actualLength);
                return sb.insert(endIndex,"...").substring(0, endIndex+3);
            }
        }
        return string;
    }
}

有人可以告诉我哪里错了吗?

告诉我您是否需要更多解释/代码资源

2 个答案:

答案 0 :(得分:3)

在充气布局中查找内部视图是Android中最常见的操作之一。这通常通过名为 findViewById()的View方法完成。此方法将以递归方式通过视图树查找具有给定IDcode的子项。在静态UI布局上使用 findViewById()是完全正常的,但正如您所见,ListView在滚动时非常频繁地调用适配器的 getView() findViewById()可能会在ListViews中实现滚动性能 - 特别是如果您的行布局非常重要。

查看持有人模式是关于减少适配器 getView() findViewById()调用的次数。在实践中,View Holder是一个轻量级内部类,它直接引用一行中的所有内部视图。在充气后将其作为标签存储在行的视图中。这样,您只需在首次创建布局时使用 findViewById()。这是应用了View Holder模式的代码示例:

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
    convertView = mInflater.inflate(R.layout.your_layout, null);

    holder = new ViewHolder();
    holder.text = (TextView) convertView.findViewById(R.id.text);

    convertView.setTag(holder);
} else {
    holder = convertView.getTag();
}

holder.text.setText("Position " + position);

return convertView;

}

private static class ViewHolder {
public TextView text;

}

答案 1 :(得分:0)

android有一个已知问题:singleLine,它大大地杀死了ListViews上的性能。请参阅此问题Why does android:singleLine="true" make ListView scrolling very laggy?

从布局中删除它并改为使用maxLines。