网上的这种demo太多太多了,但是他们只能实现基本的A-Z排序,也就是仅仅是首字母排序,涉及到一些特别人名字排序的时候相同姓氏会被分开,这也是由于他是按照把中文汉字全部转化为汉语拼音后进行一一比较而造成的结果,给一张图就很明了了
是不是发现姓李中间出现了姓刘的情况?这是因为 李四光的拼音是lisiguang 而刘德华呢是liudehua,李小龙是lixiaolong,
这样应该很清楚了吧,这种排序显然达不到我们实际的效果
我在这里帖上关键的代码吧,其他的网上也都 有就不多BB了
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
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.view.ViewGroup.MarginLayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;
import com.example.sortlistview.SideBar.OnTouchingLetterChangedListener;
public class ActivityAddFriends extends Activity implements SectionIndexer {
private ListView sortListView;
private SideBar sideBar;
private TextView dialog;
private SortGroupMemberAdapter adapter;
private ClearEditText mClearEditText;
private LinearLayout titleLayout;
private TextView title;
private TextView tvNofriends;
/**
* 上次第一个可见元素,用于滚动时记录标识。
*/
private int lastFirstVisibleItem = -1;
/**
* 汉字转换成拼音的类
*/
private CharacterParser characterParser;
private List<GroupMemberBean> SourceDateList;
private List<GroupMemberBean> SourceDateListTwo = new ArrayList<GroupMemberBean>();
/**
* 根据拼音来排列ListView里面的数据类
*/
private PinyinComparator pinyinComparator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_friends);
initViews();
// System.out.println("qqqqqqq==");
// String[] newArray = getResources().getStringArray(R.array.date) ;
// Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
// List<String> list = Arrays.asList(newArray);
// Collections.sort(list, com);
// System.out.println("qqqqqqq=="+list);
// for(String i:list){
// System.out.println("qqqqqqq=="+i);
// }
//
}
private void initViews() {
titleLayout = (LinearLayout) findViewById(R.id.title_layout);
title = (TextView) this.findViewById(R.id.title_layout_catalog);
tvNofriends = (TextView) this
.findViewById(R.id.title_layout_no_friends);
// 实例化汉字转拼音类
characterParser = CharacterParser.getInstance();
pinyinComparator = new PinyinComparator();
sideBar = (SideBar) findViewById(R.id.sidrbar);
dialog = (TextView) findViewById(R.id.dialog);
sideBar.setTextView(dialog);
// 设置右侧触摸监听
sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
@Override
public void onTouchingLetterChanged(String s) {
// 该字母首次出现的位置
int position = adapter.getPositionForSection(s.charAt(0));
if (position != -1) {
sortListView.setSelection(position);
}
}
});
sortListView = (ListView) findViewById(R.id.country_lvcountry);
sortListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 这里要利用adapter.getItem(position)来获取当前position所对应的对象
Toast.makeText(
getApplication(),
((GroupMemberBean) adapter.getItem(position)).getName(),
Toast.LENGTH_SHORT).show();
}
});
// SourceDateList = filledData(getResources().getStringArray(R.array.date));
String[] newArray = getResources().getStringArray(R.array.date) ;
Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
List<String> list = Arrays.asList(newArray);
Collections.sort(list, com);
// for(String i:list){
// }
SourceDateListTwo = filledDataTwo(list) ;
SourceDateList = filledData(SourceDateListTwo) ;
System.out.println("qqqqqqq=="+SourceDateList.toString());
// // 根据a-z进行排序源数据
Collections.sort(SourceDateList, pinyinComparator);
adapter = new SortGroupMemberAdapter(this, SourceDateList);
sortListView.setAdapter(adapter);
sortListView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int section = getSectionForPosition(firstVisibleItem);
int nextSection = getSectionForPosition(firstVisibleItem + 1);
int nextSecPosition = getPositionForSection(+nextSection);
if (firstVisibleItem != lastFirstVisibleItem) {
MarginLayoutParams params = (MarginLayoutParams) titleLayout
.getLayoutParams();
params.topMargin = 0;
titleLayout.setLayoutParams(params);
title.setText(SourceDateList.get(
getPositionForSection(section)).getSortLetters());
}
if (nextSecPosition == firstVisibleItem + 1) {
View childView = view.getChildAt(0);
if (childView != null) {
int titleHeight = titleLayout.getHeight();
int bottom = childView.getBottom();
MarginLayoutParams params = (MarginLayoutParams) titleLayout
.getLayoutParams();
if (bottom < titleHeight) {
float pushedDistance = bottom - titleHeight;
params.topMargin = (int) pushedDistance;
titleLayout.setLayoutParams(params);
} else {
if (params.topMargin != 0) {
params.topMargin = 0;
titleLayout.setLayoutParams(params);
}
}
}
}
lastFirstVisibleItem = firstVisibleItem;
}
});
mClearEditText = (ClearEditText) findViewById(R.id.filter_edit);
// 根据输入框输入值的改变来过滤搜索
mClearEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// 这个时候不需要挤压效果 就把他隐藏掉
titleLayout.setVisibility(View.GONE);
// 当输入框里面的值为空,更新为原来的列表,否则为过滤数据列表
filterData(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
/**
* 为ListView填充数据
*
* @param date
* @return
*/
private List<GroupMemberBean> filledData(List<GroupMemberBean> SourceDateListTwo) {
List<GroupMemberBean> mSortList = new ArrayList<GroupMemberBean>();
for (int i = 0; i < SourceDateListTwo.size(); i++) {
GroupMemberBean sortModel = new GroupMemberBean();
sortModel.setName(SourceDateListTwo.get(i).getName());
// 汉字转换成拼音
String pinyin = characterParser.getSelling(SourceDateListTwo.get(i).getName());
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 date
* @return
*/
private List<GroupMemberBean> filledDataTwo(List<String> list) {
List<GroupMemberBean> mSortList = new ArrayList<GroupMemberBean>();
for (int i = 0; i < list.size(); i++) {
GroupMemberBean sortModel = new GroupMemberBean();
sortModel.setName(list.get(i));
// 汉字转换成拼音
String pinyin = characterParser.getSelling(list.get(i));
System.out.println("pinyin=="+pinyin);
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<GroupMemberBean> filterDateList = new ArrayList<GroupMemberBean>();
if (TextUtils.isEmpty(filterStr)) {
filterDateList = SourceDateList;
tvNofriends.setVisibility(View.GONE);
} else {
filterDateList.clear();
for (GroupMemberBean sortModel : SourceDateList) {
String name = sortModel.getName();
if (name.indexOf(filterStr.toString()) != -1
|| characterParser.getSelling(name).startsWith(
filterStr.toString())) {
filterDateList.add(sortModel);
}
}
}
// 根据a-z进行排序
Collections.sort(filterDateList, pinyinComparator);
adapter.updateListView(filterDateList);
if (filterDateList.size() == 0) {
tvNofriends.setVisibility(View.VISIBLE);
}
}
@Override
public Object[] getSections() {
return null;
}
/**
* 根据ListView的当前位置获取分类的首字母的Char ascii值
*/
public int getSectionForPosition(int position) {
return SourceDateList.get(position).getSortLetters().charAt(0);
}
/**
* 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
*/
public int getPositionForSection(int section) {
for (int i = 0; i < SourceDateList.size(); i++) {
String sortStr = SourceDateList.get(i).getSortLetters();
char firstChar = sortStr.toUpperCase().charAt(0);
if (firstChar == section) {
return i;
}
}
return -1;
}
}
这里面主要代码是这块:
String[] newArray = getResources().getStringArray(R.array.date) ;
Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
List<String> list = Arrays.asList(newArray);
Collections.sort(list, com);
// for(String i:list){
// }
SourceDateListTwo = filledDataTwo(list) ;
SourceDateList = filledData(SourceDateListTwo) ;
"date"就是数据,我这里是测试的死的数据 ,改成从服务器获取的数据就行了
先将中文进行排序再按首字母进行排序就行了。