侧边字母索引在通讯录、地区选择之类的长listview中比较实用。
自定义View
public class SectionIndexBar extends View {
//索引
private String[] indexs = {"#", "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"};
private int selectIndex = -1;
private Paint paintNormal;
private Paint paintSelect;
//普通色
private int colorNormal = Color.WHITE;
//选中色
private int colorSelect = Color.BLACK;
//字体大小
private float textSize = 40f;
private int mWidth;
private int itemHeight;
//背景色
private int colorBackground = Color.GRAY;
private OnIndexListener onIndexListener;
public void setOnIndexListener(OnIndexListener onIndexListener) {
this.onIndexListener = onIndexListener;
}
public interface OnIndexListener {
void onIndexSelect(String str);
void onIndexChange(String str);
}
public SectionIndexBar(Context context) {
this(context, null);
}
public SectionIndexBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setClickable(true);
paintNormal = new Paint();
paintNormal.setColor(colorNormal);
paintNormal.setAntiAlias(true);
paintNormal.setTextSize(textSize);
paintSelect = new Paint();
paintSelect.setColor(colorSelect);
paintNormal.setAntiAlias(true);
paintSelect.setTextSize(textSize);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置背景
setBackgroundColor(colorBackground);
mWidth = getWidth();
itemHeight = getHeight() / indexs.length;
drawChars(canvas);
invalidate();
}
/**
* 绘制字母
*
* @param canvas
*/
private void drawChars(Canvas canvas) {
for (int i = 0; i < indexs.length; i++) {
if (selectIndex == i) {
//绘制选中字母
canvas.drawText(indexs[i], (mWidth - paintNormal.measureText(indexs[i])) / 2, (i + 1) * itemHeight, paintSelect);
} else {
//绘制普通字母
canvas.drawText(indexs[i], (mWidth - paintSelect.measureText(indexs[i])) / 2, (i + 1) * itemHeight, paintNormal);
}
}
}
/**
* 手势控制
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
int count = (int) event.getY() / itemHeight;
count = count < 0 ? 0 : (count >= indexs.length ? indexs.length - 1 : count);
selectIndex = count;
if (onIndexListener != null) {
onIndexListener.onIndexChange(indexs[selectIndex]);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
if (onIndexListener != null) {
onIndexListener.onIndexSelect(indexs[selectIndex]);
}
}
return super.onTouchEvent(event);
}
}
获取联系人
public class ContactUtil {
private static final String[] PHONES_PROJECTION = {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
/**
* 联系人显示名称
**/
private static final int PHONES_DISPLAY_NAME_INDEX = 0;
/**
* 电话号码
**/
private static final int PHONES_NUMBER_INDEX = 1;
/**
* 得到所有的手机号码联系人信息
**/
public static List<PhoneContact> getAllPhoneContacts(Context context) {
List<PhoneContact> phoneContactList = getPhoneContact(context);
List<PhoneContact> simContactList = getSIMContacts(context);
for (PhoneContact contact : simContactList) {
if (!isContainObject(phoneContactList, contact)) {
phoneContactList.add(contact);
}
}
return phoneContactList;
}
/*
* 某个联系人是否已经存在于联系人列表中
*/
public static boolean isContainObject(List<PhoneContact> phoneContactList, PhoneContact contact) {
for (int i = 0; i < phoneContactList.size(); i++) {
PhoneContact curContact = phoneContactList.get(i);
if (curContact.getMobile().equals(contact.getMobile()) && curContact.getName().equals(contact.getName())) {
return true;
}
}
return false;
}
/**
* 获取手机通讯录联系人
*
* @param context
* @return
*/
public static List<PhoneContact> getPhoneContact(Context context) {
List<PhoneContact> list = new ArrayList<PhoneContact>();
try {
ContentResolver contentResolver = context.getContentResolver();
Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = contentResolver.query(contentUri, PHONES_PROJECTION, null, null, null);
while (cursor != null ? cursor.moveToNext() : false) {
String name = cursor.getString(0);
String mobile = cursor.getString(1).replace("+86", "").replace(" ", "").replace("-", "");
String namePinYin = PinYinUtil.getPinYin(name).toUpperCase(Locale.US);
PhoneContact contactInfo = new PhoneContact(name, mobile, namePinYin);
list.add(contactInfo);
}
cursor.close();
} catch (Exception e) {
}
return list;
}
/**
* 得到手机SIM卡联系人人信息
**/
private static List<PhoneContact> getSIMContacts(Context context) {
List<PhoneContact> list = new ArrayList<PhoneContact>();
try {
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Uri.parse("content://icc/adn");
Cursor cursor = contentResolver.query(uri, PHONES_PROJECTION, null, null, null);
while (cursor != null ? cursor.moveToNext() : false) {
String name = cursor.getString(PHONES_DISPLAY_NAME_INDEX);
String mobile = cursor.getString(PHONES_NUMBER_INDEX).replace("+86", "").replace(" ", "").replace("-", "");
String namePinYin = PinYinUtil.getPinYin(name).toUpperCase(Locale.US);
PhoneContact contactInfo = new PhoneContact(name, mobile, namePinYin);
list.add(contactInfo);
}
cursor.close();
} catch (Exception ex) {
}
return list;
}
}
源码