当前位置: 移动技术网 > 移动技术>移动开发>Android > Android仿微信联系人字母排序效果

Android仿微信联系人字母排序效果

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享了android联系人字母排序的具体代码,供大家参考,具体内容如下

实现思路:首先说下布局,整个是一个相对布局,最下面是一个listview,listview上面是一个自定义的view(右边显示字母),最上面是一个textview(屏幕中间的方块)。

首先说一下右边自定义view,字母是画到view上面的,首先计算一下view的高度,然后除以存放字母数组的长的,得到每个字符的高度;每个字母的宽度都是一样的,所以这里直接设置30sp;

listview显示的是108个梁山好汉的名字;

项目里面使用了一个pinyin4j.jar把每个名字转换为拼音,然后使用charat(0)获得拼音的第一个字母;

listview的每个item是一个线性布局包裹的两个textview,上面的tv显示的是拼音的第一个字母,下面的tv显示的就是名字;在adapter判断当前条目和上一个条目的拼音首字母是否相同,如果相同隐藏当前item上面的tv;

然后在自定义view设置一个自定义监听;当点击自定义view的时候根据高度判断当前点击的是那个字母,然后根据字母设置listview要跳转的位置;

首先看下布局:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" > 
 
  <listview 
    android:id="@+id/listview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:cachecolorhint="@android:color/transparent" > 
  </listview> 
 
  <com.wxcontacts.www.view.quickindexbar 
    android:id="@+id/quickindexbar" 
    android:layout_width="30dp" 
    android:layout_height="match_parent" 
    android:layout_alignparentright="true" 
     /> 
   
  <!-- 默认隐藏,当点击自定义view的时候显示 --> 
  <textview  
    android:visibility="gone" 
    android:id="@+id/tv_hint" 
    android:gravity="center" 
    android:layout_centerinparent="true" 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:background="@drawable/bg_text" 
    android:text="a" 
    android:textsize="24sp"/> 
 
</relativelayout> 

先不着急看mainactivity代码首先看下自定义view的代码:

//自定义view 
public class quickindexbar extends view{ 
 
  //画笔 
  private paint paint; 
   
  string[] lettres={"↑","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","↓"}; 
 
  //自定义view的宽高 
  private int viewhight; 
  private int viewwidth; 
  //每个文本的高度;(文本高度等于view高度除以文本个数) 
  private int cellheight; 
 
  //文本的高度 
  private float textheight; 
 
  private int currentindex=-1; 
 
  public onletterchangelisten onletterchangelisten; 
   
  public onletterchangelisten getonletterchangelisten(){ 
    return onletterchangelisten; 
  } 
   
   
  public void setonletterchangelistener(onletterchangelisten onletterchangelisten){ 
    this.onletterchangelisten=onletterchangelisten; 
  } 
  //自定义一个字母改变的监听 
  public interface onletterchangelisten{ 
    void onletterchange(string letter); 
    void onresert(); 
  } 
   
  public quickindexbar(context context) { 
    this(context,null); 
  } 
  public quickindexbar(context context, attributeset attrs) { 
    this(context, attrs,0); 
  } 
 
  public quickindexbar(context context, attributeset attrs, int defstyle) { 
    super(context, attrs, defstyle); 
    paint=new paint(); 
    //设置字体颜色;默认为黑色;我们这里使用黑色 
    //paint.setcolor(color.white); 
    //设置字体大小 
    paint.settextsize(20); 
    //抗锯齿 
    paint.setantialias(true); 
    //获取字体宽高,因为每个字体宽度不一样,所以获得宽度必须放在循环中做 
    //首先获取字体的属性 
    fontmetrics fontmetrics = paint.getfontmetrics(); 
    //下边界 - 上边界 
    textheight = (float) math.ceil(fontmetrics.descent-fontmetrics.ascent); 
  } 
   
  @override 
  protected void ondraw(canvas canvas) { 
    super.ondraw(canvas); 
    //获取文本的宽高 
//   gettextwh(); 
    //每个文本的高度; 
    cellheight=viewhight/lettres.length; 
     
    //通过循环把字母画出来 
    for(int x=0;x<lettres.length;x++){ 
      string text=lettres[x]; 
       
      //paint给我们提供了一个测量字体宽度的方法 
      float textwidth = paint.measuretext(text); 
      if(currentindex==x){ 
        //当点击某个字母的时候变色 
        paint.setcolor(color.gray); 
      }else{ 
        paint.setcolor(color.black); 
      } 
       
      /* 
       * 参数1:要画的内容   参数23:要画的位置   参数4:画笔 
       * 参数23所画的位置指的是字母左下角坐标 
       */ 
      canvas.drawtext(text,viewwidth/2-textwidth/2,cellheight/2+textheight/2+cellheight*x,paint); 
    } 
     
  } 
   
  //测量view的宽高 
  @override 
  protected void onsizechanged(int w, int h, int oldw, int oldh) { 
    super.onsizechanged(w, h, oldw, oldh); 
    viewhight =getmeasuredheight(); 
    viewwidth =getmeasuredwidth(); 
  } 
   
  @override 
  public boolean ontouchevent(motionevent event) { 
    switch (event.getaction()) { 
     
    case motionevent.action_down: 
      //计算当前点击的字母,宽度不用考虑,因为宽度都是一样的,计算高度即可,根据你点击或者移动的高度计算你当前所点击的是哪个字母 
      float downy=event.gety(); 
      currentindex = (int)downy/cellheight; 
      if(currentindex<0 || currentindex>lettres.length-1){ 
      }else{ 
        if(onletterchangelisten!=null){ 
          onletterchangelisten.onletterchange(lettres[currentindex]); 
        } 
      } 
      //重新绘制;相当于重新调用ondraw()方法 
      invalidate(); 
      break; 
    case motionevent.action_move: 
      float movey=event.gety(); 
      currentindex = (int)movey/cellheight; 
      if(currentindex<0 || currentindex>lettres.length-1){ 
      }else{ 
        if(onletterchangelisten!=null){ 
          onletterchangelisten.onletterchange(lettres[currentindex]); 
        } 
      } 
      //重新绘制 
      invalidate(); 
      break; 
    case motionevent.action_up: 
      currentindex=-1; 
      if(onletterchangelisten!=null){ 
        onletterchangelisten.onresert(); 
      } 
      break; 
 
    default: 
      break; 
    } 
    return true; 
  } 
 
} 

下面我为大家准备了一张计算字母xy坐标的图片:


最后是mainactivity的代码:

public class mainactivity extends activity { 
 
  private com.wxcontacts.www.view.quickindexbar quickindexbar; 
  private listview mlistview; 
  //当点击自定义view的时候屏幕中间显示一个方块,显示当前点击的字母,默认是隐藏的,当点击的时候才显示 
  private textview tvhint; 
   
  private list<haohan> hhlist=new arraylist<haohan>(); 
  context context; 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    requestwindowfeature(window.feature_no_title); 
    setcontentview(r.layout.activity_main); 
    tvhint=(textview) findviewbyid(r.id.tv_hint); 
    context=this; 
    //listview 和 自定义view 
    mlistview=(listview) findviewbyid(r.id.listview); 
    quickindexbar=(quickindexbar) findviewbyid(r.id.quickindexbar); 
     
    inithhlistdata(); 
     
    mlistview.setadapter(new mainlistviewadapter(context,hhlist)); 
     
    //给自定义view设置自定义监听,当点击到某个字母的时候弹出吐司显示这个字母; 
    //当点击字母的时候遍历集合,找到首字母为点击字母的haohan的下标,让listview跳转到响应位置 
    quickindexbar.setonletterchangelistener(new quickindexbar.onletterchangelisten() { 
       
      @override 
      public void onletterchange(string letter) { 
         
        quickindexbar.setbackgroundresource(r.drawable.bg_text); 
         
        tvhint.setvisibility(view.visible); 
        tvhint.settext(letter); 
         
        for(int x=0;x<hhlist.size();x++){ 
          if((hhlist.get(x).pinyin.charat(0)+"").equals(letter)){ 
            mlistview.setselection(x); 
            //找到第一个字母相同的直接结束,不再向下找; 
            break; 
          } 
        } 
      } 
 
      @override 
      //当手指弹起的时候此方法执行 
      public void onresert() { 
        tvhint.setvisibility(view.gone); 
        quickindexbar.setbackgroundresource(color.transparent); 
      } 
    }); 
  } 
   
  //初始化集合数据 
  private void inithhlistdata() { 
     
    haohan hh; 
    for(int x=0;x<cheeses.names.length;x++){ 
      hh=new haohan(cheeses.names[x]); 
      hhlist.add(hh); 
    } 
     
    //给集合排序 
    collections.sort(hhlist); 
     
  } 
 
} 

源码下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网