[英]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_height
和layout_width
上使用match_parent
僅加載必要的數據(這可以提高很多性能,但仍然不夠用) getView()
方法中進行任何數據庫查詢 有人看到我犯了一個錯誤嗎? 我可能做錯了什么?
首先,將TextView子類化,並在構造函數中設置字體。 不需要每次輸入新項目時都設置字體。您還可以緩存DateFormatters和LayoutInflater。 內存分配非常慢。
如果您使用RelativeLayout進行定位,為什么要嵌套LinearLayout? 有點奇怪
將setTypeface放置在if(view == null)語句的getview方法中
此MainApplication.OpenSans(ctx)
可能會創建一個完整的字體完整實例。 這可能是一個相當漫長的操作。
我建議您在應用啟動時創建一次,然后再使用該引用。 (其他字體也一樣,不用說)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.