// 获取标题栏索引
int position = sectionIndexter.getPositionForSection(l[idx]);
if (position == -) {
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 ;
}
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();
if (position == ) {
setSection(header, label);
} else {
String preLabel = stringArray.get(position - );
char preFirstChar = preLabel.toUpperCase().charAt();
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(, ).toUpperCase());
text.setTextSize();
text.setPadding(, , , );
text.setGravity(Gravity.CENTER_VERTICAL);
header.addView(text);
}
public int getPositionForSection(int section) {
if (section == ) {
return ;
}
for (int i = ; i < stringArray.size(); i++) {
String l = stringArray.get(i);
char firstChar = l.toUpperCase().charAt();
if (firstChar == section) {
return i;
}
}
return -;
}
public int getSectionForPosition(int arg0) {
return ;
}
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 = ;
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 - ;
} else if (idx < ) {
idx = ;
}
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 == -) {
return true;
}
list.setSelection(position);
}
return true;
}
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(0xFFA6A9AA);
paint.setTextSize(getMeasuredHeight()/);
paint.setTextAlign(Paint.Align.CENTER);
float widthCenter = getMeasuredWidth() / ;
for (int i = ; 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;
}
}