Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android手機聯系人帶字母索引的快速查找

Android手機聯系人帶字母索引的快速查找

編輯:關於Android編程

喜歡另辟蹊徑的我,在這裡廢話不多說了,直接上代碼和圖片了。
效果圖如下:

第一步:MainActivity的代碼如下:

package net.loonggg.test; 
 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.List; 
import java.util.TreeSet; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Color; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.Window; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.LinearLayout.LayoutParams; 
 
public class MainActivity extends Activity { 
  private HashMap<String, Integer> selector;// 存放含有索引字母的位置 
  private LinearLayout layoutIndex; 
  private ListView listView; 
  private TextView tv_show; 
  private ListViewAdapter adapter; 
  private String[] indexStr = { "#", "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 List<Person> persons = null; 
  private List<Person> newPersons = new ArrayList<Person>(); 
  private int height;// 字體高度 
  private boolean flag = false; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // 去標題欄 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 
    layoutIndex = (LinearLayout) this.findViewById(R.id.layout); 
    layoutIndex.setBackgroundColor(Color.parseColor("#00ffffff")); 
    listView = (ListView) findViewById(R.id.listView); 
    tv_show = (TextView) findViewById(R.id.tv); 
    tv_show.setVisibility(View.GONE); 
    setData(); 
    String[] allNames = sortIndex(persons); 
    sortList(allNames); 
 
    selector = new HashMap<String, Integer>(); 
    for (int j = 0; j < indexStr.length; j++) {// 循環字母表,找出newPersons中對應字母的位置 
      for (int i = 0; i < newPersons.size(); i++) { 
        if (newPersons.get(i).getName().equals(indexStr[j])) { 
          selector.put(indexStr[j], i); 
        } 
      } 
 
    } 
    adapter = new ListViewAdapter(this, newPersons); 
    listView.setAdapter(adapter); 
  } 
 
  /** 
   * 重新排序獲得一個新的List集合 
   * 
   * @param allNames 
   */ 
  private void sortList(String[] allNames) { 
    for (int i = 0; i < allNames.length; i++) { 
      if (allNames[i].length() != 1) { 
        for (int j = 0; j < persons.size(); j++) { 
          if (allNames[i].equals(persons.get(j).getPinYinName())) { 
            Person p = new Person(persons.get(j).getName(), persons 
                .get(j).getPinYinName()); 
            newPersons.add(p); 
          } 
        } 
      } else { 
        newPersons.add(new Person(allNames[i])); 
      } 
    } 
  } 
 
  @Override 
  public void onWindowFocusChanged(boolean hasFocus) { 
    // 在oncreate裡面執行下面的代碼沒反應,因為oncreate裡面得到的getHeight=0 
    if (!flag) {// 這裡為什麼要設置個flag進行標記,我這裡不先告訴你們,請讀者研究,因為這對你們以後的開發有好處 
      height = layoutIndex.getMeasuredHeight() / indexStr.length; 
      getIndexView(); 
      flag = true; 
    } 
  } 
 
  /** 
   * 獲取排序後的新數據 
   * 
   * @param persons 
   * @return 
   */ 
  public String[] sortIndex(List<Person> persons) { 
    TreeSet<String> set = new TreeSet<String>(); 
    // 獲取初始化數據源中的首字母,添加到set中 
    for (Person person : persons) { 
      set.add(StringHelper.getPinYinHeadChar(person.getName()).substring( 
          0, 1)); 
    } 
    // 新數組的長度為原數據加上set的大小 
    String[] names = new String[persons.size() + set.size()]; 
    int i = 0; 
    for (String string : set) { 
      names[i] = string; 
      i++; 
    } 
    String[] pinYinNames = new String[persons.size()]; 
    for (int j = 0; j < persons.size(); j++) { 
      persons.get(j).setPinYinName( 
          StringHelper 
              .getPingYin(persons.get(j).getName().toString())); 
      pinYinNames[j] = StringHelper.getPingYin(persons.get(j).getName() 
          .toString()); 
    } 
    // 將原數據拷貝到新數據中 
    System.arraycopy(pinYinNames, 0, names, set.size(), pinYinNames.length); 
    // 自動按照首字母排序 
    Arrays.sort(names, String.CASE_INSENSITIVE_ORDER); 
    return names; 
  } 
 
  /** 
   * 繪制索引列表 
   */ 
  public void getIndexView() { 
    LinearLayout.LayoutParams params = new LayoutParams( 
        LayoutParams.WRAP_CONTENT, height); 
    for (int i = 0; i < indexStr.length; i++) { 
      final TextView tv = new TextView(this); 
      tv.setLayoutParams(params); 
      tv.setText(indexStr[i]); 
      tv.setPadding(10, 0, 10, 0); 
      layoutIndex.addView(tv); 
      layoutIndex.setOnTouchListener(new OnTouchListener() { 
 
        @Override 
        public boolean onTouch(View v, MotionEvent event) 
 
        { 
          float y = event.getY(); 
          int index = (int) (y / height); 
          if (index > -1 && index < indexStr.length) {// 防止越界 
            String key = indexStr[index]; 
            if (selector.containsKey(key)) { 
              int pos = selector.get(key); 
              if (listView.getHeaderViewsCount() > 0) {// 防止ListView有標題欄,本例中沒有。 
                listView.setSelectionFromTop( 
                    pos + listView.getHeaderViewsCount(), 0); 
              } else { 
                listView.setSelectionFromTop(pos, 0);// 滑動到第一項 
              } 
              tv_show.setVisibility(View.VISIBLE); 
              tv_show.setText(indexStr[index]); 
            } 
          } 
          switch (event.getAction()) { 
          case MotionEvent.ACTION_DOWN: 
            layoutIndex.setBackgroundColor(Color 
                .parseColor("#606060")); 
            break; 
 
          case MotionEvent.ACTION_MOVE: 
 
            break; 
          case MotionEvent.ACTION_UP: 
            layoutIndex.setBackgroundColor(Color 
                .parseColor("#00ffffff")); 
            tv_show.setVisibility(View.GONE); 
            break; 
          } 
          return true; 
        } 
      }); 
    } 
  } 
 
  /** 
   * 設置模擬數據 
   */ 
  private void setData() { 
    persons = new ArrayList<Person>(); 
    Person p1 = new Person("耿琦"); 
    persons.add(p1); 
    Person p2 = new Person("王寶強"); 
    persons.add(p2); 
    Person p3 = new Person("柳巖"); 
    persons.add(p3); 
    Person p4 = new Person("文章"); 
    persons.add(p4); 
    Person p5 = new Person("馬伊琍"); 
    persons.add(p5); 
    Person p6 = new Person("李晨"); 
    persons.add(p6); 
    Person p7 = new Person("張馨予"); 
    persons.add(p7); 
    Person p8 = new Person("韓紅"); 
    persons.add(p8); 
    Person p9 = new Person("韓寒"); 
    persons.add(p9); 
    Person p10 = new Person("丹丹"); 
    persons.add(p10); 
    Person p11 = new Person("丹鳳眼"); 
    persons.add(p11); 
    Person p12 = new Person("哈哈"); 
    persons.add(p12); 
    Person p13 = new Person("萌萌"); 
    persons.add(p13); 
    Person p14 = new Person("蒙混"); 
    persons.add(p14); 
    Person p15 = new Person("煙花"); 
    persons.add(p15); 
    Person p16 = new Person("眼黑"); 
    persons.add(p16); 
    Person p17 = new Person("許三多"); 
    persons.add(p17); 
    Person p18 = new Person("程咬金"); 
    persons.add(p18); 
    Person p19 = new Person("程哈哈"); 
    persons.add(p19); 
    Person p20 = new Person("愛死你"); 
    persons.add(p20); 
    Person p21 = new Person("阿萊"); 
    persons.add(p21); 
 
  } 
 
}

 此Activity的布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="match_parent" 
  android:background="#ffffff" 
  android:orientation="vertical" > 
 
  <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:text="列表顯示" 
    android:textColor="#000000" 
    android:textSize="16sp" /> 
 
  <FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" > 
 
    <ListView 
      android:id="@+id/listView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:cacheColorHint="#00000000" 
      android:fadingEdge="none" 
      android:scrollbars="none" > 
    </ListView> 
 
    <TextView 
      android:id="@+id/tv" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:layout_gravity="center" 
      android:background="#f0606060" 
      android:gravity="center" 
      android:text="A" 
      android:textColor="#ffffff" 
      android:textSize="30sp" /> 
 
    <LinearLayout 
      android:id="@+id/layout" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_gravity="right" 
      android:background="#d7d7d7" 
      android:gravity="center" 
      android:orientation="vertical" > 
    </LinearLayout> 
  </FrameLayout> 
 
</LinearLayout> 

第二步:自定義了一個Adapter,代碼如下:

package net.loonggg.test; 
 
import java.util.List; 
 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 
 
public class ListViewAdapter extends BaseAdapter { 
  private Context context; 
  private List<Person> list; 
  private ViewHolder viewHolder; 
 
  public ListViewAdapter(Context context, List<Person> list) { 
    this.context = context; 
    this.list = list; 
  } 
 
  @Override 
  public int getCount() { 
    return list.size(); 
  } 
 
  @Override 
  public Object getItem(int position) { 
    return list.get(position); 
  } 
 
  @Override 
  public long getItemId(int position) { 
    return position; 
  } 
 
  @Override 
  public boolean isEnabled(int position) { 
    // TODO Auto-generated method stub 
    if (list.get(position).getName().length() == 1)// 如果是字母索引 
      return false;// 表示不能點擊 
    return super.isEnabled(position); 
  } 
 
  @Override 
  public View getView(int position, View convertView, ViewGroup parent) { 
    String item = list.get(position).getName(); 
    viewHolder = new ViewHolder(); 
    if (item.length() == 1) { 
      convertView = LayoutInflater.from(context).inflate(R.layout.index, 
          null); 
      viewHolder.indexTv = (TextView) convertView 
          .findViewById(R.id.indexTv); 
    } else { 
      convertView = LayoutInflater.from(context).inflate(R.layout.item, 
          null); 
      viewHolder.itemTv = (TextView) convertView 
          .findViewById(R.id.itemTv); 
    } 
    if (item.length() == 1) { 
      viewHolder.indexTv.setText(list.get(position).getName()); 
    } else { 
      viewHolder.itemTv.setText(list.get(position).getName()); 
    } 
    return convertView; 
  } 
 
  private class ViewHolder { 
    private TextView indexTv; 
    private TextView itemTv; 
  } 
 
} 

第三步:用到的ListView中的子布局文件如下:
1、index.xml布局文件代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:background="#9c9c9c" 
  android:orientation="vertical" 
  android:paddingBottom="5dp" 
  android:paddingLeft="10dp" 
  android:paddingTop="5dp" > 
 
  <TextView 
    android:id="@+id/indexTv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#0f0f0f" /> 
 
</LinearLayout> 

2、item.xml布局文件代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:background="#ffffff" 
  android:orientation="vertical" 
  android:paddingBottom="5dp" 
  android:paddingLeft="20dp" 
  android:paddingTop="5dp" > 
 
  <TextView 
    android:id="@+id/itemTv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textSize="20sp" /> 
 
</LinearLayout> 

第四步:用到的實體類及工具類如下:
1、Person.java代碼如下:

package net.loonggg.test; 
 
public class Person { 
  private String name; 
  private String pinYinName; 
 
  public Person(String name) { 
    super(); 
    this.name = name; 
  } 
 
  public Person(String name, String pinYinName) { 
    super(); 
    this.name = name; 
    this.pinYinName = pinYinName; 
  } 
 
  public String getName() { 
    return name; 
  } 
 
  public void setName(String name) { 
    this.name = name; 
  } 
 
  public String getPinYinName() { 
    return pinYinName; 
  } 
 
  public void setPinYinName(String pinYinName) { 
    this.pinYinName = pinYinName; 
  } 
 
} 

2、工具類代碼如下:

package net.loonggg.test; 
 
import net.sourceforge.pinyin4j.PinyinHelper; 
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; 
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; 
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; 
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; 
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; 
 
public class StringHelper { 
  /** 
   * 得到 全拼 
   * 
   * @param src 
   * @return 
   */ 
  public static String getPingYin(String src) { 
    char[] t1 = null; 
    t1 = src.toCharArray(); 
    String[] t2 = new String[t1.length]; 
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat(); 
    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE); 
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
    t3.setVCharType(HanyuPinyinVCharType.WITH_V); 
    String t4 = ""; 
    int t0 = t1.length; 
    try { 
      for (int i = 0; i < t0; i++) { 
        // 判斷是否為漢字字符 
        if (java.lang.Character.toString(t1[i]).matches( 
            "[\\u4E00-\\u9FA5]+")) { 
          t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3); 
          t4 += t2[0]; 
        } else { 
          t4 += java.lang.Character.toString(t1[i]); 
        } 
      } 
      return t4; 
    } catch (BadHanyuPinyinOutputFormatCombination e1) { 
      e1.printStackTrace(); 
    } 
    return t4; 
  } 
 
  /** 
   * 得到首字母 
   * 
   * @param str 
   * @return 
   */ 
  public static String getHeadChar(String str) { 
 
    String convert = ""; 
    char word = str.charAt(0); 
    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word); 
    if (pinyinArray != null) { 
      convert += pinyinArray[0].charAt(0); 
    } else { 
      convert += word; 
    } 
    return convert.toUpperCase(); 
  } 
   
  /** 
   * 得到中文首字母縮寫 
   * 
   * @param str 
   * @return 
   */ 
  public static String getPinYinHeadChar(String str) { 
 
    String convert = ""; 
    for (int j = 0; j < str.length(); j++) { 
      char word = str.charAt(j); 
      String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word); 
      if (pinyinArray != null) { 
        convert += pinyinArray[0].charAt(0); 
      } else { 
        convert += word; 
      } 
    } 
    return convert.toUpperCase(); 
  } 
} 

到這裡就完事,非常簡單吧!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved