SectionIndexer 的施用(联系人分类索引)

SectionIndexer 的使用(联系人分类索引)

 

  1. // 获取标题栏索引
  2. int position = sectionIndexter.getPositionForSection(l[idx]);  
  3.             if (position == -1) {  
  4.                 return true;  
  5.             }  
  6.     // 设置调整到指定区域
  7.             list.setSelection(position); 

 

1. Main.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6. <ListView android:id="@+id/myListView"  
  7.     android:layout_width="fill_parent"  
  8.     android:layout_height="fill_parent"  
  9. />  
  10. <android.test.SideBar  
  11.     android:id = "@+id/sideBar"  
  12.     android:layout_height="fill_parent"  
  13.     android:layout_width="22px"  
  14.     android:layout_alignParentRight="true"  
  15. />  
  16. </RelativeLayout>  

 

 

 

2. listview_row.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content">  
  7. <LinearLayout  
  8.     android:id="@+id/section"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11. />  
  12. <TextView android:id="@+id/textView"  
  13.     android:layout_width="wrap_content"  
  14.     android:layout_height="80sp"  
  15.     android:textSize="45sp"  
  16. />  
  17. </LinearLayout>  

 

 

 

3. MyAdapter.java

 

  1. package android.test;  
  2. import java.util.ArrayList;  
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.Color;  
  6. import android.view.Gravity;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.BaseAdapter;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.SectionIndexer;  
  13. import android.widget.TextView;  
  14. public class MyAdapter extends BaseAdapter implements SectionIndexer {  
  15.     private ArrayList<String> stringArray;  
  16.     private Context context;  
  17.     public MyAdapter(Context _context, ArrayList<String> arr) {  
  18.         stringArray = arr;  
  19.         context = _context;  
  20.     }  
  21.     public int getCount() {  
  22.         return stringArray.size();  
  23.     }  
  24.     public Object getItem(int arg0) {  
  25.         return stringArray.get(arg0);  
  26.     }  
  27.     public long getItemId(int arg0) {  
  28.         return 0;  
  29.     }  
  30.     public View getView(int position, View v, ViewGroup parent) {  
  31.         LayoutInflater inflate = ((Activity) context).getLayoutInflater();  
  32.         View view = (View) inflate.inflate(R.layout.listview_row, null);  
  33.         LinearLayout header = (LinearLayout) view.findViewById(R.id.section);  
  34.         String label = stringArray.get(position);  
  35.         char firstChar = label.toUpperCase().charAt(0);  
  36.         if (position == 0) {  
  37.             setSection(header, label);  
  38.         } else {  
  39.             String preLabel = stringArray.get(position - 1);  
  40.             char preFirstChar = preLabel.toUpperCase().charAt(0);  
  41.             if (firstChar != preFirstChar) {  
  42.                 setSection(header, label);  
  43.             } else {  
  44.                 header.setVisibility(View.GONE);  
  45.             }  
  46.         }  
  47.         TextView textView = (TextView) view.findViewById(R.id.textView);  
  48.         textView.setText(label);  
  49.         return view;  
  50.     }  
  51.     private void setSection(LinearLayout header, String label) {  
  52.         TextView text = new TextView(context);  
  53.         header.setBackgroundColor(0xffaabbcc);  
  54.         text.setTextColor(Color.WHITE);  
  55.         text.setText(label.substring(01).toUpperCase());  
  56.         text.setTextSize(20);  
  57.         text.setPadding(5000);  
  58.         text.setGravity(Gravity.CENTER_VERTICAL);  
  59.         header.addView(text);  
  60.     }  
  61.     public int getPositionForSection(int section) {  
  62.         if (section == 35) {  
  63.             return 0;  
  64.         }  
  65.         for (int i = 0; i < stringArray.size(); i++) {  
  66.             String l = stringArray.get(i);  
  67.             char firstChar = l.toUpperCase().charAt(0);  
  68.             if (firstChar == section) {  
  69.                 return i;  
  70.             }  
  71.         }  
  72.         return -1;  
  73.     }  
  74.     public int getSectionForPosition(int arg0) {  
  75.         return 0;  
  76.     }  
  77.     public Object[] getSections() {  
  78.         return null;  
  79.     }  
  80. }  

 

 

 

4.  SideBar.java

 

  1. package android.test;  
  2. import android.content.Context;  
  3. import android.graphics.Canvas;  
  4. import android.graphics.Paint;  
  5. import android.util.AttributeSet;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.widget.ListView;  
  9. import android.widget.SectionIndexer;  
  10. public class SideBar extends View {  
  11.     private char[] l;  
  12.     private SectionIndexer sectionIndexter = null;  
  13.     private ListView list;  
  14.     private final int m_nItemHeight = 29;  
  15.     public SideBar(Context context) {  
  16.         super(context);  
  17.         init();  
  18.     }  
  19.     public SideBar(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.         init();  
  22.     }  
  23.     private void init() {  
  24.         l = new char[] { 'A''B''C''D''E''F''G''H''I''J''K''L''M''N''O''P''Q''R''S',  
  25.                 'T''U''V''W''X''Y''Z' };  
  26.         setBackgroundColor(0x44FFFFFF);  
  27.     }  
  28.     public SideBar(Context context, AttributeSet attrs, int defStyle) {  
  29.         super(context, attrs, defStyle);  
  30.         init();  
  31.     }  
  32.     public void setListView(ListView _list) {  
  33.         list = _list;  
  34.         sectionIndexter = (SectionIndexer) _list.getAdapter();  
  35.     }  
  36.     public boolean onTouchEvent(MotionEvent event) {  
  37.         super.onTouchEvent(event);  
  38.         int i = (int) event.getY();  
  39.         int idx = i / m_nItemHeight;  
  40.         if (idx >= l.length) {  
  41.             idx = l.length - 1;  
  42.         } else if (idx < 0) {  
  43.             idx = 0;  
  44.         }  
  45.         if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {  
  46.             if (sectionIndexter == null) {  
  47.                 sectionIndexter = (SectionIndexer) list.getAdapter();  
  48.             }  
  49.             int position = sectionIndexter.getPositionForSection(l[idx]);  
  50.             if (position == -1) {  
  51.                 return true;  
  52.             }  
  53.             list.setSelection(position);  
  54.         }  
  55.         return true;  
  56.     }  
  57.     protected void onDraw(Canvas canvas) {  
  58.         Paint paint = new Paint();  
  59.         paint.setColor(0xFFA6A9AA);  
  60.         paint.setTextSize(20);  
  61.         paint.setTextAlign(Paint.Align.CENTER);  
  62.         float widthCenter = getMeasuredWidth() / 2;  
  63.         for (int i = 0; i < l.length; i++) {  
  64.             canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint);  
  65.         }  
  66.         super.onDraw(canvas);  
  67.     }  
  68. }  

 

 

 

5. Main.java

 

  1. package android.test;  
  2. import java.util.ArrayList;  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.ListView;  
  6. public class Main extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.         ListView list = (ListView) findViewById(R.id.myListView);  
  13.         ArrayList<String> stringList = InitListViewData();  
  14.         MyAdapter adapter = new MyAdapter(this, stringList);  
  15.         list.setAdapter(adapter);  
  16.         SideBar indexBar = (SideBar) findViewById(R.id.sideBar);  
  17.         indexBar.setListView(list);  
  18.     }  
  19.     private ArrayList<String> InitListViewData() {  
  20.         ArrayList<String> stringList = new ArrayList<String>();  
  21.         stringList.add("aback");  
  22.         stringList.add("abash");  
  23.         stringList.add("abbey");  
  24.         stringList.add("abhor");  
  25.         stringList.add("abide");  
  26.         stringList.add("abuse");  
  27.         stringList.add("candidate");  
  28.         stringList.add("capture");  
  29.         stringList.add("careful");  
  30.         stringList.add("catch");  
  31.         stringList.add("cause");  
  32.         stringList.add("celebrate");  
  33.         stringList.add("forever");  
  34.         stringList.add("fable");  
  35.         stringList.add("fidelity");  
  36.         stringList.add("fox");  
  37.         stringList.add("funny");  
  38.         stringList.add("fail");  
  39.         stringList.add("jail");  
  40.         stringList.add("jade");  
  41.         stringList.add("jailor");  
  42.         stringList.add("january");  
  43.         stringList.add("jasmine");  
  44.         stringList.add("jazz");  
  45.         stringList.add("zero");  
  46.         stringList.add("zoo");  
  47.         stringList.add("zeus");  
  48.         stringList.add("zebra");  
  49.         stringList.add("zest");  
  50.         stringList.add("zing");  
  51.         return stringList;  
  52.     }  
  53. }