ListView 实现点击侧边A-Z快速查找[中英文排序混排]
相信大家一定见过这样的一个效果吧,也相信大家也在网上找到了许多的例子,但是大多的都是残缺不全的,没能真正的实现大家的一个效果吧,那么今天我就和大家分享我的这个完全的源代码,希望能对大家有所帮助吧,需要的人可以直接拿过去用,至于技术点嘛,其实没什么的,对于获取拼音的用到了一个pinyin4j-2.5.0.jar这个jar包,可以帮助我们实现效果。
还是直接上效果图,之后再上源码吧。
先看旁边的那个26个字母控件的实现:
自己画了出26个字母。
public class TestActivity extends Activity implementsOnTouchingLetterChangedListener {private ListView lvShow;private List<UserInfo> userInfos;private TextView overlay;private MySideBar myView;private MyUserInfoAdapter adapter;private OverlayThread overlayThread = new OverlayThread();@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);lvShow = (ListView) findViewById(R.id.lvShow);myView = (MySideBar) findViewById(R.id.myView);overlay = (TextView) findViewById(R.id.tvLetter);lvShow.setTextFilterEnabled(true);overlay.setVisibility(View.INVISIBLE);getUserInfos();Log.i("coder", "userInfos.size" + userInfos.size());adapter = new MyUserInfoAdapter(this, userInfos);lvShow.setAdapter(adapter);myView.setOnTouchingLetterChangedListener(this);}private void getUserInfos() {UserInfo[] userinfoArray = new UserInfo[] {new UserInfo("唐僧", "18765432345", PinyinUtils.getAlpha("唐僧")),new UserInfo("猪师弟", "18765432345", PinyinUtils.getAlpha("猪师弟")),new UserInfo("阿呆", "18765432345", PinyinUtils.getAlpha("阿呆")),new UserInfo("8899", "18765432345",PinyinUtils.getAlpha("8899")),new UserInfo("孙悟空", "18765432345", PinyinUtils.getAlpha("孙悟空")),new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),new UserInfo("李四", "18909876545", PinyinUtils.getAlpha("李四")),new UserInfo("王小二", "18909876545", PinyinUtils.getAlpha("王小二")),new UserInfo("张三丰", "18909876545", PinyinUtils.getAlpha("张三丰")),new UserInfo("郭靖", "18909876545", PinyinUtils.getAlpha("郭靖")),new UserInfo("张无忌", "18909876545", PinyinUtils.getAlpha("张无忌")),new UserInfo("黄小贤", "18909876545", PinyinUtils.getAlpha("黄小贤")) };Arrays.sort(userinfoArray, new PinyinComparator());userInfos = Arrays.asList(userinfoArray);}private Handler handler = new Handler() {};private class OverlayThread implements Runnable {public void run() {overlay.setVisibility(View.GONE);}}@Overridepublic void onTouchingLetterChanged(String s) {Log.i("coder", "s:" + s);overlay.setText(s);overlay.setVisibility(View.VISIBLE);handler.removeCallbacks(overlayThread);handler.postDelayed(overlayThread, 1000);if (alphaIndexer(s) > 0) {int position = alphaIndexer(s);Log.i("coder", "position:" + position);lvShow.setSelection(position);}}public int alphaIndexer(String s) {int position = 0;for (int i = 0; i < userInfos.size(); i++) {if (userInfos.get(i).getPy().startsWith(s)) {position = i;break;}}Log.i("coder", "i" + position + userInfos.get(position));return position;}}
好了这样的一效果就算完成了,也希望大家能分享更多的东西出来,大家一起成长一起学习!
如需转载引用请注明出处:http://blog.csdn.net/jiahui524