Android SectionIndexer 的使用(联系人分类索引)
// 获取标题栏索引
int position = sectionIndexter.getPositionForSection(l[idx]);
if (position == -) {
return true;
}
// 设置调整到指定区域
list.setSelection(position);
1. Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<android.test.SideBar
android:id = "@+id/sideBar"
android:layout_height="fill_parent"
android:layout_width="22px"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
2. listview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/section"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="80sp"
android:textSize="45sp"
/>
</LinearLayout>
3. MyAdapter.Java
package android.test;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter implements SectionIndexer {
private ArrayList<String> stringArray;
private Context context;
public MyAdapter(Context _context, ArrayList<String> arr) {
stringArray = arr;
context = _context;
}
public int getCount() {
return stringArray.size();
}
public Object getItem(int arg0) {
return stringArray.get(arg0);
}
public long getItemId(int arg0) {
return ;
}
public View getView(int position, View v, ViewGroup parent) {
LayoutInflater inflate = ((Activity) context).getLayoutInflater();
View view = (View) inflate.inflate(R.layout.listview_row, null);
LinearLayout header = (LinearLayout) view.findViewById(R.id.section);
String label = stringArray.get(position);
char firstChar = label.toUpperCase().charAt();
if (position == ) {
setSection(header, label);
} else {
String preLabel = stringArray.get(position - );
char preFirstChar = preLabel.toUpperCase().charAt();
if (firstChar != preFirstChar) {
setSection(header, label);
} else {
header.setVisibility(View.GONE);
}
}
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(label);
return view;
}
private void setSection(LinearLayout header, String label) {
TextView text = new TextView(context);
header.setBackgroundColor(0xffaabbcc);
text.setTextColor(Color.WHITE);
text.setText(label.substring(, ).toUpperCase());
text.setTextSize();
text.setPadding(, , , );
text.setGravity(Gravity.CENTER_VERTICAL);
header.addView(text);
}
public int getPositionForSection(int section) {
if (section == ) {
return ;
}
for (int i = ; i < stringArray.size(); i++) {
String l = stringArray.get(i);
char firstChar = l.toUpperCase().charAt();
if (firstChar == section) {
return i;
}
}
return -;
}
public int getSectionForPosition(int arg0) {
return ;
}
public Object[] getSections() {
return null;
}
}
4. SideBar.java
package android.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.SectionIndexer;
public class SideBar extends View {
private char[] l;
private SectionIndexer sectionIndexter = null;
private ListView list;
private final int m_nItemHeight = ;
public SideBar(Context context) {
super(context);
init();
}
public SideBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
l = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
setBackgroundColor(0x44FFFFFF);
}
public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void setListView(ListView _list) {
list = _list;
sectionIndexter = (SectionIndexer) _list.getAdapter();
}
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int i = (int) event.getY();
int idx = i / m_nItemHeight;
if (idx >= l.length) {
idx = l.length - ;
} else if (idx < ) {
idx = ;
}
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
if (sectionIndexter == null) {
sectionIndexter = (SectionIndexer) list.getAdapter();
}
int position = sectionIndexter.getPositionForSection(l[idx]);
if (position == -) {
return true;
}
list.setSelection(position);
}
return true;
}
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(0xFFA6A9AA);
paint.setTextSize(getMeasuredHeight()/);
paint.setTextAlign(Paint.Align.CENTER);
float widthCenter = getMeasuredWidth() / ;
for (int i = ; i < l.length; i++) {
canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint);
}
super.onDraw(canvas);
}
}
5. Main.java
package android.test;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.myListView);
ArrayList<String> stringList = InitListViewData();
MyAdapter adapter = new MyAdapter(this, stringList);
list.setAdapter(adapter);
SideBar indexBar = (SideBar) findViewById(R.id.sideBar);
indexBar.setListView(list);
}
private ArrayList<String> InitListViewData() {
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("aback");
stringList.add("abash");
stringList.add("abbey");
stringList.add("abhor");
stringList.add("abide");
stringList.add("abuse");
stringList.add("candidate");
stringList.add("capture");
stringList.add("careful");
stringList.add("catch");
stringList.add("cause");
stringList.add("celebrate");
stringList.add("forever");
stringList.add("fable");
stringList.add("fidelity");
stringList.add("fox");
stringList.add("funny");
stringList.add("fail");
stringList.add("jail");
stringList.add("jade");
stringList.add("jailor");
stringList.add("january");
stringList.add("jasmine");
stringList.add("jazz");
stringList.add("zero");
stringList.add("zoo");
stringList.add("zeus");
stringList.add("zebra");
stringList.add("zest");
stringList.add("zing");
return stringList;
}
}
Android SectionIndexer 的使用(联系人分类索引)的更多相关文章
- 开源 iOS 项目分类索引大全 - 待整理
开源 iOS 项目分类索引大全 GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Cate ...
- Android-自定义联系人快速索引
效果图: 布局去指定 view.custom.shangguigucustomview.MyCustomIndexView 自定义View对象 <!-- 自定义联系人快速索引 --> &l ...
- Android项目——读取手机联系人信息
加入读取联系人信息的权限 <uses-permission android:name="android.permission.READ_CONTACTS"/> cont ...
- Android实现SQLite数据库联系人列表
Android实现SQLite数据库联系人列表 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个通讯录查看程序: 要求使用SQLite ...
- Android开发之读写联系人
读写联系人需要用到android的ContentProvider 同时需要读和写联系人的权限 需要使用到联系人数据库中的 * raw_contacts表: * contact_id:联系人id * d ...
- Android向通讯录添加联系人的一般方法
Android向通讯录添加联系人的一般方法 以一个简单的例子加以说明,记得需要相应的权限: 测试代码,关键的内容就在add函数里面. package zhangphil.demo; import an ...
- [android] 获取系统的联系人信息
内容提供是实质上是个接口,后门,他给别人提供数据,系统联系人是个比较复杂的内容通过者. 找到/data/data/com.android.providers.contacts/contacts2.db ...
- Android 手机卫士--获取联系人信息并显示与回显
前面的文章已经实现相关的布局,本文接着进行相关的功能实现 本文地址:http://www.cnblogs.com/wuyudong/p/5951794.html,转载请注明出处. 读取系统联系人 当点 ...
- 开源 iOS 项目分类索引大全
GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Category类型的库,附带很多自定义 ...
随机推荐
- hdu2018java
母牛的故事 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- Shell脚本编程入门(一) 分类: 学习笔记 linux ubuntu 2015-07-09 21:06 29人阅读 评论(0) 收藏
最近在学shell,记录一下. if语句的使用: 1.判断两个参数大小 #!/bin/sh #a test about if statement a=10 b=20 if [ $a -eq $b ]; ...
- AndroidStudio1.4 manifest 中注册Activity时的错误提示解决办法
问题截图如下: 解决办法截图如下: 1: File->setting->Editor->Language Injections到如下界面 2:双击右侧选中的Item进入编辑界面 3: ...
- CSS Positioning(定位)
Positioning(定位) CSS定位属性允许你为一个元素定位.它也可以将一个元素放在另一个元素后面,并指定一个元素的内容太大时,应该发生什么. 元素可以使用的顶部,底部,左侧和右侧属性定位.然而 ...
- ZOJ 1733 Common Subsequence(LCS)
Common Subsequence Time Limit: 2 Seconds Memory Limit: 65536 KB A subsequence of a given sequen ...
- 【PHP】 foreach循环中变量引用的一道面试题
$a = array('a','b','c'); foreach($a as &$v){} foreach($a as $v){ } var_dump($a); 现在.不要打开浏览器,猜测一下 ...
- js获取css属性方法
function getDefaultStyle(obj,attribute){ return obj.currentStyle?obj.currentStyle[attribute] : docum ...
- C#.Net网页加载等待效果漂亮并且简单
最近网页加载数据比较多,点击后给用户就是白板很不友好,想了很久找了些资料,在网页加载中显示等待画面给客户,页面加载完成自动隐藏等待效果. 在网页后台cs代码: protected void Pa ...
- Jquery 和 Js
jQuery [一] jQuery 1: javaScript和 javaScript库: (1) javaScript自身存在的3个弊端:复杂的文档对象模型(DOM),不 ...
- Apache 支持PHP
①加载PHP模块到Apache中: LoadModule php5_module "d:\php5\php5apache2_2.dll" ②加入识别扩展名为.php文件(也可以 ...