Android开源代码解读のOnScrollListener实现ListView滚屏时不加载数据
使用ListView过程中,如果滚动加载数据的操作比较费时,很容易在滚屏时出现屏幕卡住的现象,一个解决的办法就是不要在滚动时加载数据,而是等到滚动停止后再进行数据的加载。这同样要实现OnScrollListener接口,关于该接口的简要描述见上一篇文章,这里直接进行代码的分析:
- package hust.iprai.asce1885;
- import android.app.ListActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- public class MainActivity extends ListActivity implements OnScrollListener {
- private TextView mStatus; //显示滚屏状态
- private boolean mBusy = false; //标识是否存在滚屏操作
- /**
- * 自定义Adapter,实现ListView中view的显示
- *
- */
- private class SlowAdapter extends BaseAdapter {
- private LayoutInflater mInflater;
- public SlowAdapter(Context context) {
- mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- /**
- * 列表中元素个数取决于数据的个数
- */
- public int getCount() {
- return mStrings.length;
- }
- /**
- * 我们的模拟数据是从数组中获取的,因此这里直接返回索引值就可以获取相应的数据了
- */
- public Object getItem(int position) {
- return position;
- }
- /**
- * 使用数组的索引作为唯一的id
- */
- public long getItemId(int position) {
- return position;
- }
- /**
- * 获取List中每一行的view
- */
- public View getView(int position, View convertView, ViewGroup parent) {
- TextView text;
- //给text赋值
- if (null == convertView) {
- text = (TextView) mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
- } else {
- text = (TextView) convertView;
- }
- if (!mBusy) {
- //当前不处于加载数据的忙碌时期(没有滚屏),则显示数据
- text.setText(mStrings[position]);
- //这里约定将tag设置为null说明这个view已经有了正确的数据
- text.setTag(null);
- } else {
- //当前处于滚屏阶段,不加载数据,直接显示数据加载中提示
- text.setText("Loading...");
- //tag非空说明这个view仍然需要进行数据加载并显示
- text.setTag(this);
- }
- return text;
- }
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mStatus = (TextView) findViewById(R.id.status);
- mStatus.setText("Idle");
- //使用自定义的ListAdapter将数据映射到TextView中
- setListAdapter(new SlowAdapter(this));
- //设置滚动监听器
- getListView().setOnScrollListener(this);
- }
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- }
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- switch (scrollState) {
- case OnScrollListener.SCROLL_STATE_IDLE: //Idle态,进行实际数据的加载显示
- mBusy = false;
- int first = view.getFirstVisiblePosition();
- int count = view.getChildCount();
- for (int i = 0; i < count; i++) {
- TextView tv = (TextView) view.getChildAt(i);
- if (tv.getTag() != null) { //非null说明需要加载数据
- tv.setText(mStrings[first + i]);
- tv.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;
- default:
- mStatus.setText("Are you kidding me!");
- break;
- }
- }
- private String[] mStrings = {
- "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
- "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
- "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
- "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
- "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh",
- "Anthoriro", "Appenzell", "Aragon", "Ardi Gasna", "Ardrahan",
- "Armenian String", "Aromes au Gene de Marc", "Asadero", "Asiago",
- "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss", "Babybel",
- "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal",
- "Banon", "Barry's Bay Cheddar", "Basing", "Basket Cheese",
- "Bath Cheese", "Bavarian Bergkase", "Baylough", "Beaufort",
- "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese",
- "Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir",
- "Bierkase", "Bishop Kennedy", "Blarney", "Bleu d'Auvergne",
- "Bleu de Gex", "Bleu de Laqueuille", "Bleu de Septmoncel",
- "Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore",
- "Blue Vein (Australian)", "Blue Vein Cheeses", "Bocconcini",
- "Bocconcini (Australian)", "Boeren Leidenkaas", "Bonchester",
- "Bosworth"};
- }
下面是布局文件main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ListView android:id="@android:id/list"
- android:layout_width="match_parent"
- android:layout_height="0dip"
- android:layout_weight="1"
- android:drawSelectorOnTop="false"/>
- <TextView android:id="@+id/status"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="8dip"
- android:paddingRight="8dip"/>
- </LinearLayout>
程序运行结果如下图所示:

Android开源代码解读のOnScrollListener实现ListView滚屏时不加载数据的更多相关文章
- jQuery+Ajax滚屏异步加载数据实现(附源码)
一.CSS样式 body { font:12px/1.0em Microsoft Yahei; line-height:1.6em; background:#fff; line-height:1.2e ...
- Android开源代码解读-基于SackOfViewAdapter类实现类似状态通知栏的布局
一般来说,ListView的列表项都会采用相同的布局,只是填充的内容不同而已,这种情况下,Android提供了convertView帮我们缓存列表项,达到循环利用的目的,开发者也会使用ViewHold ...
- Listview滑动时不加载数据,停下来时加载数据,让App更优
http://blog.csdn.net/yy1300326388/article/details/45153813
- material design 的android开源代码整理
material design 的android开源代码整理 1 android (material design 效果的代码库) 地址请点击:MaterialDesignLibrary 效果: 2 ...
- 22个值得收藏的Android开源代码-UI篇
本文介绍了android开发者中比较热门的开源代码,这些代码绝大多数可以直接应用到项目中. FileBrowserView 一个强大的文件选择控件.界面比较漂亮,使用也很简单.特点:可以自定义UI:支 ...
- android开源代码
Android开源项目--分类汇总 转自:https://github.com/Trinea/android-open-project Android开源项目第一篇——个性化控件(View)篇 包括L ...
- 160多个android开源代码汇总
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
- 22个值得收藏的Android开源代码——cool
转自http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1020/1808.html 本文介绍了android开发者中比较热门的开源代 ...
- 优秀开源代码解读之JS与iOS Native Code互调的优雅实现方案
简介 本篇为大家介绍一个优秀的开源小项目:WebViewJavascriptBridge. 它优雅地实现了在使用UIWebView时JS与ios 的ObjC nativecode之间的互调,支持消息发 ...
随机推荐
- tomcat端口号、日志、启停
cd到tomcat目录下 1.[root@rusky bin]# ./shutdown.sh 关闭tomcat 2.[root@rusky bin]# ./startup.sh ...
- Nginx常见502错误
1.配置错误因为nginx找不到php-fpm了,所以报错,一般是fastcgi_pass后面的路径配置错误了,后面可以是socket或者是ip:port2.资源耗尽lnmp架构在处理php时,ngi ...
- Linux_x64安装Oracle11g(完整版)
一.修改操作系统核心参数 在Root用户下执行以下步骤: 1)修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/lim ...
- partial修饰符,可以让同类命名空间下出现重名
public partial class Person { } public partial class Person { } partial修饰符,可以让同类命名空间下出现重名,两个类其实是一个类, ...
- 使用angularjs中ng-repeat的$even与$odd属性时的注意事项
JavaScript中数组的索引是从0开始的,因此我们再取奇偶的时候需要用!$even和!$odd来将$even和$odd的布尔值反转 下面给出一个实例: 使用$odd和$even来制作一个红蓝相间的 ...
- explicit 只对构造函数起作用,用来抑制隐式转换。
class A { private: int a; public: A(int x) :a(x){} void display(){ cout << a << endl; } ...
- 利用redis协助mysql数据库搬迁
最近公司新项目上线,需要数据库搬迁,但新版本和老版本数据库差距比较大,关系也比较复杂.如果用传统办法,需要撰写很多mysql脚本,工程量虽然不大,但对于没有dba的公司来说,稍微有点难度.本人就勉为其 ...
- Python socket 广播信息到所有连接的客户端
Python3,多线程,多客户端,广播数据 #!/usr/bin/env python3 import time import threading import queue import socket ...
- commons-beanutils使用
Jakarta Commons项目提供了相当丰富的API,我们之前了解到的Commons Lang只是众多API的比较核心的一小部分而已.Commons下面还有相当数量的子项目,用于解决各种各样不同方 ...
- Activiti 5.18 流程Model 转成 流程BPMN文件
直接上代码吧 byte[] bpmnBytes = null; String filename = null; JsonNode editorNode = new ObjectMapper().rea ...