This small tutorial will show you how to create a ListView, enable fast scrolling, and create a alphabetical section list that displays the letter as you quickly scroll the list.

这个简单的教程会教你创建一个ListView,实现快速滑动,并且创建一个按字母排序的分类的列表。当你快速滑动的时候,就会显示一个字母,表示当前滚动的位置。

First lets create a layout file with a ListView in it list this

首先创建一个存在ListView的布局文件

代码如下:

 <?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"> <ListView
android:id="@+id/thelist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
android:padding="2dp">
</ListView>
</LinearLayout>

Notice I am putting a background image on the layout and setting the ListView to be transparent with the cacheColorHint. I also set fastScrollEnabled to true. Now I have a store object to store the attributes about a store.

请注意我为整个布局设置了一个背景图片,用cacheColorHint属性吧Listview设置为透明。我也把fastScrollEnabled 属性设置为true了。 接下来我用一个store对象存储了一些关于store的属性。

public class Store {

    public int id;
public String name;
public String direction;
public int floor;
public String address;
public String category;
public String phone;
}

Finally we need to create the Activity. We will assign a custom ArrayAdapter to the list and the custom ArrayAdapter will implement SectionIndexer
最后,我们需要创建一个Activity。我们需要指派一个实现了SectionIndexer接口的自定义的ArrayAdapter给这个ListView。

/**
* The Store List Activity
*/
public class StoreListActivity extends Activity {
private DBAdapter db; private LinkedList<Store> storeList = new LinkedList<Store>();
private StoreListAdaptor storeListAdaptor; private ListView list; /**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.storelist); list = (ListView) findViewById(R.id.thelist); // populate the store list
storeList = db.getAllStoresOrderByName(storeList); // create an adaptor with the store list
storeListAdaptor = new StoreListAdaptor(this,storeList); // assign the listview an adaptor
list.setAdapter(storeListAdaptor); } /**
* The List row creator
*/
class StoreListAdaptor extends ArrayAdapter<Store> implements SectionIndexer{ HashMap<String, Integer> alphaIndexer;
String[] sections; public StoreListAdaptor(Context context, LinkedList<Store> items) {
super(context, R.layout.storerow, items); alphaIndexer = new HashMap<String, Integer>();
int size = items.size(); for (int x = 0; x < size; x++) {
Store s = items.get(x); // get the first letter of the store
String ch = s.name.substring(0, 1);
// convert to uppercase otherwise lowercase a -z will be sorted after upper A-Z
ch = ch.toUpperCase(); // HashMap will prevent duplicates
alphaIndexer.put(ch, x);
} Set<String> sectionLetters = alphaIndexer.keySet(); // create a list from the set to sort
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); Collections.sort(sectionList); sections = new String[sectionList.size()]; sectionList.toArray(sections);
} public View getView(int position, View convertView, ViewGroup parent) {
// expand your row xml if you are using custom xml per row
} public int getPositionForSection(int section) {
return alphaIndexer.get(sections[section]);
} public int getSectionForPosition(int position) {
return 1;
} public Object[] getSections() {
return sections;
}
}
}

SectionIndexer示例的更多相关文章

  1. 自定义View(1)简单流程及示例模板

    1,继承View , ViewGroup,或TextView等等 2,绘制相关的api, canvas 画布, paint 画笔 2,重写重要的函数(注意这个顺序) onMeasure 属于View的 ...

  2. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  3. .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1

    微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...

  4. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  5. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九)   ...

  6. JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

  7. XAMARIN ANDROID 二维码扫描示例

    现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile  做一个简单的 Android 条码扫描示 ...

  8. iOS之ProtocolBuffer搭建和示例demo

    这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...

  9. Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)

    示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...

随机推荐

  1. mybatis调用存储过程 无参、带有输入输出参数,输出游标类型的 存储

    存储过程在小公司用的不多,但是如果业务比较复杂或者性能要求比较苛刻的时候存储过程就派上用场了,ibatis的前期的一些版本貌似不支持存储过程因此我选择了mybatis来做实验. 1.无输入和输出参数的 ...

  2. jquery 双向select控件bootstrap Dual listbox

    http://www.cnblogs.com/hangwei/p/5040866.html       -->jquery 双向select控件bootstrap Dual listboxhtt ...

  3. javaSE基础06

    javaSE基础06 一.匿名对象 没有名字的对象,叫做匿名对象. 1.2匿名对象的使用注意点: 1.我们一般不会用匿名对象给属性赋值的,无法获取属性值(现阶段只能设置和拿到一个属性值.只能调用一次方 ...

  4. swift之inout

    在swift中,我们常常对数据进行一些处理.因为swift的计算属性,所以如果不是大量重复性处理,基本可以在set及didSet中改变原数据的一些状态.但需要用到同样的算法处理大量数据的时候,仍然需要 ...

  5. lattice 与 modelsim 仿真 笔记

    对于 lattice  Diamond 与 modelsim 的联合仿真,我总结了一句话,那就是—— 难者不会,会者不难.  也许刚开始 觉得 摸不着 头脑,但是 一旦学会 感觉还是很简单和直观的. ...

  6. opencv计算运行时间

    double Time = (double)cvGetTickCount();//  算法过程Time = (double)cvGetTickCount() - Time ; printf( &quo ...

  7. APP产品交互设计资源汇总(不断更新中...)

    Axure RP 7 元件库 http://www.iaxure.com/tag/axure7-0%E5%85%83%E4%BB%B6%E5%BA%93 ios8组合元件库V1.2版 http://w ...

  8. [leetcode]leetcode初体验

    这几天把之前的设计模式回顾了一遍,整理了一点以前的项目.同学说,打算刷leetcode题目,也勾起了我的兴趣,索性也刷一些题目,再提高一些内功.刚开始进去,leetcode随机分配的题目,直接也就做了 ...

  9. PostgreSQL中标准的SQL boolean数据类型

  10. GDB的常用命令

    定断点b line.会返回一个断点号(breakpoint-no). 输出p val.可以夹杂类型装换.解引用. 遇到断点自动执行命令commands breakpoint-no. 停止执行s. 退出 ...