簡體   English   中英

如何提高Android ListView的性能

[英]How to improve performance on Android ListView

我的ListView加載速度非常慢。 我的應用程序中有多個選項卡,每次切換到ListView選項卡都需要很長時間(〜1-1.5秒)。

我的適配器的getView()方法:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        convertView = LayoutInflater.from(ctx).inflate(R.layout.gallery_list_item, parent, false);
        holder = new ViewHolder();
        holder.image = (ImageView) convertView.findViewById(R.id.image);
        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.date = (TextView) convertView.findViewById(R.id.date);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    bindItems(position, holder);
    ImageLoader.getInstance().displayImage(Uri.fromFile(new File(documents.get(position).getPath())).toString(), holder.image, options, animateFirstListener);
    return convertView;
}

@Background
void bindItems(final int position, final ViewHolder holder) {
    holder.date.setTypeface(MainApplication.OpenSans(ctx));
    holder.text.setTypeface(MainApplication.OpenSansBold(ctx));

    SimpleDateFormat date = new SimpleDateFormat("dd.MM.yyyy");
    SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss");
    holder.date.setText(date.format(documents.get(position).getCreateDate()) + "  " + time.format(documents.get(position).getCreateDate()));

    holder.text.setText(documents.get(position).getDocument());
}

我的gallery_list_layout.xml一部分(除了列表之外,還顯示一些與我的問題無關的按鈕和微調器):

 <ListView
    android:id="@+id/mylist"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ListView>

和我的gallery_list_item.xml (用於一個列表項):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >

<ImageView
    android:id="@+id/image"
    android:layout_width="@dimen/image_width"
    android:layout_height="@dimen/image_height"
    android:layout_centerVertical="true"
    android:contentDescription="@string/app_name"
    android:scaleType="centerCrop" />

<LinearLayout
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/image"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

<CheckBox
    android:id="@+id/chkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true" />
</RelativeLayout>

我省略了一些樣式屬性以使其更具可讀性。

到目前為止,我所做的是:

  • 使用通用圖像加載器,但問題不在於圖像加載(性能大致相同)
  • layout_heightlayout_width上使用match_parent僅加載必要的數據(這可以提高很多性能,但仍然不夠用)
  • 避免在我的Adapter getView()方法中進行任何數據庫查詢
  • 將數據綁定到后台任務中

有人看到我犯了一個錯誤嗎? 我可能做錯了什么?

首先,將TextView子類化,並在構造函數中設置字體。 不需要每次輸入新項目時都設置字體。您還可以緩存DateFormatters和LayoutInflater。 內存分配非常慢。

如果您使用RelativeLayout進行定位,為什么要嵌套LinearLayout? 有點奇怪

將setTypeface放置在if(view == null)語句的getview方法中

MainApplication.OpenSans(ctx)可能會創建一個完整的字體完整實例。 這可能是一個相當漫長的操作。

我建議您在應用啟動時創建一次,然后再使用該引用。 (其他字體也一樣,不用說)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM