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之间的互调,支持消息发 ...
随机推荐
- javascript封装id|class|元素选择器
由于各个浏览器都支持的选择方法只有如下三种: 1 document.getElementById() 2 document.getElementsByName() 3 document.getElem ...
- 前端笔试题目总结——应用JavaScript函数递归打印数组到HTML页面上
数组如下: var item=[{ name:'Tom', age:70, child:[{ name:'Jerry', age:50, child:[{ name:'William', age:20 ...
- Ajax--WebService返回List
WebService: using System.Web.Script.Services; [GenerateScriptType(typeof(people))] [WebMethod] publi ...
- java文件io过滤器
package cn.stat.p1.file; import java.io.File; public class newfilelist { /** * @param args */ public ...
- C/C++语言中const的用法
1. const 在C和C++中的区别 C++中的const正常情况下是看成编译期的常量,编译器并不为const分配空间,只是在编译的时候将期值保存在名字表中,并在适当的时候折合在代码中. 所 ...
- Go语言之defer
defer语句被用于预定对一个函数的调用.我们把这类被defer语句调用的函数称为延迟函数.注意,defer语句只能出现在函数或方法的内部. 一条defer语句总是以关键字defer开始.在defer ...
- javascript版QQ在线聊天挂件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- WEB开发中常用的正则表达式
在计算机科学中,正则表达式用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在WEB开发中,正则表达式通常用来检测.查找替换某些符合规则的字符串,如检测用户输入E-mai格式是否正确,采集符 ...
- python之6-3嵌套函数
1. 嵌套函数 子函数可以继承父函数的变量 父函数返回子函数 子函数返回结果 看例子如下:结果是一个字符串fun1+fun2 #!/usr/bin/env python # coding=utf-8 ...
- .Net SSRS(rdlc) 报表经验总结
排版 1. 可以利用表格来布局,以避免调整固定宽度的麻烦. 2. 一个表的表头里还可以嵌套表格. 3. 设置rdlc报表打印格式.首先打开RDLC报表设计器页面.在灰色部分点右键 -> 报表属性 ...