【发布时间】:2014-03-27 02:05:23
【问题描述】:
我的 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>
我省略了一些样式属性以使其更具可读性。
到目前为止我做了什么:
- 使用通用图像加载器,但问题不在于图像加载(性能大致相同)
- 使用
match_parentonlayout_height和layout_width仅加载必要的数据(这大大提高了我的性能,但仍然不够) - 避免在我的 Adapter
getView()方法中进行任何数据库查询 - 在后台任务中绑定数据
有人看到我犯的错误吗?我可能做错了什么?
【问题讨论】:
-
那是什么类型的适配器?数组还是光标?
-
数组;我的适配器扩展了
BaseAdapter -
这可能就是原因。你的数据有多大?有多少文件?
-
初始加载只有6个项目(设备上的空间),甚至需要很长时间。在我的列表中大约有 30-50 项。如何将其更改为光标?
-
你可能想在后台执行一些工作,你可以查看我的相关帖子,我在
ListView中描述了我如何解决性能问题,我在其中加载了一些相当大的图像stackoverflow.com/questions/19469073/…
标签: android performance listview android-fragments android-listview