// 获取标题栏索引 int position = sectionIndexter.getPositionForSection(l[idx]); if (position == -1) { return true; } // 设置调整到指定区域 list.setSelection(position);
1. Main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/myListView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <android.test.SideBar android:id = "@+id/sideBar" android:layout_height="fill_parent" android:layout_width="22px" android:layout_alignParentRight="true" /> </RelativeLayout>
2. listview_row.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/section" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="80sp" android:textSize="45sp" /> </LinearLayout>
3. MyAdapter.Java
package android.test; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.SectionIndexer; import android.widget.TextView; public class MyAdapter extends BaseAdapter implements SectionIndexer { private ArrayList<String> stringArray; private Context context; public MyAdapter(Context _context, ArrayList<String> arr) { stringArray = arr; context = _context; } public int getCount() { return stringArray.size(); } public Object getItem(int arg0) { return stringArray.get(arg0); } public long getItemId(int arg0) { return 0; } public View getView(int position, View v, ViewGroup parent) { LayoutInflater inflate = ((Activity) context).getLayoutInflater(); View view = (View) inflate.inflate(R.layout.listview_row, null); LinearLayout header = (LinearLayout) view.findViewById(R.id.section); String label = stringArray.get(position); char firstChar = label.toUpperCase().charAt(0); if (position == 0) { setSection(header, label); } else { String preLabel = stringArray.get(position - 1); char preFirstChar = preLabel.toUpperCase().charAt(0); if (firstChar != preFirstChar) { setSection(header, label); } else { header.setVisibility(View.GONE); } } TextView textView = (TextView) view.findViewById(R.id.textView); textView.setText(label); return view; } private void setSection(LinearLayout header, String label) { TextView text = new TextView(context); header.setBackgroundColor(0xffaabbcc); text.setTextColor(Color.WHITE); text.setText(label.substring(0, 1).toUpperCase()); text.setTextSize(20); text.setPadding(5, 0, 0, 0); text.setGravity(Gravity.CENTER_VERTICAL); header.addView(text); } public int getPositionForSection(int section) { if (section == 35) { return 0; } for (int i = 0; i < stringArray.size(); i++) { String l = stringArray.get(i); char firstChar = l.toUpperCase().charAt(0); if (firstChar == section) { return i; } } return -1; } public int getSectionForPosition(int arg0) { return 0; } public Object[] getSections() { return null; } }
4. SideBar.java
package android.test; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.ListView; import android.widget.SectionIndexer; public class SideBar extends View { private char[] l; private SectionIndexer sectionIndexter = null; private ListView list; private final int m_nItemHeight = 60; public SideBar(Context context) { super(context); init(); } public SideBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { l = new char[] { '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' }; setBackgroundColor(0x44FFFFFF); } public SideBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public void setListView(ListView _list) { list = _list; sectionIndexter = (SectionIndexer) _list.getAdapter(); } public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); int i = (int) event.getY(); int idx = i / m_nItemHeight; if (idx >= l.length) { idx = l.length - 1; } else if (idx < 0) { idx = 0; } if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { if (sectionIndexter == null) { sectionIndexter = (SectionIndexer) list.getAdapter(); } int position = sectionIndexter.getPositionForSection(l[idx]); if (position == -1) { return true; } list.setSelection(position); } return true; } protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(0xFFA6A9AA); paint.setTextSize(getMeasuredHeight()/26); paint.setTextAlign(Paint.Align.CENTER); float widthCenter = getMeasuredWidth() / 2; for (int i = 0; i < l.length; i++) { canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint); } super.onDraw(canvas); } }
5. Main.java
package android.test; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView list = (ListView) findViewById(R.id.myListView); ArrayList<String> stringList = InitListViewData(); MyAdapter adapter = new MyAdapter(this, stringList); list.setAdapter(adapter); SideBar indexBar = (SideBar) findViewById(R.id.sideBar); indexBar.setListView(list); } private ArrayList<String> InitListViewData() { ArrayList<String> stringList = new ArrayList<String>(); stringList.add("aback"); stringList.add("abash"); stringList.add("abbey"); stringList.add("abhor"); stringList.add("abide"); stringList.add("abuse"); stringList.add("candidate"); stringList.add("capture"); stringList.add("careful"); stringList.add("catch"); stringList.add("cause"); stringList.add("celebrate"); stringList.add("forever"); stringList.add("fable"); stringList.add("fidelity"); stringList.add("fox"); stringList.add("funny"); stringList.add("fail"); stringList.add("jail"); stringList.add("jade"); stringList.add("jailor"); stringList.add("january"); stringList.add("jasmine"); stringList.add("jazz"); stringList.add("zero"); stringList.add("zoo"); stringList.add("zeus"); stringList.add("zebra"); stringList.add("zest"); stringList.add("zing"); return stringList; } }