ApiDemos-->Views-lists-slow adapter学习
今天来依照apidemos提供的方法来实现slow loading的效果.
简单说下实现方法:
实现ListView.OnScrollListener ,监听到手势滑动的情况,当处于滚动状态时,将新显示的items 设置为Loading , 当离开屏幕时,才载入真实的数据.
设置数据时,要用到getFirstVisiblePosition属性来计算应该载入第几个item.
该小demo应该算是学习Android AsyncTask异步载入的基础.
Main.java
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.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView; import com.example.testmyviewslistsactivateitems.R; /**
*
* @author Administrator 仿效果slow loading apiDemos -- Views -Lists - Slow Adapter
*/
public class Main extends ListActivity { private boolean mBusy = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new SlowAdapter(this));
// 设置选择模式为单选
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// 首次载入设置选中items
getListView().setItemChecked(0, true);
getListView().setOnScrollListener(new OnScrollListener());
} protected class OnScrollListener implements ListView.OnScrollListener { @Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
//The view is not scrolling.
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);
}
}
break;
// The user is scrolling using touch, and their finger is still on the screen
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
mBusy = true;
break;
//The user had previously been scrolling using touch and had performed a fling.
//The animation is now coasting to a stop
case OnScrollListener.SCROLL_STATE_FLING:
mBusy = true;
break;
}
} @Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
} } @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
getListView().setItemChecked(position, true);
} // 自己定义适配器
private class SlowAdapter extends BaseAdapter {
private LayoutInflater mInflater; public SlowAdapter(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} @Override
public int getCount() {
return mStrings.length;
} @Override
public Object getItem(int position) {
return position;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text; if (convertView == null) {
text = (TextView) mInflater.inflate(R.layout.main, null, false);
} else {
text = (TextView) convertView;
} if (!mBusy) {
text.setText(mStrings[position]);
text.setTag(null);
} else {
text.setText("Loading...");
text.setTag(this);
}
return text;
} } // data
public static final 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",
"Vacherin-Fribourgeois", "Valencay", "Vasterbottenost", "Venaco",
"Vendomois", "Vieux Corse", "Vignotte", "Vulscombe",
"Waimata Farmhouse Blue", "Washed Rind Cheese (Australian)",
"Waterloo", "Weichkaese", "Wellington", "Wensleydale",
"White Stilton", "Whitestone Farmhouse", "Wigmore",
"Woodside Cabecou", "Xanadu", "Xynotyro", "Yarg Cornish",
"Yarra Valley Pyramid", "Yorkshire Blue", "Zamorano",
"Zanetti Grana Padano", "Zanetti Parmigiano Reggiano" };
}
ApiDemos-->Views-lists-slow adapter学习的更多相关文章
- listview中的adapter学习小结
概述 Adapter是数据和UI之间的一个桥梁,在listview,gridview等控件中都会使用到,android给我们提拱了4个adapte供我们使用: BaseAdapter是一个抽象类,继承 ...
- Android适配器Adapter学习
在开发中我们需要绑定一些数据展现到桌面上,这是就需要AdapterView.AdapterView是ViewGroup的子类,它决定了怎么展现视图通过Adapter来绑定特殊的数据类型. Adapte ...
- Django之views.py视图函数学习
视图函数: 视图函数时存在django项目中的应用程的一个名为views.py的文件模块: 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 一 ...
- Apidemos-->Views-Lists-Cursor(people)学�
Apidemos-->Views-Lists-Cursor(people)-主要用到了获取手机联系人信息,属于内容提供者的范畴,要想了解这方面的内容,能够參考官方docs /sdk/docs/g ...
- 使用同步适配器(sync adapter)数据传输
在android设备与webserver之间实现数据同步能显著提高你的应用的有用性.让你的应用更受用户的欢迎. 比方说.你的数据上传给webserver,这就有了一个有用的备份.当用户的设备离线工作时 ...
- 3d ListView翻译
作为一个刚毕业的大学生,我要提醒自己时时刻刻要学习. 最近做listview看到很久以前的一个demo,高手如云啊,我们就只有好好加油了. 这是索尼公司的一个员工写的学习博客,由于本人英文能力有限是按 ...
- 解决RecyclerView无法onItemClick问题
供RecyclerView采用.会员可以查看将替代ListView的RecyclerView 的使用(一),单单从代码结构来说RecyclerView确实比ListView优化了非常多.也简化了我们编 ...
- Microsoft SQL Server Trace Flags
Complete list of Microsoft SQL Server trace flags (585 trace flags) REMEMBER: Be extremely careful w ...
- Python的平凡之路(19)
一.Django请求生命周期 对于所有的web框架来说本质就是一个socket服务端,浏览器是socket客户端 ...
随机推荐
- Android项目代码混淆
http://coolshell.info/blog/2015/03/Android-studio-prefrence.html 什么是Gradle Gradle是一种依赖管理工具,基于Groovy语 ...
- Sqlite 错误码
#define SQLITE_OK 0 /* 成功 | Successful result */ /* 错误码开始 */ #define SQLITE_ERROR 1 /* SQL错误 或 丢失数据库 ...
- 为你的网页中添加一些空格
在上一节的例子,我们已经讲解过在html代码中输入空格.回车都是没有作用的.要想输入空格,必须写入 . 语法: 在html代码中输入空格是不起作用的,如下代码. 在浏览中显示,还是没有空格效果. ...
- entity framework 动态条件
entity framework 动态条件 问题:在实际编码过程中,根据不同的选择情况,会需要按照不同的条件查询数据集 如:状态confirmStatus ,如果为空的时候,查询全部,如果有具体值的时 ...
- idea maven web工程明明添加了maven lib的依赖,但启动web容器时始终报No Class Found?
idea maven web工程明明添加了maven lib的依赖,但启动web容器时始终报No Class Found? 很久没用idea搭新工程,最近自己想做个东西,冲心搭个web工程,jar包都 ...
- MESH
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Unity_meshtest.html A class that allows c ...
- MyBatis学习笔记(2)——缓存
一级缓存:基于PerpetualCache的HashMap本地缓存,其存储作用域为Session,当Session flush或 close之后,该Session 中的所有Cache将被清空 二级缓存 ...
- InstallShield Basic MSI工程常见问题解答[转]
1. 问题描述:采用何种安装模式?实现方法:如果对用户界面等自定义要求不高的话,建议用Basic Msi Project,否则用InstallScript MSI Project. 2. 问题描述 ...
- highcharts实例教程一:结合php与mysql生成折线图
Highcharts是一款纯javascript和html5编写的图表库,不仅几乎能兼容所有pc浏览器,而且对ios和android手机端的兼容 性也不错,它能够很简单便捷的在Web网站或Web应用中 ...
- 【行为型】Memento模式
备忘录模式顾名思义就是一种能有备忘作用的设计模式,其目的是在对象外部保存其在某一时刻的状态信息,并且在任何需要的时候,都可以通过备忘录中保存的状态数据恢复对象在当时情形下的状态. 备忘录模式旨在对象的 ...