android仿微信联系人 首字母分类(旧)
效果图实现这个效果,需要三个知识点 :1:将字符串 进行拼音分类拼音2:ExpandableListView 二级扩展列表3:右边字母分类View4:中间的绿色对话框popupWindow首先字母分类代码 获得字符串首字母 首字符 //获得字符串的首字母 首字符 转汉语拼音publicString getFirstChar(String
这篇博客已经更新了:新的地址 http://blog.csdn.net/b275518834/article/details/51381481
效果图
代码:http://download.csdn.net/detail/b275518834/5753931
实现这个效果,需要三个知识点 :
1:将字符串 进行拼音分类
2:ExpandableListView 二级扩展列表
3:右边字母分类View
4:中间的绿色对话框popupWindow
首先字母分类代码 获得字符串首字母 首字符
//获得字符串的首字母 首字符 转汉语拼音
public String getFirstChar(String value) {
// 首字符
char firstChar = value.charAt(0);
// 首字母分类
String first = null;
// 是否是非汉字
String[] print = PinyinHelper.toHanyuPinyinStringArray(firstChar);
if (print == null) {
// 将小写字母改成大写
if ((firstChar >= 97 && firstChar <= 122)) {
firstChar -= 32;
}
if (firstChar >= 65 && firstChar <= 90) {
first = String.valueOf((char) firstChar);
} else {
// 认为首字符为数字或者特殊字符
first = "#";
}
} else {
// 如果是中文 分类大写字母
first = String.valueOf((char)(print[0].charAt(0) -32));
}
if (first == null) {
first = "?";
}
return first;
}
汉字排序
/**
* 汉字排序
* */
public class LanguageComparator_CN implements Comparator<String> {
public int compare(String ostr1, String ostr2) {
for (int i = 0; i < ostr1.length() && i < ostr2.length(); i++) {
int codePoint1 = ostr1.charAt(i);
int codePoint2 = ostr2.charAt(i);
if (Character.isSupplementaryCodePoint(codePoint1)
|| Character.isSupplementaryCodePoint(codePoint2)) {
i++;
}
if (codePoint1 != codePoint2) {
if (Character.isSupplementaryCodePoint(codePoint1)
|| Character.isSupplementaryCodePoint(codePoint2)) {
return codePoint1 - codePoint2;
}
String pinyin1 = pinyin((char) codePoint1);
String pinyin2 = pinyin((char) codePoint2);
if (pinyin1 != null && pinyin2 != null) { // 两个字符都是汉字
if (!pinyin1.equals(pinyin2)) {
return pinyin1.compareTo(pinyin2);
}
} else {
return codePoint1 - codePoint2;
}
}
}
return ostr1.length() - ostr2.length();
}
// 获得汉字拼音的首字符
private String pinyin(char c) {
String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
if (pinyins == null) {
return null;
}
return pinyins[0];
}
}
2级列表展开
//展开所有
for (int i = 0, length = adapter.getGroupCount(); i < length; i++) {
eListView.expandGroup(i);
}
2级列表adapter
package com.cn.demo.pinyin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.pingyin.LanguageComparator_CN;
import net.sourceforge.pinyin4j.PinyinHelper;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class PinyinAdapter extends BaseExpandableListAdapter {
// 字符串
private List<String> strList;
private AssortPinyinList assort = new AssortPinyinList();
private Context context;
private LayoutInflater inflater;
// 中文排序
private LanguageComparator_CN cnSort = new LanguageComparator_CN();
public PinyinAdapter(Context context, List<String> strList) {
super();
this.context = context;
this.inflater = LayoutInflater.from(context);
this.strList = strList;
if (strList == null) {
strList = new ArrayList<String>();
}
long time = System.currentTimeMillis();
// 排序
sort();
Toast.makeText(context,
String.valueOf(System.currentTimeMillis() - time), 1).show();
}
private void sort() {
// 分类
for (String str : strList) {
assort.getHashList().add(str);
}
assort.getHashList().sortKeyComparator(cnSort);
for(int i=0,length=assort.getHashList().size();i<length;i++)
{
Collections.sort((assort.getHashList().getValueListIndex(i)),cnSort);
}
}
public Object getChild(int group, int child) {
// TODO Auto-generated method stub
return assort.getHashList().getValueIndex(group, child);
}
public long getChildId(int group, int child) {
// TODO Auto-generated method stub
return child;
}
public View getChildView(int group, int child, boolean arg2,
View contentView, ViewGroup arg4) {
// TODO Auto-generated method stub
if (contentView == null) {
contentView = inflater.inflate(R.layout.adapter_chat, null);
}
TextView textView = (TextView) contentView.findViewById(R.id.name);
textView.setText(assort.getHashList().getValueIndex(group, child));
return contentView;
}
public int getChildrenCount(int group) {
// TODO Auto-generated method stub
return assort.getHashList().getValueListIndex(group).size();
}
public Object getGroup(int group) {
// TODO Auto-generated method stub
return assort.getHashList().getValueListIndex(group);
}
public int getGroupCount() {
// TODO Auto-generated method stub
return assort.getHashList().size();
}
public long getGroupId(int group) {
// TODO Auto-generated method stub
return group;
}
public View getGroupView(int group, boolean arg1, View contentView,
ViewGroup arg3) {
if (contentView == null) {
contentView = inflater.inflate(R.layout.list_group_item, null);
contentView.setClickable(true);
}
TextView textView = (TextView) contentView.findViewById(R.id.name);
textView.setText(assort.getFirstChar(assort.getHashList()
.getValueIndex(group, 0)));
// 禁止伸展
return contentView;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}
public AssortPinyinList getAssort() {
return assort;
}
}
右边字母View编写
package com.cn.demo.pinyin;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class AssortView extends Button {
public interface OnTouchAssortListener {
public void onTouchAssortListener(String s);
public void onTouchAssortUP();
}
public AssortView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public AssortView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public AssortView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
// 分类
private String[] assort = { "?", "#", "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 Paint paint = new Paint();
// 选择的索引
private int selectIndex = -1;
// 字母监听器
private OnTouchAssortListener onTouch;
public void setOnTouchAssortListener(OnTouchAssortListener onTouch) {
this.onTouch = onTouch;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int height = getHeight();
int width = getWidth();
int interval = height / assort.length;
for (int i = 0, length = assort.length; i < length; i++) {
// 抗锯齿
paint.setAntiAlias(true);
// 默认粗体
paint.setTypeface(Typeface.DEFAULT_BOLD);
// 白色
paint.setColor(Color.WHITE);
if (i == selectIndex) {
// 被选择的字母改变颜色和粗体
paint.setColor(Color.parseColor("#3399ff"));
paint.setFakeBoldText(true);
paint.setTextSize(30);
}
// 计算字母的X坐标
float xPos = width / 2 - paint.measureText(assort[i]) / 2;
// 计算字母的Y坐标
float yPos = interval * i + interval;
canvas.drawText(assort[i], xPos, yPos, paint);
paint.reset();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float y = event.getY();
int index = (int) (y / getHeight() * assort.length);
if (index >= 0 && index < assort.length) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
// 如果滑动改变
if (selectIndex != index) {
selectIndex = index;
if (onTouch != null) {
onTouch.onTouchAssortListener(assort[selectIndex]);
}
}
break;
case MotionEvent.ACTION_DOWN:
selectIndex = index;
if (onTouch != null) {
onTouch.onTouchAssortListener(assort[selectIndex]);
}
break;
case MotionEvent.ACTION_UP:
if (onTouch != null) {
onTouch.onTouchAssortUP();
}
selectIndex = -1;
break;
}
} else {
selectIndex = -1;
if (onTouch != null) {
onTouch.onTouchAssortUP();
}
}
invalidate();
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
}
中间绿色的对话框
//字母按键回调
assortView.setOnTouchAssortListener(new OnTouchAssortListener() {
View layoutView=LayoutInflater.from(MainActivity.this)
.inflate(R.layout.alert_dialog_menu_layout, null);
TextView text =(TextView) layoutView.findViewById(R.id.content);
PopupWindow popupWindow ;
public void onTouchAssortListener(String str) {
int index=adapter.getAssort().getHashList().indexOfKey(str);
if(index!=-1)
{
eListView.setSelectedGroup(index);;
}
if(popupWindow!=null){
text.setText(str);
}
else
{
popupWindow = new PopupWindow(layoutView,
80, 80,
false);
// 显示在Activity的根视图中心
popupWindow.showAtLocation(getWindow().getDecorView(),
Gravity.CENTER, 0, 0);
}
text.setText(str);
}
public void onTouchAssortUP() {
if(popupWindow!=null)
popupWindow.dismiss();
popupWindow=null;
}
});
代码:http://download.csdn.net/detail/b275518834/5753931
字母组顺序修改
[reply]libaojun6[/reply]改下字母组的顺序
public static String[] assort = { "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","?", "#", };
然后把PinyinAdapter56行的字母组排序
assort.getHashList().sortKeyComparator(cnSort);
改成
assort.getHashList().sortKeyComparator(new Comparator<String>() {
public int compare(String arg0, String arg1) {
return getIndex(arg0)-getIndex(arg1);
}
public int getIndex(String str){
for(int i=0;i<AssortView.assort.length;i++)
{
if(AssortView.assort[i].equals(str))
{
return i;
}
}
return -1;
}
});
这篇博客已经更新了:新的地址
http://blog.csdn.net/b275518834/article/details/51381481
更多推荐
所有评论(0)