SectionIndexer示例
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示例的更多相关文章
- 自定义View(1)简单流程及示例模板
1,继承View , ViewGroup,或TextView等等 2,绘制相关的api, canvas 画布, paint 画笔 2,重写重要的函数(注意这个顺序) onMeasure 属于View的 ...
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...
- 通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...
- WCF学习之旅—第三个示例之四(三十)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) ...
- JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...
- XAMARIN ANDROID 二维码扫描示例
现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile 做一个简单的 Android 条码扫描示 ...
- iOS之ProtocolBuffer搭建和示例demo
这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...
随机推荐
- yii小细节
1.main.php增加导航栏严格区分大小写,否则会出现404错误 2.增加'分页'功能---前后台的models里面的search.php 添加 public function search($pa ...
- 让一个图片在div中居中(四种方法)
第一种方法: <div class="title"> <div class="flag"></div> <div cl ...
- Jquery操作select
<select id="Select1"> <option value="one">一</option> <optio ...
- zTree和SweetAlert插件初探
1.zTree插件简介 zTree是一个依靠 jQuery实现的多功能“树插件”.优异的性能.灵活的配置.多种功能的组合是zTree最大优点.专门适合项目开发,尤其是树状菜单.树状数据的Web显示.权 ...
- Microsoft VS 2008 过期解决方法破解方法
Microsoft VS 2008 过期解决方法电脑上的Microsoft Visual Studio 2008 Team System 试用版提示离过期还有**天.于是百度,搜索结果大多是以下两种解 ...
- vnc远程运行3D游戏
使用的版本:VNC-5.2.3-Windows.exe vnc官网 安装的过程中需要输入license key,以下给出一些enterprise license(最大权限的License): K5 ...
- Duilib源码分析(六)整体流程
在<Duilib源码分析(一)整体框架>.<Duilib源码分析(二)控件构造器—CDialogBuilder>以及<Duilib源码分析(三)XML解析器—CMarku ...
- Redis3重建Cluster
1.关闭cluster全部节点2.删除所有nodes.conf文件3.开启全部节点4.依次flushall5.重建集群即可 Share the post "Redis3重建Cluster&q ...
- [Idea] 在idea中使用jetty debug
1.添加jetty的maven插件 <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId> ...
- Linux 线程管理
解析1 LINUX环境下多线程编程肯定会遇到需要条件变量的情况,此时必然要使用pthread_cond_wait()函数.但这个函数的执行过程比较难于理解. pthread_cond_wait()的工 ...