完整代码:http://download.csdn.net/detail/spring_0819/5792311
效果图:
xml代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/content_lv"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
></ListView>
<TextView
android:id="@+id/tv"
android:layout_height="60dip"
android:layout_width="60dip"
android:layout_gravity="center"
android:gravity="center"
android:background="#f0606060"
android:text="A"
android:textColor="#ffffff"
android:textSize="30sp"
/>
<LinearLayout
android:id="@+id/index_ll"
android:layout_width="30dip"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:gravity="center"
android:background="#d7d7d7"
android:orientation="vertical"
></LinearLayout>
</FrameLayout>
- xml布局如图:
Java代码:
- onCreate函数:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
index_ll = (LinearLayout) findViewById(R.id.index_ll);
content_lv = (ListView) findViewById(R.id.content_lv);
index_ll.setBackgroundColor(Color.parseColor("#00ffffff"));
tv = (TextView) findViewById(R.id.tv);
tv.setVisibility(View.INVISIBLE);
sortIndex();
listAdapter = new MyListAdapter(this);
content_lv.setAdapter(listAdapter);
}
- 由于LinearLayout的宽高在onCreate中得不到,故放在onWindowFoucsChanged中获得:
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
fontheight = index_ll.getHeight() / index_str.length;
fontwidth = index_ll.getWidth();
getIndexView();
}
- 用到的数据及一些控件的声明:
//用于索引的26个字母
private String[] index_str = { "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" };
//索引的内容
private String data[] = { "android", "java", "news", "baidu", "oberser",
"mary", "next", "ruby", "money", "lucy", "very", "thunder",
"object", "lily", "jay", "answer", "layout", "demos", "com",
"collect", "custom", "blog", "round", "redirect", "ground", "gray",
"blue", "zone", "james", "zhang", "location" };
//放在listview中的内容,包含索引的26个字母以及data
private String nData[];
private int fontheight,fontwidth;//索引的字体高度和宽度
LinearLayout index_ll;//索引所在的布局
ListView content_lv;
TextView tv;//显示触摸的具体索引字母
MyListAdapter listAdapter;
HashMap<String, Integer> selector;
- 对data进行排序:
private void sortIndex() {
// TODO Auto-generated method stub
TreeSet<String> set = new TreeSet<String>();
for(String string : data){
set.add(String.valueOf(string.charAt(0)));
}
nData = new String[set.size() + data.length];
int i = 0;
for(String string : set){
nData[i] = string;
i++;
}
System.arraycopy(data, 0, nData, set.size(), data.length);
Arrays.sort(nData, String.CASE_INSENSITIVE_ORDER);
}
- 绘制右方的索引列表:
private void getIndexView() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(fontwidth, fontheight);
Log.v("fontwidth", String.valueOf(fontwidth));
Log.v("fontheight", String.valueOf(fontheight));
for(int i = 0; i < index_str.length; i++){
final TextView tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText(index_str[i]);
tv.setGravity(Gravity.CENTER);
index_ll.addView(tv);
index_ll.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
float y = event.getY();
int index = (int) (y / fontheight);
if(index > -1 && index < index_str.length){
String key = index_str[index];
if(selector.containsKey(key)){
int pos = selector.get(key);
if(content_lv.getHeaderViewsCount() > 0){
content_lv.setSelectionFromTop(pos + content_lv.getHeaderViewsCount(), 0);
}else{
content_lv.setSelectionFromTop(pos, 0);
}
tv.setVisibility(View.VISIBLE);
tv.setText(index_str[index]);
}
}
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
index_ll.setBackgroundColor(Color.parseColor("#606060"));
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
index_ll.setBackgroundColor(Color.parseColor("#00ffffff"));
tv.setVisibility(View.INVISIBLE);
break;
}
return true;
}
});
}
}
- 适配器:
public class MyListAdapter extends BaseAdapter{ Context context; Holder holder; public MyListAdapter(Context context) { // super(); this.context = context; selector = new HashMap<String, Integer>(); for(int j = 0; j < index_str.length; j++){ for(int i = 0; i < nData.length; i++){ if(nData[i].equals(index_str[j].toLowerCase())){ selector.put(index_str[j], i); } } } } @Override public int getCount() { // TODO Auto-generated method stub return nData.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return nData[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub String item = nData[position]; if(item.length() == 1){ convertView = getLayoutInflater().inflate(R.layout.index, null); }else{ convertView = getLayoutInflater().inflate(R.layout.item, null); } TextView tv = (TextView) convertView.findViewById(R.id.textView1); tv.setText(item); return convertView; } class Holder{ TextView tv; } }