滚动游标的适配器的大名单比内存适配器小得多名单更快 [英] Scrolling large lists of Cursor-based adapters is faster than much smaller lists of in-memory adapters

查看:86
本文介绍了滚动游标的适配器的大名单比内存适配器小得多名单更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,同时具有的CursorAdapter 根据的ListView S(由sqlite的支持),以及自定义 BaseAdapter 根据的ListView ■哪些是建立在从JSON飞从服务器拉下来。

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.

在两个显示的数据是相同的 - 一个图像和几个的TextView 取值。我的光标为基础的ListView有3000行,则基于JSON的ListView中有大约30。但是,向下滚动列表显著更快基于指针的适配器。对于基于JSON列表,所述数据是所有提取前行被制成可见。这两种类型的列表图像被下载点播。

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.

我有相同配置的两个列表视图 - 以 fastScrollEnabled scrollingCache 既与 smoothScrollbar 设置为true。

I have both ListViews configured identically - both with fastScrollEnabled, scrollingCache and smoothScrollbar set to true.

我在寻找关于如何去试图弄清楚是怎么回事,并有可能解决它使导线的基于JSON的的ListView 译文]作为速度是基于游标的。

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.

推荐答案

我在我的应用程序类似的事情,但我只有JSON支持ListView和我也有在这约30个项目(有数据不断变化和动画播放以反映更改)。 这将是更容易发现一个问题,从你身边一些提供code,但这里有几个技巧,你可以起诉进行优化。

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. 传递给你在getView方法的参数之一重用convertView,它真的加速滚动。
  2. 无论是创建自己的行视图(通过扩展一些布局,或ViewGroup中),或者使用setTag某一行返回形成你的getView方法。在这个标签,你应该保持一个对象,它包含对意见该行,所以你不会每次一排被要求为他们寻找与findViewById。 对象可以是一个简单的静态类,像

  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

私有静态类ViewHolder {

private static class ViewHolder {

   ImageView image;
   TextView text;

}

在第一次(当convertView为空,你需要创建一个新的行)你刚才创建ViewHolder的实例,并设置这些参数是指由新创建的行参数(通过调用findViewById),并把这种情况在行的setTag方法。 下一次,当你重复使用convertView,只需拨打getTag并在该标签:收到你会得到参考意见,该行,所以你不会需要到再调用findViewById。

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 / O $ P $关于ListView的psentation。很多有有用的信息: http://www.youtube.com/watch?v=wDBM6wVEO70

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

这篇关于滚动游标的适配器的大名单比内存适配器小得多名单更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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