2013年8月7日星期三

Android Study Notes (three) with a side of the index to quicklyfind (with alphabetical index lookup contacts with almost)

 
  

like another way for me, here ado, directly on the code and pictures.

  

renderings are as follows:

  

  

Step: MainActivity code is as follows:

  
   
    
      [java]       view plain       copy     
   
   
        
  1. package net.loonggg.test;
  2.     
  3.     
  4. import java.util.ArrayList;
  5.     
  6. import java.util.Arrays;
  7.     
  8. import java.util.HashMap;
  9.     
  10. import java.util.List;
  11.     
  12. import java.util.TreeSet;
  13.     
  14.     
  15. import android.os.Bundle;
  16.     
  17. import android.app.Activity;
  18.     
  19. import android.graphics.Color;
  20.     
  21. import android.view.MotionEvent;
  22.     
  23. import android.view.View;
  24.     
  25. import android.view.View.OnTouchListener;
  26.     
  27. import android.view.Window;
  28.     
  29. import android.widget.LinearLayout;
  30.     
  31. import android.widget.ListView;
  32.     
  33. import android.widget.TextView;
  34.     
  35. import android.widget.LinearLayout.LayoutParams; < / li>     
  36.     
  37. public class ; MainActivity extends Activity {
  38.     
  39. private HashMap selector; / / store index letter containing the location
  40.     
  41. private LinearLayout layoutIndex; < / span>
  42.     
  43. private ListView listView;
  44.     
  45. private TextView tv_show; < / span>
  46.     
  47. private ListViewAdapter adapter;
  48.     
  49. private String [] indexStr = { "#" , "A" , "B" , "C" < span>, "D" , "E" , "F" , "G" < / span> , "H" ,
  50.     
  51. "I" , "J" , "K" , "L" , "M" , "N" , "O" , "P" , "Q" , "R" , " S " , " T " , "U" ,
  52.     
  53. "V" , "W" , "X" < / span> , "Y" , "Z" ; };
  54.     
  55. private List persons = null ;
  56.     
  57. private List newPersons = new ArrayList ();
  58.     
  59. private int < span> height; / / font height
  60.     
  61. private boolean flag = false ;
  62.     
  63.     
  64. @ Override
  65.     
  66. protected void < span> onCreate (Bundle savedInstanceState) {
  67.     
  68. super . onCreate (savedInstanceState); ;
  69.     
  70. / / go to the title bar
  71.     
  72. requestWindowFeature (Window.FEATURE_NO_TITLE);
  73.     
  74. setContentView (R.layout.activity_main);
  75.     
  76. layoutIndex = (LinearLayout) this . findViewById (R.id.layout);
  77.     
  78. layoutIndex.setBackgroundColor (Color.parseColor ( "# 00ffffff" ));
  79.     
  80. listView = (ListView) findViewById (R.id.listView);
  81.     
  82. tv_show = (TextView) findViewById (R.id.tv); < / li>     
  83. tv_show.setVisibility (View.GONE);
  84.     
  85. setData ();
  86.     
  87. String [] allNames = sortIndex (persons);
  88.     
  89. sortList (allNames);
  90.     
  91.     
  92. selector = new HashMap ();
  93.     
  94. for ( int j = 0 ; j < indexStr.length; j + +) { / / loop alphabet letters identify newPersons corresponding position
  95.     
  96. for (< / span> int i = 0 ; i ;
  97.     
  98. if (newPersons.get (i). getName (). equals (indexStr [j])) {
  99.     
  100. selector.put ( indexStr [j], i);
  101.     
  102. }
  103.     
  104. }
  105.     
  106.     
  107. }
  108.     
  109. adapter = new ListViewAdapter ( this , newPersons);
  110.     
  111. listView.setAdapter (adapter);
  112.     
  113. }
  114.     
  115.     
  116. / **
  117.     
  118. * reordered get a new List collection
  119.     
  120. *
  121.     
  122. * @ param allNames
  123.     
  124. * /
  125.     
  126. private void < span> sortList (String [] allNames) {
  127.     
  128. for ( int i = 0 ; i < allNames.length; i + +) {
  129.     
  130. if (allNames [i]. length ()! = 1 ) { < / li>     
  131. for ( int j = 0 ; j
  132.     
  133. if (allNames [i]. equals (persons.get (j). getPinYinName ())) { < / li>     
  134. ; Person p = new Person (persons.get (j). getName (), persons ;
  135.     
  136. . get (j). getPinYinName ());
  137.     
  138. ; newPersons.add (p);
  139.     
  140. } ;
  141.     
  142. }
  143.     
  144. } else ; {
  145.     
  146. newPersons.add ( new Person (allNames [i]));
  147.     
  148. }
  149.     
  150. }
  151.     
  152. }
  153.     
  154.     
  155. @ Override
  156.     
  157. public void onWindowFocusChanged ( boolean hasFocus) {
  158.     
  159. / / in oncreate execute the following code inside did not respond, because oncreate get inside getHeight = 0
  160.     
  161. if (! flag) ; { / / why we want to set a flag to mark, I am here to tell you is not first, the reader study, because it is good for your future development
  162.     
  163. height = layoutIndex.getMeasuredHeight () / indexStr.length; ;
  164.     
  165. getIndexView ();
  166.     
  167. flag = true ;
  168.     
  169. }
  170.     
  171. }
  172.     
  173.     
  174. / **
  175.     
  176. * obtain new data after sorting
  177.     
  178. *
  179.     
  180. * @ param persons
  181.     
  182. * @ return
  183.     
  184. * /
  185.     
  186. public String [] sortIndex (List persons) {
  187.     
  188. TreeSet set = new TreeSet ();
  189.     
  190. / / initialize the data source for the first letter, added to the set
  191.     
  192. for (Person person : persons) {
  193.     
  194. set.add (StringHelper.getPinYinHeadChar (person.getName ()). substring ( ;
  195.     
  196. ; 0 , 1 ));
  197.     
  198. }
  199.     
  200. / / new length of the array is set to the original data plus Size
  201.     
  202. String [] names = new String [persons.size () + set.size ()];
  203.     
  204. int i = 0 ;
  205.     
  206. for (String string: set) {
  207.     
  208. names [i] = string;
  209.     
  210. i + +;
  211.     
  212. }
  213.     
  214. String [] pinYinNames = new String [persons.size ()];
  215.     
  216. for ( int j = 0 ; j < persons.size (); j + +) {
  217.     
  218. persons.get (j). setPinYinName (
  219.     
  220. ; StringHelper
  221.     
  222. . getPingYin (persons.get (j). getName (). toString ()));
  223.     
  224. pinYinNames [j] = StringHelper.getPingYin (persons. get (j). getName ()
  225.     
  226. . toString () );
  227.     
  228. }
  229.     
  230. / / original data is copied to the new data <​​span> ;
  231.     
  232. System.arraycopy (pinYinNames, 0 , names, set.size (), pinYinNames.length);
  233.     
  234. / / automatically in alphabetical order
  235.     
  236. Arrays.sort (names, String.CASE_INSENSITIVE_ORDER);
  237.     
  238. return names;
  239.     
  240. }
  241.     
  242.     
  243. / **
  244.     
  245. * rendering index list
  246.     
  247. * /
  248.     
  249. public void < span> getIndexView () {
  250.     
  251. LinearLayout.LayoutParams params = new < span> LayoutParams (
  252.     
  253. LayoutParams.WRAP_CONTENT, height); ;
  254.     
  255. for ( int i = 0 ; i < indexStr.length; i + +) {
  256.     
  257. final TextView tv = new TextView ( this ) ;
  258.     
  259. tv.setLayoutParams (params);
  260.     
  261. tv.setText (indexStr [i]);
  262.     
  263. tv.setPadding ( 10 , 0 , 10 , 0 );
  264.     
  265. layoutIndex.addView (tv);
  266.     
  267. layoutIndex.setOnTouchListener ( new OnTouchListener () {
  268.     
  269.     
  270. @ Override
  271.     
  272. public boolean onTouch (View v, MotionEvent event)
  273.     
  274.     
  275. {
  276.     
  277. ; float y = event.getY ();
  278.     
  279. int index = ( int ) (y / height );
  280.     
  281. ; if (index> - 1 & ; & index / / prevent transboundary
  282.     
  283. String key = indexStr [index];
  284.     
  285. ; if (selector.containsKey (key)) {
  286.     
  287. int pos = selector.get (key);
  288.     
  289. ; if (listView.getHeaderViewsCount ()> 0 ) { / / prevent ListView has a title bar, in this case no.
  290.     
  291. listView.setSelectionFromTop (
  292.     
  293. ; pos + listView.getHeaderViewsCount ( ), 0 );
  294.     
  295. } else {
  296.     
  297. ; listView.setSelectionFromTop (pos, 0 ) ; / / slide to the first item
  298.     
  299. }
  300.     
  301. ; tv_show.setVisibility (View.VISIBLE);
  302.     
  303. tv_show.setText (indexStr [index]);
  304.     
  305. ;}
  306.     
  307. } ;
  308.     
  309. ; switch (event.getAction ()) {
  310.     
  311. case MotionEvent.ACTION_DOWN:
  312.     
  313. ; layoutIndex.setBackgroundColor (Color
  314.     
  315. . parseColor ( "# 606060" ));
  316.     
  317. ; break ;
  318.     
  319.     
  320. ; case MotionEvent.ACTION_MOVE:
  321.     
  322.     
  323. ; break ;
  324.     
  325. case MotionEvent.ACTION_UP:
  326.                                                  
  327. }
  328.                                                                                                                                                                                                                                                          
  329. }
  330.              
  
         
    
                      
   
                                 
  •                                        
  •                                                                      
  •                                                                                                              
  •         
        
           
         
          
            [java]                 
         
         
                 
      1.          
      2.                                                            
      3.                    
      4. }
      5.                              
      6.                    
      7. }
      8.                              
      9.                                        
      10.                                                                                                    
      11. }
      12.          
      13. }
      14.                              
      15.         
        
                  
          
                            
         
                                                           
    1.                              
    2.         
          
                 
            
                              
           
                                                             
      1.                                            
            
                  
             
              
                [java]                 
             
             
                     
          1.                    
          2.                    
          3. }
          4.                              
          5. }
          6.                    
          7. }
          8.                    
          9. }
          10.                    
          11. }
          12.                    
          13. }
          14.              
            
               
             
              
                [java]                 
             
             
                     
          1.                                                                                                                                                                                                                  
          2.                                        
          3.                                        
          4. }
          5.          
          6. }
          7.                                                                                          
          8. }
          9.                        
            
               
              

          没有评论:

          发表评论