Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何提高Android ListView的性能_Android_Performance_Listview_Android Fragments_Android Listview - Fatal编程技术网

如何提高Android ListView的性能

如何提高Android ListView的性能,android,performance,listview,android-fragments,android-listview,Android,Performance,Listview,Android Fragments,Android Listview,我的ListView加载速度非常慢。我的应用程序中有多个选项卡,每次切换到ListView选项卡都需要很长时间(约1-1.5秒) 适配器中的mygetView()方法: @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { convertVi

我的ListView加载速度非常慢。我的应用程序中有多个选项卡,每次切换到ListView选项卡都需要很长时间(约1-1.5秒)

适配器中的my
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\u list\u layout.xml的一部分(除了列表之外,还会显示一些与我的问题无关的按钮和微调器):

我省略了一些样式属性以使其更具可读性

到目前为止我所做的:

  • 使用通用映像加载程序,但问题不在于映像加载(性能大致相同)
  • 使用
    layout\u height
    layout\u width
    上的
    match\u parent
    仅加载必要的数据(这大大提高了我的性能,但仍然不够)
  • 在我的适配器
    getView()方法中避免任何数据库查询
  • 在后台任务中绑定数据

有人看到我犯的错误吗?我可能做错了什么?

首先对TextView进行子类化,并在构造函数中设置字体。没有必要在每次输入新项目时设置字体。您还可以缓存DateFormatters和LayoutInflater。内存分配相当慢


如果你有相对定位的时间,为什么要嵌套线性布局?在if(view==null)语句中将setTypeface放在getview方法中有点奇怪。

这个
main应用程序。OpenSans(ctx)
可能会创建一个完整的字体实例。这可能是一个相当长的操作


我建议你在应用程序启动时创建一次,然后再使用该引用。(不用说,另一种字体也是如此)

那是什么类型的适配器?数组或游标数组;我的适配器扩展了
BaseAdapter
,这可能就是原因。你的数据有多大?有多少文档?初始加载只有6个项目(设备上的空间),甚至需要很长时间。我的清单上有大约30-50个项目。如何将其更改为游标?您可能希望在后台执行一些工作,您可以查看我的相关帖子,其中我在
ListView
中描述了如何解决性能问题,在那里我加载了一些非常大的图像。创建字体可能很繁重。当我重新加载列表时,只创建前6项(因此setTypeface会像以前一样频繁地被调用)。所以把setTypeface放在view==null中当然是一个很好的技巧,谢谢你!,但是它对性能没有帮助您将setTypeface放在bind方法中,并且它对listview的所有项都运行不只是6个iTrms我将更改setTypeface,但它不会降低应用程序的速度(对于测试,我刚刚删除了它,它花费的时间和以前一样多),它可能会导致ImageLoader感谢使用TextView的tipp。我做了,但不幸的是字体不是问题。线性布局仍然存在,因为我还没有找到时间来修复它。我知道这有点奇怪,但它不应该引起任何问题,对吗?它不应该,尝试缓存格式化程序。您可以尝试测量后续方法调用的时间-这将为您提供问题所在的线索。您可以检查(通过注释)带有ImageLoader的行是否不是问题我的问题现在已解决。主要原因是不必要的init方法调用,但您的修复帮助我改进了列表
 <ListView
    android:id="@+id/mylist"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ListView>
<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>