Android index contact list and android index contact list
There are also many examples of the online Android contact list, which is similar to the contact list. Because the project uses the contact list index function (the product removes the letter item), it is still easy to implement, here I will share my implementation with you, so that you may not forget it later. First, let's take a look at the effect (the Demo is available at the end ):
The effect is so simple.
Let's talk about the idea: First add a pinyin field to the contact object. After obtaining the original contact data, convert the name of each contact to pinyin and set the value for the pinyin field. Then, obtain the pinyin characters of the letters in the contact and save them as an array (this is the item of the letter). Then, combine them with the contact pinyin into a new Array Using Arrays. the sort sorting function takes the contact list in alphabetical order, and stores the positions where the letters appear in the contact list, set the position of the current letter in the contact list for the corresponding letter, and move the listview to that position by sliding to a letter.
I. First, let's take a look at extracting letters from contacts and sorting them.
/*** Sort the data and add the A-Z order ** @ param carTypes * @ return */public String [] sortIndex (List <ConstastBean> constastBeans) {TreeSet <String> set = new TreeSet <String> (); for (ConstastBean constastBean: constastBeans) {char ch = constastBean. getPinyin (). charAt (0); set. add (String. valueOf (ch ). toUpperCase (Locale. getDefault (); // obtain the first letter} String [] names = new String [constastBeans. size () + set. size ()]; // new array, used to save the first letter + contact pinyin int I = 0; for (String string: set) {// Add the letters in the set to the new array (Front) names [I] = string; I ++ ;}< span style = "white-space: pre "> </span> // extract the contact pinyin to an array String [] pyheader = new String [constastBeans. size ()]; for (int j = 0; j <constastBeans. size (); j ++) {pyheader [j] = constastBeans. get (j ). getPinyin ();} System. arraycopy (pyheader, 0, names, set. size (), pyheader. length); // <span style = "font-family: Arial, Helvetica, sans-serif;"> Add the contact pinyin to the end, the result is that the contact Pinyin and the first letter appear in an array (unordered) </span> // Arrays are automatically sorted by the first letter. sort (names, String. CASE_INSENSITIVE_ORDER); // sort by letter in strict alphabetical order. case-insensitive characters are ignored. return names is returned for an array sorted by pinyin ;}The returned results are sorted in alphabetical order (for example, [A, Anne, G, Galen... Z]).
2. Sort the contact objects according to the returned order.
/*** Sort the data by name because the number is first by default. to sort the number to the end, convert ** @ param arry * @ return */public ArrayList <ConstastBean> getAllLists (String [] arry) {ArrayList <ConstastBean> lists = new ArrayList <ConstastBean> (); // Save the sorted data ArrayList <ConstastBean> lists2 = new ArrayList <ConstastBean> (); // save ArrayList <ConstastBean> lists3 = new ArrayList <ConstastBean> (); // Save the letter data // sort the data for (int I = 0; I <arry. length; I ++) {for (int j = 0; j <sourceData. size (); j ++) {if (arry [I]. equals (sourceData. get (j ). getPinyin () {lists. add (sourceData. get (j); break;} // else // the item with a single letter needs to be displayed. No comment is required here, set a separate layout for this item in the adapter // {// ConstastBean contactBean = new ConstastBean (); // contactBean. setPinyin (arry [I]); // contactBean. setNickName (arry [I]); // lists. add (contactBean); // break; // }}// extract the numeric data and the letter data int index = getLetter (lists ); // obtain the position starting with a letter for (int I = 0; I <lists. size (); I ++) {if (I <index) {lists2.add (lists. get (I);} else {lists3.add (lists. get (I) ;}} lists. clear (); lists. addAll (lists3); lists. addAll (lists2); return lists ;}
Because the numbers are arranged in order before the letters, but the numbers must be behind them, first query the position where the first letter appears, and then extract the two sets from that position, add the number set to the end of the letter set.
Iii. Initialization is complete, so we need to traverse and obtain the location corresponding to each letter.
<Span style = "white-space: pre"> </span> selector = new HashMap <String, Integer> (); // traverses sorted data, obtain the position of each letter for (int I = 0; I <indexStr. length; I ++) {for (int j = 0; j <datas. size (); j ++) {if (datas. get (j ). getPinyin (). toLowerCase (Locale. getDefault ()). startsWith (indexStr [I <span style = "white-space: pre"> </span>]. toLowerCase (Locale. getDefault () {selector. put (indexStr [I], j); break;} String pinyin = datas. get (j ). getPinyin (); if (indexStr [I]. equals ("#") & isNumeric (pinyin. substring (0, 1) {selector. put (indexStr [I], j); return ;}}}
The returned result is similar to this: {D = 2, # = 23, E = 4, G = 5, A = 0, L = 11, M = 12, N = 14, H = 6, J = 8, K = 10, T = 19, V = 20, S = 16, R = 15, Z = 21}
/*** Draw index entries */public void drawIndexView () {LinearLayout. layoutParams params = new LayoutParams (LayoutParams. MATCH_PARENT, height); for (int I = 0; I <indexStr. length; I ++) {TextView TV = new TextView (this); TV. setLayoutParams (params); TV. setText (indexStr [I]); TV. setGravity (Gravity. CENTER); TV. setTextColor (this. getResources (). getColor (R. color. indexs_color); TV. setTextSize (13); layoutIndex. addView (TV); lay OutIndex. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stubfloat y = event. getY (); int index = (int) y/height; // obtain the index String key = ""; if (index <indexStr. length & index>-1) {key = indexStr [index]; if (selector. containsKey (key) {int position = selector. get (key); if (listView. getHeaderViewsCount ()> 0 ){/ /Remove the added header from the listView. setSelectionFromTop (position + listView. getHeaderViewsCount (), 0);} else {listView. setSelectionFromTop (position, 0); // slide to the first entry} if (key. equals ("events") {listView. setSelectionFromTop (0, 0); // slide to the first entry} if (! Key. equals ("") {showTv. setText (key); showTv. setVisibility (View. VISIBLE);} switch (event. getAction () {case MotionEvent. ACTION_UP: case MotionEvent. ACTION_CANCEL: case MotionEvent. ACTION_OUTSIDE: showTv. setVisibility (View. GONE); break; case MotionEvent. ACTION_DOWN: // layoutIndex. setBackground (); break;} return true ;}});}}This completes the implementation of the contact list with indexes: Download the Demo source code
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.