国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2020-01-02 07:02:36
字體:
來源:轉載
供稿:網友

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

第一步: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("馬伊

主站蜘蛛池模板:
玉溪市|
绥江县|
兴国县|
玉环县|
成武县|
会宁县|
拉萨市|
南城县|
溧水县|
安多县|
固原市|
巩义市|
屏山县|
师宗县|
安图县|
侯马市|
柳林县|
临安市|
古浪县|
淮北市|
柳河县|
辉南县|
杂多县|
云浮市|
沁水县|
金塔县|
蓝田县|
平安县|
西乌珠穆沁旗|
龙里县|
临清市|
十堰市|
崇文区|
屏南县|
湟中县|
河北区|
灵山县|
革吉县|
沂南县|
东港市|
阳原县|