使用ListView过程中,如果滚动加载数据的操作比较费时,很容易在滚屏时出现屏幕卡住的现象,一个解决的办法就是不要在滚动时加载数据,而是等到滚动停止后再进行数据的加载。这同样要实现OnScrollListener接口,关于该接口的简要描述见上一篇文章,这里直接进行代码的分析:

  1. package hust.iprai.asce1885;
  2. import android.app.ListActivity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AbsListView;
  9. import android.widget.AbsListView.OnScrollListener;
  10. import android.widget.BaseAdapter;
  11. import android.widget.TextView;
  12. public class MainActivity extends ListActivity implements OnScrollListener {
  13. private TextView mStatus; //显示滚屏状态
  14. private boolean mBusy = false; //标识是否存在滚屏操作
  15. /**
  16. * 自定义Adapter,实现ListView中view的显示
  17. *
  18. */
  19. private class SlowAdapter extends BaseAdapter {
  20. private LayoutInflater mInflater;
  21. public SlowAdapter(Context context) {
  22. mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  23. }
  24. /**
  25. * 列表中元素个数取决于数据的个数
  26. */
  27. public int getCount() {
  28. return mStrings.length;
  29. }
  30. /**
  31. * 我们的模拟数据是从数组中获取的,因此这里直接返回索引值就可以获取相应的数据了
  32. */
  33. public Object getItem(int position) {
  34. return position;
  35. }
  36. /**
  37. * 使用数组的索引作为唯一的id
  38. */
  39. public long getItemId(int position) {
  40. return position;
  41. }
  42. /**
  43. * 获取List中每一行的view
  44. */
  45. public View getView(int position, View convertView, ViewGroup parent) {
  46. TextView text;
  47. //给text赋值
  48. if (null == convertView) {
  49. text = (TextView) mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
  50. } else {
  51. text = (TextView) convertView;
  52. }
  53. if (!mBusy) {
  54. //当前不处于加载数据的忙碌时期(没有滚屏),则显示数据
  55. text.setText(mStrings[position]);
  56. //这里约定将tag设置为null说明这个view已经有了正确的数据
  57. text.setTag(null);
  58. } else {
  59. //当前处于滚屏阶段,不加载数据,直接显示数据加载中提示
  60. text.setText("Loading...");
  61. //tag非空说明这个view仍然需要进行数据加载并显示
  62. text.setTag(this);
  63. }
  64. return text;
  65. }
  66. }
  67. @Override
  68. public void onCreate(Bundle savedInstanceState) {
  69. super.onCreate(savedInstanceState);
  70. setContentView(R.layout.main);
  71. mStatus = (TextView) findViewById(R.id.status);
  72. mStatus.setText("Idle");
  73. //使用自定义的ListAdapter将数据映射到TextView中
  74. setListAdapter(new SlowAdapter(this));
  75. //设置滚动监听器
  76. getListView().setOnScrollListener(this);
  77. }
  78. public void onScroll(AbsListView view, int firstVisibleItem,
  79. int visibleItemCount, int totalItemCount) {
  80. }
  81. public void onScrollStateChanged(AbsListView view, int scrollState) {
  82. switch (scrollState) {
  83. case OnScrollListener.SCROLL_STATE_IDLE: //Idle态,进行实际数据的加载显示
  84. mBusy = false;
  85. int first = view.getFirstVisiblePosition();
  86. int count = view.getChildCount();
  87. for (int i = 0; i < count; i++) {
  88. TextView tv = (TextView) view.getChildAt(i);
  89. if (tv.getTag() != null) { //非null说明需要加载数据
  90. tv.setText(mStrings[first + i]);
  91. tv.setTag(null);
  92. }
  93. }
  94. mStatus.setText("Idle");
  95. break;
  96. case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
  97. mBusy = true;
  98. mStatus.setText("Touch Scroll");
  99. break;
  100. case OnScrollListener.SCROLL_STATE_FLING:
  101. mBusy = true;
  102. mStatus.setText("Fling");
  103. break;
  104. default:
  105. mStatus.setText("Are you kidding me!");
  106. break;
  107. }
  108. }
  109. private String[] mStrings = {
  110. "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
  111. "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
  112. "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
  113. "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
  114. "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh",
  115. "Anthoriro", "Appenzell", "Aragon", "Ardi Gasna", "Ardrahan",
  116. "Armenian String", "Aromes au Gene de Marc", "Asadero", "Asiago",
  117. "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss", "Babybel",
  118. "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal",
  119. "Banon", "Barry's Bay Cheddar", "Basing", "Basket Cheese",
  120. "Bath Cheese", "Bavarian Bergkase", "Baylough", "Beaufort",
  121. "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese",
  122. "Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir",
  123. "Bierkase", "Bishop Kennedy", "Blarney", "Bleu d'Auvergne",
  124. "Bleu de Gex", "Bleu de Laqueuille", "Bleu de Septmoncel",
  125. "Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore",
  126. "Blue Vein (Australian)", "Blue Vein Cheeses", "Bocconcini",
  127. "Bocconcini (Australian)", "Boeren Leidenkaas", "Bonchester",
  128. "Bosworth"};
  129. }

下面是布局文件main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <ListView android:id="@android:id/list"
  7. android:layout_width="match_parent"
  8. android:layout_height="0dip"
  9. android:layout_weight="1"
  10. android:drawSelectorOnTop="false"/>
  11. <TextView android:id="@+id/status"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:paddingLeft="8dip"
  15. android:paddingRight="8dip"/>
  16. </LinearLayout>

程序运行结果如下图所示:

Android开源代码解读のOnScrollListener实现ListView滚屏时不加载数据的更多相关文章

  1. jQuery+Ajax滚屏异步加载数据实现(附源码)

    一.CSS样式 body { font:12px/1.0em Microsoft Yahei; line-height:1.6em; background:#fff; line-height:1.2e ...

  2. Android开源代码解读-基于SackOfViewAdapter类实现类似状态通知栏的布局

    一般来说,ListView的列表项都会采用相同的布局,只是填充的内容不同而已,这种情况下,Android提供了convertView帮我们缓存列表项,达到循环利用的目的,开发者也会使用ViewHold ...

  3. Listview滑动时不加载数据,停下来时加载数据,让App更优

    http://blog.csdn.net/yy1300326388/article/details/45153813

  4. material design 的android开源代码整理

    material design 的android开源代码整理 1 android (material design 效果的代码库) 地址请点击:MaterialDesignLibrary 效果: 2 ...

  5. 22个值得收藏的Android开源代码-UI篇

    本文介绍了android开发者中比较热门的开源代码,这些代码绝大多数可以直接应用到项目中. FileBrowserView 一个强大的文件选择控件.界面比较漂亮,使用也很简单.特点:可以自定义UI:支 ...

  6. android开源代码

    Android开源项目--分类汇总 转自:https://github.com/Trinea/android-open-project Android开源项目第一篇——个性化控件(View)篇 包括L ...

  7. 160多个android开源代码汇总

    第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...

  8. 22个值得收藏的Android开源代码——cool

    转自http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1020/1808.html 本文介绍了android开发者中比较热门的开源代 ...

  9. 优秀开源代码解读之JS与iOS Native Code互调的优雅实现方案

    简介 本篇为大家介绍一个优秀的开源小项目:WebViewJavascriptBridge. 它优雅地实现了在使用UIWebView时JS与ios 的ObjC nativecode之间的互调,支持消息发 ...

随机推荐

  1. tomcat端口号、日志、启停

    cd到tomcat目录下 1.[root@rusky bin]# ./shutdown.sh         关闭tomcat 2.[root@rusky bin]# ./startup.sh     ...

  2. Nginx常见502错误

    1.配置错误因为nginx找不到php-fpm了,所以报错,一般是fastcgi_pass后面的路径配置错误了,后面可以是socket或者是ip:port2.资源耗尽lnmp架构在处理php时,ngi ...

  3. Linux_x64安装Oracle11g(完整版)

    一.修改操作系统核心参数 在Root用户下执行以下步骤: 1)修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/lim ...

  4. partial修饰符,可以让同类命名空间下出现重名

    public partial class Person { } public partial class Person { } partial修饰符,可以让同类命名空间下出现重名,两个类其实是一个类, ...

  5. 使用angularjs中ng-repeat的$even与$odd属性时的注意事项

    JavaScript中数组的索引是从0开始的,因此我们再取奇偶的时候需要用!$even和!$odd来将$even和$odd的布尔值反转 下面给出一个实例: 使用$odd和$even来制作一个红蓝相间的 ...

  6. explicit 只对构造函数起作用,用来抑制隐式转换。

    class A { private: int a; public: A(int x) :a(x){} void display(){ cout << a << endl; } ...

  7. 利用redis协助mysql数据库搬迁

    最近公司新项目上线,需要数据库搬迁,但新版本和老版本数据库差距比较大,关系也比较复杂.如果用传统办法,需要撰写很多mysql脚本,工程量虽然不大,但对于没有dba的公司来说,稍微有点难度.本人就勉为其 ...

  8. Python socket 广播信息到所有连接的客户端

    Python3,多线程,多客户端,广播数据 #!/usr/bin/env python3 import time import threading import queue import socket ...

  9. commons-beanutils使用

    Jakarta Commons项目提供了相当丰富的API,我们之前了解到的Commons Lang只是众多API的比较核心的一小部分而已.Commons下面还有相当数量的子项目,用于解决各种各样不同方 ...

  10. Activiti 5.18 流程Model 转成 流程BPMN文件

    直接上代码吧 byte[] bpmnBytes = null; String filename = null; JsonNode editorNode = new ObjectMapper().rea ...