下文来自:
http://www.cnblogs.com/qianxudetianxia/archive/2011/08/04/2088493.html
这个人的博客,所有技术文章 值得精读
---------------------------------------------------------------------------------------------------------------------------------------------------
游标ListView,提供索引标签,使用户能够快速定位列表项。
也可以叫索引ListView,有的人称也为Tweaked ListView,可能更形象些吧。
一看图啥都懂了:
1.游标(Fast scroll thumb)
就是右边的那个拖动的方块,这个非常的简单:
2 |
android:id = "@+id/tweaked_list"
|
3 |
android:layout_width = "fill_parent"
|
4 |
android:layout_height = "wrap_content"
|
5 |
android:fastScrollEnabled = "true" />
|
也可以用在java后台书写:
1 |
tweakedListView.setFastScrollEnabled( true );
|
在数据量有一定大的时候,滑动列表,就会出现右边的所谓的"游标"了。
简单,这也是我为什么私下里喜欢自己写控件,但是工作中却喜欢用通用控件。
我们看下源代码,其实就是启用FastScroller对象:
02 |
public void setFastScrollEnabled( boolean enabled) {
|
03 |
mFastScrollEnabled = enabled;
|
05 |
if (mFastScroller == null ) {
|
06 |
mFastScroller = new FastScroller(getContext(), this );
|
09 |
if (mFastScroller != null ) {
|
2.字母索引
在Android学习系列(10)--App列表之拖拽ListView(上)中我们使用了一种WindowManager在ListView中添加一些自定义影像,这种方法我觉得一定是可行的。
但是,android系统给我们提供了一个更简单的方法:使用AlphabetIndexer。
AlphabetIndexer,实现了SectionIndexer接口,是adapter的一个辅助类,辅助实现在快滑时,显示索引字母。
使用字母索引的话,必须保证数据列表是按字母顺序排序,以便AlphabetIndexerh采用二分查找法快速定位。
3 |
* sortedColumnIndex数据集合中的第几列 |
4 |
* alphabet字母列表,用的最多的是"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
6 |
public AlphabetIndexer(Cursor cursor, int sortedColumnIndex, CharSequence alphabet) {}
|
用到3个方法:
2 |
public int getPositionForSection( int section) {}
|
3 |
public int getSectionForPosition( int position) {}
|
4 |
public Object[] getSections() {}
|
3.游标Cursor的实现
Cursor接口的实现,有两种选择:
(1).直接使用数据库查询返回的cursor
(2).自定义实现Cursor接口的新类
第一种方式很简单,查询一下数据库返回Cursor即可。
这里我们以第二种方式实践,伪装一个Cursor,主要是实现3个方法:
(1).getCount()
(2).moveToPosition()
(3). getString()
002 |
* 伪装一个Cursor供AlphabetIndexer作数据索引源
|
004 |
private class IndexCursor implements Cursor{
|
006 |
private ListAdapter adapter;
|
008 |
private Map<String, String> map;
|
010 |
public IndexCursor(ListAdapter adapter){
|
011 |
this .adapter = adapter;
|
015 |
public int getCount() { return this .adapter.getCount();}
|
018 |
* 取得索引字母,这个方法非常重要,根据实际情况具体处理
|
020 |
@SuppressWarnings ( "unchecked" )
|
022 |
public String getString( int columnIndex) {
|
023 |
map = (HashMap<String, String>)adapter.getItem(position);
|
024 |
return map.get(key).substring( 0 , 1 );
|
028 |
public boolean moveToPosition( int position) {
|
029 |
if (position<- 1 ||position>getCount()){
|
033 |
this .position = position;
|
046 |
public void copyStringToBuffer( int arg0, CharArrayBuffer arg1) {}
|
048 |
public void deactivate() {}
|
050 |
public byte [] getBlob( int arg0) { return null ;}
|
052 |
public int getColumnCount() { return 0 ;}
|
054 |
public int getColumnIndex(String columnName) { return 0 ;}
|
056 |
public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException
{ return 0 ;}
|
058 |
public String getColumnName( int columnIndex) { return null ;}
|
060 |
public String[] getColumnNames() { return null ;}
|
062 |
public double getDouble( int columnIndex) { return 0 ;}
|
064 |
public Bundle getExtras() { return null ;}
|
066 |
public float getFloat( int columnIndex) { return 0 ;}
|
068 |
public int getInt( int columnIndex) { return 0 ;}
|
070 |
public long getLong( int columnIndex) { return 0 ;}
|
072 |
public int getPosition() { return position;}
|
074 |
public short getShort( int columnIndex) { return 0 ;}
|
076 |
public boolean getWantsAllOnMoveCalls() { return false ;}
|
078 |
public boolean isAfterLast() { return false ;}
|
080 |
public boolean isBeforeFirst() { return false ;}
|
082 |
public boolean isClosed() { return false ;}
|
084 |
public boolean isFirst() { return false ;}
|
086 |
public boolean isLast() { return false ;}
|
088 |
public boolean isNull( int columnIndex) { return false ;}
|
090 |
public boolean move( int offset) { return false ;}
|
092 |
public boolean moveToFirst() { return false ;}
|
094 |
public boolean moveToLast() { return false ;}
|
096 |
public boolean moveToNext() { return false ;}
|
098 |
public boolean moveToPrevious() { return false ;}
|
100 |
public void registerContentObserver(ContentObserver observer) {}
|
102 |
public void registerDataSetObserver(DataSetObserver observer) {}
|
104 |
public boolean requery() { return false ;}
|
106 |
public Bundle respond(Bundle extras) { return null ;}
|
108 |
public void setNotificationUri(ContentResolver cr, Uri uri) {}
|
110 |
public void unregisterContentObserver(ContentObserver observer) {}
|
112 |
public void unregisterDataSetObserver(DataSetObserver observer) {}
|
这个类的实例就可作为AlphaIndexer的构造函数第一个参数数据游标。
4.自定义Adapter的实现
使用前面介绍的东西,我们来实现最终的IndexAdapter:
01 |
class IndexAdapter extends SimpleAdapter implements
SectionIndexer{
|
03 |
private AlphabetIndexer alphabetIndexer;
|
05 |
public IndexAdapter(Context context,List<? extends Map<String, ?>> data, int resource,String[]
from, int [] to) {
|
06 |
super (context, data, resource, from, to);
|
09 |
alphabetIndexer = new AlphabetIndexer( new IndexCursor( this ), 0 , "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
|
13 |
public Object[] getSections() {
|
14 |
return alphabetIndexer.getSections();
|
18 |
public int getPositionForSection( int section) {
|
19 |
return alphabetIndexer.getPositionForSection(section);
|
23 |
public int getSectionForPosition( int position) {
|
24 |
return alphabetIndexer.getSectionForPosition(position);
|
5.跑起来
提供样本数据如下:
01 |
public List<Map<String, String>> getData(){
|
02 |
List<Map<String, String>> itemList = new ArrayList<Map<String, String>>();
|
03 |
String alphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
|
05 |
Map<String, String> map = null ;
|
06 |
for ( char c:alphas.toCharArray()){
|
07 |
for ( int i= 0 ; i< 10 ;
i++){
|
08 |
map = new HashMap<String, String>();
|
09 |
map.put( "itemText" , "" +c+i);
|
子项的布局文件:
01 |
<? xml version = "1.0" encoding = "utf-8" ?>
|
03 |
android:orientation = "vertical"
|
04 |
android:layout_width = "fill_parent"
|
05 |
android:layout_height = "50dip"
|
06 |
android:gravity = "center_vertical"
|
09 |
android:id = "@+id/tweaked_item_text"
|
10 |
android:layout_width = "fill_parent"
|
11 |
android:layout_height = "wrap_content" />
|
使用并运行:
01 |
protected
void onCreate(Bundle savedInstanceState) {
|
02 |
super .onCreate(savedInstanceState);
|
03 |
setContentView(R.layout.tweake_list);
|
05 |
tweakedListView = (ListView)findViewById(R.id.tweaked_list);
|
08 |
List<Map<String, String>> itemList = getData();
|
09 |
ListAdapter adapter = new IndexAdapter( this , itemList, R.layout.tweake_list_item, new String[]{ "itemText" }, new int []{R.id.tweaked_item_text});
|
10 |
tweakedListView.setAdapter(adapter);
|
效果如下:
6.小结
这种索引效果,在大数据量列表显示中非常的实用,是android开发必备常识。
本文只是一个简单的sample,实际工作中肯定会需要进一步扩展定义:
(1).对于复杂类型的处理,可根据Map<String,?>扩展自定义实体类,再通过adapter转换使用即可。
(2).对于索引字母列表,可动态设置,举个例子,你的列表只有ABCD四个字母,如果索引字母列表还是设置“ABCDEFGHIJKLMNOPQRSTUVWXYZ”就不合适了,会有个索引偏位的问题。
(3).对于复杂界面的显示,可重写adapter的getView方法自定义视图。
注意:如果不想出现,右边的辅助拖动块,就 设置android:fastScrollEnabled=false
分享到:
相关推荐
1-oracle培训整套教程(存储过程-函数-触发器,异常处理,游标.存储包)
Transact-SQL语言极其程序设计的方法; T-SQL游标的使用方法。
0203-流程化控制-游标练习题.sql
行业文档-设计装置-纸质游标尺.zip
matlab开发-图窗口的数据游标。fugure中多轴数据光标支持
福建省电力公司oracle培训教材--PLSQL之游标与异常处理
本文对ORA-01000_maximum_open_cursors_exceeded_超出打开游标的最大数的原因和解决方案有详细描述
数据库视图和索引游标实验报告.doc
Android基础画廊 Android Basic Galler是一个简单但实用的照相馆,它使用Cursor Loader与AsycTask一起加载图像,并使用Cursor Adapter在GridView中显示图像。 我已经从Jeff Sharkey的本演示中获得了基本实现,...
circle-text-follow-mousemove:21-0311游标周围旋转字
SQL Server对游标的使用要遵循:声明游标–打开游标–读取数据–关闭游标–删除游标。下面让我们来看看几种常用游标是怎么使用的! 1、只读游标的使用(只能使用next提取数据) --声明一个只读游标 declare cur_stu ...
11-3--游标Cursor1
ora-exploits-evilcursor Oracle Evil 游标注入漏洞利用存储库 - Perl 和 SQL 版本SQL bunkerview.sql ctxsys-drvxtabc-create_tablesV2.sql dbms_cdc_subscribeV2.sql dbms_meta_get_ddlV2.sql kupm...
数据库视图和索引-游标实验报告.doc
索引和游标.ppt
Oracle入门--Oracle游标使用! 值得下载看看!资源免费,大家分享!!
当时自己初学SQL时,对游标很难理解,现在编写了一个实例游标循环,到时写SQL循环时方便些 declare cursor1 cursor for select *from bb --把查询数据给游标 fetch next from cursor1 into @id,@name -- 定义一个...
主要描述oracle中游标的学习隐式游标和显示游标
06-游标操作 07-固定集合 07-索引 08-备份与恢复 08-用户管理 09-mongoDB备份与恢复 09-安全和认证 10-replication复制集 10-主从复制 11-shard分片 11-分片 12-使用java操作mongodb
行业资料-电子功用-微机电结构偏转角测量游标尺