Android 字母导航条实现
在Activity中进行功能的实现,需要用到第三方jar包:pinyin4j.jar,此jar包用于将汉字转换为汉语拼音。
首先,设置右侧边栏索引列表(A-Z),并且设置列表点击,Touch事件,点击索引,如果在内容列表中存在对应的数据,则自动跳转显示到那一条数据,如果没有则不处理,长按的时候在内容列表中显示当前选择了哪一项;
获取内容列表list,并且获取此列表中联系人对象的名字所对应的第一个字母,将所有的联系人名字第一个字母取出(无重复),然后将两个列表合并,最后根据联系人或者是联系人组目录拼音进行排序比较;
public class ContactsActivity extends Activity { private TextView tv_center = null; private ListView content; // 左边的联系人列表 private ListView lvseek; // 右边的索引列表 private HanyuPinyinOutputFormat spellFormat; // 转换汉字得到其汉语拼音 private List<String> index_list = new ArrayList<String>(); // 联系人列表的组索引列表 private ContactsAdapter adapter; // 联系人列表的适配器
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contacts); init(); } /** * 初始化变量 */ private void init() { lvseek = (ListView) findViewById(R.id.layout); tv_center = (TextView) findViewById(R.id.tv);
spellFormat = new HanyuPinyinOutputFormat(); spellFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); spellFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); spellFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
content = (ListView) findViewById(R.id.listView1); setContent(content); setLvSeekAdapter(lvseek); setLvSeekListener(lvseek); } /** * 设置索引列表的数据源适配器 * * @param lvseek2 */ private void setLvSeekAdapter(ListView lvseek2) { AZAdapter adapter = new AZAdapter(this, getList()); int per_height = getResources().getDisplayMetrics().heightPixels / 26; adapter.setHeight(per_height); lvseek2.setAdapter(adapter); } /** * 设置seekLv事件响应 * * @param lvseek2 */ private void setLvSeekListener(final ListView lvseek2) { // Touch事件,按下和放开两个事件处理 lvseek2.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { tv_center.setVisibility(View.INVISIBLE); lvseek2.setBackgroundColor(Color.parseColor("#FDFDFD")); } else if (event.getAction() == MotionEvent.ACTION_DOWN) { lvseek2.setBackgroundColor(Color.parseColor("#F2F1F0")); } return false; } }); // 长按的时候在屏幕中间显示当前选择的是哪一个索引项 lvseek2.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { String str = (String) parent.getAdapter().getItem(position); tv_center.setVisibility(View.VISIBLE); tv_center.setText(str); int index = adapter.getItemIndex(str); if (index != -1) content.setSelection(index); return false; } });
lvseek2.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String str = (String) parent.getAdapter().getItem(position); int index = adapter.getItemIndex(str); if (index != -1) content.setSelection(index); } }); } /** * 将内容添加到列表 * * @param content2 */ private void setContent(ListView content2) { adapter = new ContactsAdapter(this, getList_content()); content2.setAdapter(adapter); } // 李四, 王五, 张三, 赵六 private List<Object> getList_content() { List<Object> list = new ArrayList<Object>(); List<Contact> contact_list = new ArrayList<Contact>(); contact_list.add(new Contact("张三", null, "")); contact_list.add(new Contact("李四", null, "")); 。。。// 将联系人添加进列表 list.addAll(contact_list); // 获取联系人索引列表 getIndex_List(contact_list); // 将索引列表添加进list; list.addAll(index_list); Collections.sort(list, new PinyinComparator()); return list; } /** * 根据联系人列表获取索引 * * @param list */ private void getIndex_List(List<Contact> list) { for (Contact con : list) { String pinying = PinyinHelper.toHanyuPinyinString(con.getName(), spellFormat, ""); String first_s = pinying.substring(0, 1); String str = first_s.toUpperCase(); if (isA_Z(str)) {// 首字母是a-z if (!index_list.contains(str)) { index_list.add(str); } } } } class PinyinComparator implements Comparator<Object> { // 比较器,在内容列表中有两种类型的数据,所以定义为Object; public int compare(Object o1, Object o2) { String str1; String str2; if (o1.getClass() == Contact.class) { str1 = ((Contact) o1).getName(); } else { str1 = o1.toString(); } if (o2.getClass() == Contact.class) { str2 = ((Contact) o2).getName(); } else { str2 = o2.toString(); } return chineneToSpell(str1).compareTo(chineneToSpell(str2)); } /** * 使用PinYin4j.jar将汉字转换为拼音 * * @param chineseStr * @return */ private String chineneToSpell(String chineseStr) { if (isChinese(chineseStr)) { String pinying = PinyinHelper.toHanyuPinyinString(chineseStr, spellFormat, ""); return pinying.toUpperCase(); } else return chineseStr; } } /** * 加载右边显示栏 * * @return */ private List<String> getList() { return Arrays.asList(Utils.RIGHT_INDEX); } }
至此基本功能已经实现;
补充:最近在学习的过程中,发现了另一种方式,相比前一种方式,更加合理,记录如下:
public class MyTrackView extends View { private OnTrackListener listener; private String bg = "#40000000"; private boolean showbg = false; private Paint paint = new Paint(); private String[] b = new String[] { "#", "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 choose = -1; public MyTrackView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MyTrackView(Context context, AttributeSet attrs) { super(context, attrs); } public MyTrackView(Context context) { super(context); } @Override public boolean dispatchTouchEvent(MotionEvent event) { int item = (int) (event.getY() / getHeight() * b.length); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (item >= 0 && item < b.length) { showbg = true; choose = item; invalidate(); if (listener != null) { listener.getStr(b[item]); } } break; case MotionEvent.ACTION_MOVE: if (item >= 0 && item < b.length) { showbg = true; choose = item; invalidate(); if (listener != null) { listener.getStr(b[item]); } } break; case MotionEvent.ACTION_UP: showbg = false; choose = -1; invalidate(); if (listener != null) { listener.getStr(-1 + ""); } break; } return true; } @Override protected void onDraw(Canvas canvas) { int singleHeight = getHeight() / b.length; if (showbg) { canvas.drawColor(Color.parseColor(bg)); } for (int i = 0; i < b.length; i++) { paint.setColor(Color.WHITE); paint.setAntiAlias(true); float textWidth = paint.measureText(b[i]); if (i == choose) { paint.setColor(Color.RED); paint.setFakeBoldText(true); } canvas.drawText(b[i], (getWidth() - textWidth) / 2, (i + 1) * singleHeight, paint); paint.reset(); } super.onDraw(canvas); } public void setTrackListener(OnTrackListener listener) { this.listener = listener; } interface OnTrackListener { public void getStr(String str); } }
利用pinyin.jar轻松将汉字转换为拼音
public class Main{private HanyuPinyinOutputFormat spellFormat; // 转换汉字得到其汉语拼音 public Main() { spellFormat = new HanyuPinyinOutputFormat(); spellFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); spellFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); spellFormat.setVCharType(HanyuPinyinVCharType.WITH_V); Log.e(chineneToSpell("大家好"), "fuck"); } /** * 使用PinYin4j.jar将汉字转换为拼音 * * @param chineseStr * @return */ private String chineneToSpell(String chineseStr) { if (isChinese(chineseStr)) { String pinying = PinyinHelper.toHanyuPinyinString(chineseStr, spellFormat, ""); return pinying.toUpperCase(); } return chineseStr; } /** * 判断字符串中是否包含有中文 * * @param str * @return */ protected static boolean isChinese(String str) { String regex = "[\\u4e00-\\u9fa5]"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); return matcher.find(); } /** * 判断是否是a-z * * @param str * @return */ protected static boolean isA2Z(String str) { String regex = "[A-Za-z]"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); return matcher.find(); } }