本文实例讲述了Android开发之自定义view实现通讯录列表A~Z字母提示效果。分享给大家供大家参考,具体如下:
开发工具:eclipse
运行环境:htc G9 android2.3.3
话不多说,先看效果图
其实左右边的A~Z是一个自定义的View,它直接覆盖在ListView上。
MyLetterListView:
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); } }
然后我在Activity中OnTouchingLetterChangedListener中监听手指触摸到了哪个字母,然后让列表跳转到对应的位置,
弹出首字母提示框:
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); } }
还有关于解析汉子的首字母拼音的问题,我这里是查的系统数据库,里面正好有sort_key这一列,比如名字是张三,那么他对应的sort_key就是:ZHANG张SAN三,这样一来就容易多了:
//获得汉语拼音首字母 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]+{1}quot;); if (pattern.matcher(c + "").matches()) { return (c + "").toUpperCase(); } else { return "#"; } }
如果你的数据不是从联系人表中查的,那可以使用第三方jar包,就是pinyin4j-2.5.0。
activity代码和布局文件比较长,我就不在这里贴了。
附:demo源码点击此处本站下载。
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作json格式数据技巧总结》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。