开发者

Scrolling large lists of Cursor-based adapters is faster than much smaller lists of in-memory adapters

开发者 https://www.devze.com 2023-01-23 10:54 出处:网络
I have an Android app that has both CursorAdapter based ListViews (backed by sqlite) as well as custom BaseAdapter based ListViews which are built on the fly from JSON pulled down from a server.

I have an Android app that has both CursorAdapter based ListViews (backed by sqlite) as well as custom BaseAdapter based ListViews which are built on the fly from JSON pulled down from a server.

The data displayed in both is identical - an image and a couple of TextViews. My Cursor-based ListView has 3000 rows, the JSON-based ListView has about 30. However, scrolling down the list is significantly faster for the Cursor-based adapter. For the JSON-based lists, the data is all fetched before the rows are made visible. Images for both types of lists are downloaded on-demand.

I have both开发者_JAVA百科 ListViews configured identically - both with fastScrollEnabled, scrollingCache and smoothScrollbar set to true.

I'm looking for leads on how to go about trying to figure out what's going on here and to potentially fix it so that the JSON-based ListViews as as fast as the Cursor-based ones.


I have a similar thing in my application, except I only have json backed ListView and I also have about 30 items in it (with data constantly changing and animation playing to reflect changes). It would be much more easier to detect a problem with some provided code from your side, but here are couple of tricks you can sue for optimization.

  1. Reuse convertView that is passed to you as one of the parameters in getView method, it really speeds up the scrolling.
  2. Either create your own row view (by extending some layout, or ViewGroup), or use setTag on a row you return form your getView method. In this tag, you should keep an object that contains references to views in that row, so you wont be searching for them with findViewById everytime a row is requested. Object can be a simple static class like

    private static class ViewHolder {

       ImageView image;
       TextView text;
    

    }

On the first time (when the convertView is null and you need to create a fresh row) you just create instance of your ViewHolder and set those parameters to refer to parameters from your newly created row (by calling findViewById), and put this instance in row's setTag method. Next time, when you reuse convertView, just call getTag and in the tag you recieved you'll get references to views in that row, so you won't be needing to call findViewById anymore.

Of course, you might already done all those things.

P.S. I advice you (if you haven't already) to watch Google I/O presentation about ListView's. A lot of useful information there: http://www.youtube.com/watch?v=wDBM6wVEO70

0

精彩评论

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