activity_main.xml布局
<RelativeLayout 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" >
<EditText
android:id="@+id/et_text"
android:layout_width="200dp"
android:layout_height="40dp"
android:hint="输入关键字" />
<ListView
android:id="@+id/lv_listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/et_text"
android:background="#ffffff"
android:cacheColorHint="#00000000"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dip"
android:scrollbars="none" />
<com.example.tongxunlu.MySideBar
android:id="@+id/sb_sidebar"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
activity_main_item.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_tvLetter"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#ccc"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="test"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_tvTitle"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="#ccc"
android:text="test"
android:textSize="20dip" />
</LinearLayout>
MainActivity代码
package com.example.tongxunlu;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.example.tongxunlu.MySideBar.OnTouchingLetterChangedListener;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private MySideBar sb_sidebar;
private ListView lv_listview;
private SortAdapter adapter;
private List<SortModel> sourceDateList;
private CharacterParser characterParser;
private PinyinComparator pinyinComparator;
private TextView tv_clear;
private EditText et_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
characterParser = CharacterParser.getInstance();
pinyinComparator = new PinyinComparator();
lv_listview = (ListView) findViewById(R.id.lv_listview);
sb_sidebar = (MySideBar) findViewById(R.id.sb_sidebar);
et_text = (EditText) findViewById(R.id.et_text);
// 设置右侧触摸监听
sb_sidebar
.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
@Override
public void onTouchingLetterChanged(String s) {
// 该字母首次出现的位置
int position = adapter.getPositionForSection(s
.charAt(0));
if (position != -1) {
lv_listview.setSelection(position);
}
}
});
lv_listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 这里要利用adapter.getItem(position)来获取当前position所对应的对象
Toast.makeText(getApplication(),
((SortModel) adapter.getItem(position)).getName(),
Toast.LENGTH_SHORT).show();
}
});
// 准备数据,设置适配器 资源文件下的数据
sourceDateList = filledData(getResources()
.getStringArray(R.array.dates));
// 根据a-z进行排序源数据
Collections.sort(sourceDateList, pinyinComparator);
adapter = new SortAdapter(this, sourceDateList);
lv_listview.setAdapter(adapter);
// 根据输入框输入值的改变来过滤搜索
et_text.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// 当输入框里面的值为空,更新为原来的列表,否则为过滤数据列表
filterData(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
/**
* 滑动状态设置
*/
/*lv_listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
break;
case OnScrollListener.SCROLL_STATE_FLING:
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//显示文本
//文本框显示
Toast.makeText(getApplication(),
((SortModel) adapter.getItem(firstVisibleItem)).getSortLetters(),
Toast.LENGTH_SHORT).show();
}
});*/
}
/**
* 为ListView填充数据
*
* @param date
* @return
*/
private List<SortModel> filledData(String[] date) {
List<SortModel> mSortList = new ArrayList<SortModel>();
for (int i = 0; i < date.length; i++) {
SortModel sortModel = new SortModel();
sortModel.setName(date[i]);
// 汉字转换成拼音
String pinyin = characterParser.getSelling(date[i]);
String sortString = pinyin.substring(0, 1).toUpperCase();
// 正则表达式,判断首字母是否是英文字母
if (sortString.matches("[A-Z]")) {
sortModel.setSortLetters(sortString.toUpperCase());
} else {
sortModel.setSortLetters("#");
}
mSortList.add(sortModel);
}
return mSortList;
}
/**
* 根据输入框中的值来过滤数据并更新ListView
*
* @param filterStr
*/
private void filterData(String filterStr) {
List<SortModel> filterDateList = new ArrayList<SortModel>();
if (TextUtils.isEmpty(filterStr)) {
filterDateList = sourceDateList;
} else {
filterDateList.clear();
for (SortModel sortModel : sourceDateList) {
String name = sortModel.getName();
if (name.toUpperCase().indexOf(
filterStr.toString().toUpperCase()) != -1
|| characterParser.getSelling(name).toUpperCase()
.startsWith(filterStr.toString().toUpperCase())) {