首先放出一些相关类:
1、储存数据的类

、
2、GriView(和listView用法相似)的item布局

3、自定义的适配器

这里就涉及到了优化问题,原生的ArrayAdapter在用户滑动的时候会不断的调用getView方法来加载View,不管是不是以前加载过的都会重复加载。
解决这个问题的方法关键在于getVIew方法的第二个参数,这个参数可以保存每一个位置加载的view,那么我们只要判断这个参数是否为空就可以知道这个item有没有被加载,已经加载的可以重复利用。
当然,这里还可以进一步优化一下,不用每一次加载view 的时候都绑定一下控件,我们把第一次加载view 的时候绑定好了的控件对象放入ViewHolder来储存一下。通过view的setTag()方法将ViewHolder对象和View绑定。
如果找到已经加载过的View那就getTag读出来即可。

5、效果

关于ArrayAdapter的使用可以看上一篇文章
网友评论