功能的实现结合了网上很多非常的好的实现,小小的优化看下。
实现思路:
1. 获取手机联系人列表:通过Uri uri = Uri.parse("content://com.android.contacts/data/phones"); 查询联系人列表
2 联系人的字段非常的多,摘取了NAME = "name", NUMBER = "number", SORT_KEY = "sort_key";姓名,电话号码,以及非常重要的sort_key 字段,
在之前做项目,而是通过导入的拼音包转化的,这是看网上学的,非常的不错。
3 得到联系列表,则通过listview显示,显示的时候进行A--Z分组显示, 这个也好办,因为通过sort_key已经得到了从A--Z的排序,只要判断是否属于同一字符下,是则不显示,不是则显示,同时把该条设置分组标题
4 安字母分组显示完全后,发现联系人一多,定位到某一个分组下也很大的力气,上面加上一个搜索的话,又懒得输入,因为手机嘛,操作不变,如果能选择,并且直接定位到
该分组下面,那就容易了,因为人再懒,让你去摸一下还是不费力气的,并且体验的效果也不错
所以废话不多说,就是在右边需要一个A--z竖直排列的字母表,能选择,并且能够定位到该字母的分组。
定位到分组这还容易,用为listview为咋门提供了方法setSelection(postion)就ok 能定位了
关键是实现右边的点击,并且知道你点击的字母:
5 。 首先是布局,布局方式很多,直接帧布局把他定义在右边,但发现相对布局也行,而且更简单
package com.droid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.droid.MyLetterListView.OnTouchingLetterChangedListener;
public class ContactList extends Activity {
private BaseAdapter adapter;
private ListView personList;
private TextView overlay;
private MyLetterListView letterListView;
private AsyncQueryHandler asyncQuery;
private static final String NAME = "name", NUMBER = "number", SORT_KEY = "sort_key";
private HashMap<String, Integer> alphaIndexer;//存放存在的汉语拼音首字母和与之对应的列表位置
private String[] sections;//存放存在的汉语拼音首字母
private Handler handler;
private OverlayThread overlayThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
personList = (ListView) findViewById(R.id.list_view);
letterListView = (MyLetterListView) findViewById(R.id.MyLetterListView01);
letterListView.setOnTouchingLetterChangedListener(new LetterListViewListener());
asyncQuery = new MyAsyncQueryHandler(getContentResolver());
alphaIndexer = new HashMap<String, Integer>();
handler = new Handler();
overlayThread = new OverlayThread();
initOverlay();
}
@Override
protected void onResume() {
super.onResume();
Uri uri = Uri.parse("content://com.android.contacts/data/phones");
String[] projection = { "_id", "display_name", "data1", "sort_key" };
asyncQuery.startQuery(0, null, uri, projection, null, null,
"sort_key COLLATE LOCALIZED asc");
}
//异步查询联系人
private class MyAsyncQueryHandler extends AsyncQueryHandler {
public MyAsyncQueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
if (cursor != null && cursor.getCount() > 0) {
List<ContentValues> list = new ArrayList<ContentValues>();
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
ContentValues cv = new ContentValues();
cursor.moveToPosition(i);
String name = cursor.getString(1);
String number = cursor.getString(2);
String sortKey = cursor.getString(3);
System.out.println(sortKey);
if (number.startsWith("+86")) {
cv.put(NAME, name);
cv.put(NUMBER, number.substring(3)); //去掉+86
cv.put(SORT_KEY, sortKey);
} else {
cv.put(NAME, name);
cv.put(NUMBER, number);
cv.put(SORT_KEY, sortKey);
}
list.add(cv);
}
if (list.size() > 0) {
setAdapter(list);
}
}
}
}
private void setAdapter(List<ContentValues> list) {
adapter = new ListAdapter(this, list);
personList.setAdapter(adapter);
}
private class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<ContentValues> list;
public ListAdapter(Context context, List<ContentValues> list) {
this.inflater = LayoutInflater.from(context);
this.list = list;
alphaIndexer = new HashMap<String, Integer>();
sections = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
//当前汉语拼音首字母
String currentStr = getAlpha(list.get(i).getAsString(SORT_KEY));
//上一个汉语拼音首字母,如果不存在为“ ”
String previewStr = (i - 1) >= 0 ? getAlpha(list.get(i - 1).getAsString(SORT_KEY)) : " ";
if (!previewStr.equals(currentStr)) {
String name = getAlpha(list.get(i).getAsString(SORT_KEY));
alphaIndexer.put(name, i);
sections[i] = name;
}
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.alpha = (TextView) convertView.findViewById(R.id.alpha);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.number = (TextView) convertView.findViewById(R.id.number);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ContentValues cv = list.get(position);
holder.name.setText(cv.getAsString(NAME));
holder.number.setText(cv.getAsString(NUMBER));
String currentStr = getAlpha(list.get(position).getAsString(SORT_KEY));//当前字母
String previewStr = (position - 1) >= 0 ? getAlpha(list.get(position - 1).getAsString(SORT_KEY)) : " ";
if (!previewStr.equals(currentStr)) {
holder.alpha.setVisibility(View.VISIBLE);
holder.alpha.setText(currentStr);
} else {
holder.alpha.setVisibility(View.GONE);
}
return convertView;
}
private class ViewHolder {
TextView alpha;
TextView name;
TextView number;
}
}
//初始化汉语拼音首字母弹出提示框
private void initOverlay() {
LayoutInflater inflater = LayoutInflater.from(this);
overlay = (TextView) inflater.inflate(R.layout.overlay, null);
overlay.setVisibility(View.INVISIBLE);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT);
WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
windowManager.addView(overlay, lp);
}
private class LetterListViewListener implements OnTouchingLetterChangedListener{
@Override
public void onTouchingLetterChanged(final String s) {
if(alphaIndexer.get(s) != null) {
int position = alphaIndexer.get(s);
personList.setSelection(position);
overlay.setText(sections[position]);
overlay.setVisibility(View.VISIBLE);
handler.removeCallbacks(overlayThread);
//延迟一秒后执行,让overlay为不可见
handler.postDelayed(overlayThread, 1500);
}
}
}
//设置overlay不可见
private class OverlayThread implements Runnable {
@Override
public void run() {
overlay.setVisibility(View.GONE);
}
}
//获得汉语拼音首字母
private String getAlpha(String str) {
if (str == null) {
return "#";
}
if (str.trim().length() == 0) {
return "#";
}
char c = str.trim().substring(0, 1).charAt(0);
// 正则表达式,判断首字母是否是英文字母
Pattern pattern = Pattern.compile("^[A-Za-z]+$");
if (pattern.matcher(c + "").matches()) {
return (c + "").toUpperCase();
} else {
return "#";
}
}
}
package com.droid;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.style.TypefaceSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MyLetterListView extends View {
OnTouchingLetterChangedListener onTouchingLetterChangedListener;
String[] b = {"#","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"};
int choose = -1;
Paint paint = new Paint();
boolean showBkg = false;
public MyLetterListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyLetterListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLetterListView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(showBkg){
canvas.drawColor(Color.parseColor("#40000000"));
}
int height = getHeight();
int width = getWidth();
int singleHeight = height / b.length;
for(int i=0;i<b.length;i++){
paint.setColor(Color.WHITE);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
if(i == choose){
paint.setColor(Color.parseColor("#3399ff"));
paint.setFakeBoldText(true);
}
float xPos = width/2 - paint.measureText(b[i])/2;
float yPos = singleHeight * i + singleHeight;
canvas.drawText(b[i], xPos, yPos, paint);
paint.reset();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y/getHeight()*b.length);
switch (action) {
case MotionEvent.ACTION_DOWN:
showBkg = true;
if(oldChoose != c && listener != null){
if(c > 0 && c< b.length){
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_MOVE:
if(oldChoose != c && listener != null){
if(c > 0 && c< b.length){
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
showBkg = false;
choose = -1;
invalidate();
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
public interface OnTouchingLetterChangedListener{
public void onTouchingLetterChanged(String s);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/alpha"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="13dip"
android:background="#333333"
android:textColor="#99CCFF"
android:textAppearance="?android:textAppearanceMedium"
android:visibility="gone" />
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5.0dip"
android:src="@drawable/contact_list_icon"
android:layout_below="@id/alpha" />
<TextView
android:id="@+id/name"
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2.0dip"
android:layout_marginTop="6.0dip"
android:layout_marginRight="5.0dip"
android:singleLine="true"
android:layout_toRightOf="@id/image_view"
android:layout_alignTop="@id/image_view" />
<TextView
android:id="@+id/number"
android:textAppearance="?android:textAppearanceSmall"
android:ellipsize="marquee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/list_view" android:layout_height="wrap_content" android:layout_width="fill_parent" android:scrollbars="none" android:cacheColorHint="#00000000" /> <com.droid.MyLetterListView android:id="@+id/MyLetterListView01" android:background="#40000000" android:layout_width="30dip" android:layout_height="fill_parent" android:layout_alignParentRight="true" /> </RelativeLayout>
list_item.xml<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:textSize="70sp" android:textColor="#3399ff" android:background="#ffffff" android:minWidth="80dip" android:maxWidth="80dip" android:padding="5dip" android:gravity="center" />
main.xml
overlay.xml