开发者

ListView get slow when the Base adapter.getview() method is called

I have been debugging my application and i saw that when i was scrolling the listview the method getView() of the class BaseAdapter is called to generate new views

public View getView(int position, View convertView, ViewGroup parent) {
    Article article = this.articles.get(position);
    return new MainView(this.context, articulo.getTitle() , articles.getDescription(),articles.getImgUrl());)  }

when i scroll the listActivity to see the new items this method is invoked again to create the below list view items, as a cons开发者_如何学Cequence that the list items have images the ListActivity get slow, is there any way to create all the items view once, and not create ListItems when we are scrolling the listActivity


ListViews are highly optimized for performance, you should use ViewHolder inside your ListAdapter to cache the ListItems.

check http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

the rule is, first set up your customview, pack everything inside your holder and pin this holder onto the view, the second time the view is used android simple extract the holder information (really fast).


It's probably slowing down because of the number of objects that are created. For performance you should reuse your rows. See the getView implementation here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List4.html and http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html


You should not create a new View on each call to getView. The convertView that is being passed in allows use to reuse an existing View. In your case this will be an instance of MainView. So you can do something like this:

MainView mv;
if (convertView != null){
  mv = (MainView) convertView;
  ((TextView) mv.findViewById(R.id.title)).setText(articulo.getTitle());
  // similar for description and imgUrl
} else {
  mv = new MainView(...);
}
return mv;

In addition, you could use the ViewHolder pattern suggested by Michele. This will allow you to avoid the findViewById lookups when setting title etc. Here is a great explanation of ViewHolder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜