Android ApiDemos示例解析(180):Views->Lists->13. Slow Adapter
来源:互联网 发布:java微信号自定义菜单 编辑:程序博客网 时间:2024/05/18 12:01
对于一个长列表,如果每个列表项载入比较费时( 比如从网络下载图像),如果此时需要快速滚动列表,可以先给列表项使用一个替代名称或是其它可以快速载入的项,当列表停止滚动时再载入实际的列表项,这样可以大大提高列表的响应性能。
本例在ListView中显示一个字符串数组来模拟一个费时的列表项载入(尽管显示文字列表项实际上很快),当列表开始滚动或是快速飞行时,使用一个临时数据来绑定列表项(如Loading …),而在列表停止滚动时,使用实际需要显示的文字替换之前显示的临时数据。
看看自定义SlowAdapter的getView 的实现:
public View getView(int position, View convertView, ViewGroup parent) { TextView text; if (convertView == null) { text = (TextView)mInflater.inflate (android.R.layout.simple_list_item_1, parent, false); } else { text = (TextView)convertView; } if (!mBusy) { text.setText(mStrings[position]); // Null tag means the view has the c //orrect data text.setTag(null); } else { text.setText("Loading..."); // Non-null tag means the view still //needs to load it's data text.setTag(this); } return text;}
可以看到在ListView busy(滚动时) ,将textview 的文字设置成临时数据(loading …”) ,并把textView 的tag 设为非空, TextView 的Tag 可以使用任何对象,本例用它来做个标记,非空时表示当前TextView使用的是临时数据。 而的ListView不忙时(停止滚动),将TextView设置成实际需要显示的文字(或是其它费时的操作),并把它的Tag设为空,表示TextView显示的是真实数据。
为ListView 添加滚动事件处理
getListView().setOnScrollListener(this);}...public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case OnScrollListener.SCROLL_STATE_IDLE: mBusy = false; int first = view.getFirstVisiblePosition(); int count = view.getChildCount(); for (int i=0; i<count; i++) { TextView t = (TextView)view.getChildAt(i); if (t.getTag() != null) { t.setText(mStrings[first + i]); t.setTag(null); } } mStatus.setText("Idle"); break; case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: mBusy = true; mStatus.setText("Touch scroll"); break; case OnScrollListener.SCROLL_STATE_FLING: mBusy = true; mStatus.setText("Fling"); break; }}
在ListView 滚动或是飞速滑动时,将busy设为true, 当滚动停止时SCROLL_STATE_IDLE ,busy为false, 并根据TextView的Tag值判断是否需要显示实际的文字串。
本例最下面并使用了一个文本框显示了当前ListView的滚动状态。
- Android ApiDemos示例解析(180):Views->Lists->13. Slow Adapter
- Android ApiDemos示例解析(181):Views->Lists->14.Efficient Adapter
- ApiDemos-->Views-lists-slow adapter学习
- Android ApiDemos示例解析(112):Views->Expandable Lists->1. Custom Adapter
- Android ApiDemos示例解析(112):Views->Expandable Lists->1. Custom Adapter
- Android ApiDemos示例解析(114):Views->Expandable Lists->3. SimpleAdapter
- Android ApiDemos示例解析(168):Views->Lists->1. Array
- Android ApiDemos示例解析(169):Views->Lists->2. Cursor(People)
- Android ApiDemos示例解析(170):Views->Lists->3. Cursor (Phones)
- Android ApiDemos示例解析(171):Views->Lists->4. ListAdapter
- Android ApiDemos示例解析(172):Views->Lists->5. Separators
- Android ApiDemos示例解析(173):Views->Lists->6. ListAdapter Collapsed
- Android ApiDemos示例解析(174):Views->Lists->7. Cursor(Phones)
- Android ApiDemos示例解析(175):Views->Lists->8. Photos
- Android ApiDemos示例解析(176):Views->Lists->9. Array (Overlay)
- Android ApiDemos示例解析(179):Views->Lists->12. Transcript
- Android ApiDemos示例解析(113):Views->Expandable Lists->2. Cursor(People)
- Android ApiDemos示例解析(177):Views->Lists->10. Single choice list
- 链表的创建、插入、删除操作
- access中的字段类型
- STM32菜鸟成长记录---窗口看门狗
- 【spring】IOC入门
- 如何设计Delphi TCP客户端
- Android ApiDemos示例解析(180):Views->Lists->13. Slow Adapter
- 指针与数组的区别
- 内部类访问访问其他成员限制
- 堆和栈的区别
- STM32菜鸟成长记录---AD7792遇到的问题
- 常用的几种排序算法
- 本地连接图标丢失,上网正常
- Android的系统架构
- jquery特效,不用再写繁琐的js脚本