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. gnuWin32-mini-2016.10.30

    2016-10-28 04:48 1,017,856 awk.exe ver 4.1.4 2016-10-29 00:26 77,312 bc.exe ver 1.06 2016-10-30 01:4 ...

  2. 【安装Redis】CentOS7 下安装NodeJs+Express+MongoDB+Redis

    Redis,V3.2,官网l官方链接:http://www.redis.io/download,参考:http://blog.csdn.net/mlks_2008/article/details/19 ...

  3. 鼠标划过用户名时在鼠标右下角显示div展示用户资料

    最近做一个网站论坛,为了方便会员之间相互了解,又不想再做一个页面展示用户资料,就想到了鼠标划过用户名时在鼠标右下角显示div展示用户资料这个效果, 这里要注意的该方法不是给每个用户名的旁边都绑定一个d ...

  4. lockf

    lockf( fd, mode, size ); mode 为 1 时表示加锁,为 0 时表示解锁. #include<stdio.h> #include<unistd.h> ...

  5. 高德地图纯js和html

    <!doctype html> <html> <head> <meta content="" charset="utf-8&qu ...

  6. 让你的 Xcode8 继续使用插件

    发表于 2016-09-10   |   分类于 Tips   |   21条评论   |   阅读次数 8383 随着 iOS10 的正式版即将发布,Xcode8 GM 也在发布会后放出,本文不会涉 ...

  7. maven打包问题

    <build> <finalName>项目名</finalName> <!-- 配置maven打包时过滤的文件 --> <resources> ...

  8. Delphi控件之---UpDown以及其与TEdit的配合使用(比如限制TEdit只能输入数字,还有Object Inspector之组件属性的介绍)

    最近在开发中使用到了UpDown这个控件,但是因为之前没有使用过,所以很不熟悉,于是就编写了一个简单的demo来学习UpDown以及其结合TEdit的用法. 初步的常用功能的简介 目前(2015.08 ...

  9. WebGL入门教程(四)-webgl颜色

    前面文章: WebGL入门教程(一)-初识webgl WebGL入门教程(二)-webgl绘制三角形 WebGL入门教程(三)-webgl动画 颜色效果图: 操作步骤: 1.创建HTML5 canva ...

  10. tcp三次握手、四次挥手

    TCP的三次握手(建立连接)和四次挥手(关闭连接):http://blog.csdn.net/whuslei/article/details/6667471/ TCP协议中的三次握手和四次挥手(图解) ...