欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发之自定义view实现通讯录列表A~Z字母提示效果【附demo源码下载】

程序员文章站 2023-01-26 21:56:41
本文实例讲述了android开发之自定义view实现通讯录列表a~z字母提示效果。分享给大家供大家参考,具体如下: 开发工具:eclipse 运行环境:htc g9 a...

本文实例讲述了android开发之自定义view实现通讯录列表a~z字母提示效果。分享给大家供大家参考,具体如下:

开发工具:eclipse

运行环境:htc g9 android2.3.3

话不多说,先看效果图

Android开发之自定义view实现通讯录列表A~Z字母提示效果【附demo源码下载】

Android开发之自定义view实现通讯录列表A~Z字母提示效果【附demo源码下载】

其实左右边的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程序设计有所帮助。