具有自定义BaseAdapter的可滚动ListView非常慢 [英] Scrollable ListView with customized BaseAdapter is very slow

查看:83
本文介绍了具有自定义BaseAdapter的可滚动ListView非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的listView有一个定制的BaseAdapter,其代码如下:

I have a customized BaseAdapter for my listView with this code:

public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  if (convertView == null) 
  { 
  } 
  else 
  {
   holder = (ViewHolder) convertView.getTag();
  }
  convertView = mInflater.inflate(R.layout.user_submissions_customisation, null);
   holder = new ViewHolder();
   holder.JournalName = (TextView) convertView.findViewById(R.id.JournalName);
   holder.SubmissionTitle = (TextView) convertView.findViewById(R.id.SubmissionTitle);
   holder.SubmissionDate = (TextView) convertView.findViewById(R.id.SubmissionDate);
   holder.statusOk=(ImageView)convertView.findViewById(R.id.statusOkImage);
   holder.statusRejected=(ImageView)convertView.findViewById(R.id.statusRejectedImage);
   holder.statusProcessing=(ImageView)convertView.findViewById(R.id.statusProcessingImage);
   convertView.setTag(holder);

  ImageView statusOk=(ImageView)convertView.findViewById(R.id.statusOkImage);
  ImageView statusRejected=(ImageView)convertView.findViewById(R.id.statusRejectedImage);
  ImageView statusProcessing=(ImageView)convertView.findViewById(R.id.statusProcessingImage);

  MDPIActivity mdpi = new MDPIActivity();
  Context context =mdpi.getContext();
  LocalDatabase localDatabase = new LocalDatabase(context); //Instantiation of the DB
  int status = localDatabase.getSubmissionStatus(position+1);

  if (status==102 | status==19)
  {
      statusRejected.setVisibility(View.VISIBLE);
  }
  else
  {
      if (status==29)
      {
      statusOk.setVisibility(View.VISIBLE);
      }
      else
      {
          statusProcessing.setVisibility(View.VISIBLE);
      }
  }

  holder.JournalName.setText(submissionsArrayList.get(position).getJournalTitle()+"-"+submissionsArrayList.get(position).getID());
  holder.SubmissionTitle.setText(submissionsArrayList.get(position).getTitle());
  holder.SubmissionDate.setText(submissionsArrayList.get(position).getDate());

  return convertView;
}

 static class ViewHolder {
  TextView JournalName;
  TextView SubmissionTitle;
  TextView SubmissionDate;
  ImageView statusOk;
  ImageView statusRejected;
  ImageView statusProcessing;
 }

所有工作都可以,但是在显示器上创建列表和滚动非常慢. 我也将其用于éy列表:

All works wel but the creation of the list on the display and the scrolling is pretty slow. I used this for éy list too:

android:fastScrollEnabled="true"
android:scrollingCache="true"
android:smoothScrollbar="true"

但是视图从不清晰的视图切换到列表视图的速度很慢,列表的滚动也是如此.

But the view changing from the previeous view to the listview is slow and the scrolling of the list too.

推荐答案

首先进行此操作
在转换视图为空"的if条件下移动此代码.

convertView = mInflater.inflate(R.layout.user_submissions_customisation, null); holder = new ViewHolder(); holder.JournalName = (TextView) convertView.findViewById(R.id.JournalName); holder.SubmissionTitle = (TextView) convertView.findViewById(R.id.SubmissionTitle); holder.SubmissionDate = (TextView) convertView.findViewById(R.id.SubmissionDate); holder.statusOk=(ImageView)convertView.findViewById(R.id.statusOkImage); holder.statusRejected=(ImageView)convertView.findViewById(R.id.statusRejectedImage); holder.statusProcessing=(ImageView)convertView.findViewById(R.id.statusProcessingImage); convertView.setTag(holder);

您不能像以前那样通过编码有效地使用适配器.
上述方法肯定会提高性能.

总而言之,只有在以下情况下才必须为列表行填充视图:转换视图为null.
此外,必须使用convertView本身来初始化行视图中的不同视图.
然后,将Holder视图设置为convert视图的标签,适配器将连续提供相同的视图,而不是每次都填充新视图.时间.

您应该移动代码以从getView()方法获取上下文并实例化db.
只需在其中创建类级变量"mContext"您的适配器类,并通过传递活动的上下文在适配器的构造函数中实例化它.
然后在同一位置(构造函数)也实例化数据库.

All apart first do this thing
Move this code in the if condition when "convert view is null"

convertView = mInflater.inflate(R.layout.user_submissions_customisation, null); holder = new ViewHolder(); holder.JournalName = (TextView) convertView.findViewById(R.id.JournalName); holder.SubmissionTitle = (TextView) convertView.findViewById(R.id.SubmissionTitle); holder.SubmissionDate = (TextView) convertView.findViewById(R.id.SubmissionDate); holder.statusOk=(ImageView)convertView.findViewById(R.id.statusOkImage); holder.statusRejected=(ImageView)convertView.findViewById(R.id.statusRejectedImage); holder.statusProcessing=(ImageView)convertView.findViewById(R.id.statusProcessingImage); convertView.setTag(holder);

You are not using the adapter efficiently by coding as you did.
The above way will improve the performance for sure.

In nutshell, you must inflate the view for list row only when the convert view is null.
Also, different views in the row's view must be initialized with the convertView itself.
Then you set holder view as the tag to the convert view and the adapter will continuously provide the same view instead of inflating the new one every time.

You should move the code for getting the context and instantiating the db from the getView() method.
Just make a class level variable "mContext" in your adapter class and instantiate it in the constructor of your adapter by passing activity's context.
Then at same place (constructor) instantiate the db also.

这篇关于具有自定义BaseAdapter的可滚动ListView非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆