天天看点

ListView 实现点击侧边A-Z快速查找[中英文排序混排]

相信大家一定见过这样的一个效果吧,也相信大家也在网上找到了许多的例子,但是大多的都是残缺不全的,没能真正的实现大家的一个效果吧,那么今天我就和大家分享我的这个完全的源代码,希望能对大家有所帮助吧,需要的人可以直接拿过去用,至于技术点嘛,其实没什么的,对于获取拼音的用到了一个pinyin4j-2.5.0.jar这个jar包,可以帮助我们实现效果。

还是直接上效果图,之后再上源码吧。

ListView 实现点击侧边A-Z快速查找[中英文排序混排]

 先看旁边的那个26个字母控件的实现:

自己画了出26个字母。

[java]  view plain copy print ?

  1. public class MySideBar extends View {  
  2.     OnTouchingLetterChangedListener onTouchingLetterChangedListener;  
  3.     // 26个字母  
  4.     public static String[] b = { "#", "A", "B", "C", "D", "E", "F", "G", "H",  
  5.             "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",  
  6.             "V", "W", "X", "Y", "Z" };  
  7.     int choose = -1;  
  8.     Paint paint = new Paint();  
  9.     public MySideBar(Context context, AttributeSet attrs, int defStyle) {  
  10.         super(context, attrs, defStyle);  
  11.     }  
  12.     public MySideBar(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.     }  
  15.     public MySideBar(Context context) {  
  16.         super(context);  
  17.     }  
  18.     protected void onDraw(Canvas canvas) {  
  19.         super.onDraw(canvas);  
  20.         if (showBkg) {  
  21.             canvas.drawColor(Color.parseColor("#40000000"));  
  22.         }  
  23.         int height = getHeight();  
  24.         int width = getWidth();  
  25.         int singleHeight = height / b.length;  
  26.         for (int i = 0; i < b.length; i++) {  
  27.             paint.setColor(Color.BLACK);  
  28.             // paint.setColor(Color.WHITE);  
  29.             paint.setTypeface(Typeface.DEFAULT_BOLD);  
  30.             paint.setAntiAlias(true);  
  31.             paint.setTextSize(20);  
  32.             if (i == choose) {  
  33.                 paint.setColor(Color.parseColor("#3399ff"));  
  34.                 paint.setFakeBoldText(true);  
  35.             }  
  36.             float xPos = width / 2 - paint.measureText(b[i]) / 2;  
  37.             float yPos = singleHeight * i + singleHeight;  
  38.             canvas.drawText(b[i], xPos, yPos, paint);  
  39.             paint.reset();  
  40.         }  
  41.     }  
  42.     private boolean showBkg = false;  
  43.     @Override  
  44.     public boolean dispatchTouchEvent(MotionEvent event) {  
  45.         final int action = event.getAction();  
  46.         final float y = event.getY();  
  47.         final int oldChoose = choose;  
  48.         final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;  
  49.         final int c = (int) (y / getHeight() * b.length);  
  50.         switch (action) {  
  51.         case MotionEvent.ACTION_DOWN:  
  52.             showBkg = true;  
  53.             if (oldChoose != c && listener != null) {  
  54.                 if (c > 0 && c < b.length) {  
  55.                     listener.onTouchingLetterChanged(b[c]);  
  56.                     choose = c;  
  57.                     invalidate();  
  58.                 }  
  59.             }  
  60.             break;  
  61.         case MotionEvent.ACTION_MOVE:  
  62.             if (oldChoose != c && listener != null) {  
  63.                 if (c > 0 && c < b.length) {  
  64.                     listener.onTouchingLetterChanged(b[c]);  
  65.                     choose = c;  
  66.                     invalidate();  
  67.                 }  
  68.             }  
  69.             break;  
  70.         case MotionEvent.ACTION_UP:  
  71.             showBkg = false;  
  72.             choose = -1;  
  73.             invalidate();  
  74.             break;  
  75.         }  
  76.         return true;  
  77.     }  
  78.     @Override  
  79.     public boolean onTouchEvent(MotionEvent event) {  
  80.         return super.onTouchEvent(event);  
  81.     }  
  82.     public void setOnTouchingLetterChangedListener(  
  83.             OnTouchingLetterChangedListener onTouchingLetterChangedListener) {  
  84.         this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;  
  85.     }  
  86.     public interface OnTouchingLetterChangedListener {  
  87.         public void onTouchingLetterChanged(String s);  
  88.     }  
  89. }   

再看布局文件的实现,这里用到FrameLayout来实现

[html]  view plain copy print ?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <FrameLayout  
  7.         android:id="@+id/llParent"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:orientation="vertical" >  
  11.         <ListView  
  12.             android:id="@+id/lvShow"  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="fill_parent" />  
  15.         <TextView  
  16.             android:id="@+id/tvLetter"  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:layout_gravity="center"  
  20.             android:background="@drawable/show_head_toast_bg"  
  21.             android:gravity="center"  
  22.             android:maxWidth="70dip"  
  23.             android:minWidth="70dip"  
  24.             android:padding="10dip"  
  25.             android:textColor="#99FFFFFF"  
  26.             android:textSize="50sp" >  
  27.         </TextView>  
  28.         <com.jiahui.view.MySideBar  
  29.             android:id="@+id/myView"  
  30.             android:layout_width="30dip"  
  31.             android:layout_height="fill_parent"  
  32.             android:layout_gravity="right" >  
  33.         </com.jiahui.view.MySideBar>  
  34.     </FrameLayout>  
  35. </LinearLayout>  

接下来再看Activity里的实现了,详细的请直接看代码吧,不懂的可以加Q

[java]  view plain copy print ?

  1. public class TestActivity extends Activity implements  
  2.         OnTouchingLetterChangedListener {  
  3.     private ListView lvShow;  
  4.     private List<UserInfo> userInfos;  
  5.     private TextView overlay;  
  6.     private MySideBar myView;  
  7.     private MyUserInfoAdapter adapter;  
  8.     private OverlayThread overlayThread = new OverlayThread();  
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         // TODO Auto-generated method stub  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.         lvShow = (ListView) findViewById(R.id.lvShow);  
  15.         myView = (MySideBar) findViewById(R.id.myView);  
  16.         overlay = (TextView) findViewById(R.id.tvLetter);  
  17.         lvShow.setTextFilterEnabled(true);  
  18.         overlay.setVisibility(View.INVISIBLE);  
  19.         getUserInfos();  
  20.         Log.i("coder", "userInfos.size" + userInfos.size());  
  21.         adapter = new MyUserInfoAdapter(this, userInfos);  
  22.         lvShow.setAdapter(adapter);  
  23.         myView.setOnTouchingLetterChangedListener(this);  
  24.     }  
  25.     private void getUserInfos() {  
  26.         UserInfo[] userinfoArray = new UserInfo[] {  
  27.                 new UserInfo("唐僧", "18765432345", PinyinUtils.getAlpha("唐僧")),  
  28.                 new UserInfo("猪师弟", "18765432345", PinyinUtils.getAlpha("猪师弟")),  
  29.                 new UserInfo("阿呆", "18765432345", PinyinUtils.getAlpha("阿呆")),  
  30.                 new UserInfo("8899", "18765432345",  
  31.                         PinyinUtils.getAlpha("8899")),  
  32.                 new UserInfo("孙悟空", "18765432345", PinyinUtils.getAlpha("孙悟空")),  
  33.                 new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),  
  34.                 new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),  
  35.                 new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),  
  36.                 new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),  
  37.                 new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),  
  38.                 new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),  
  39.                 new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),  
  40.                 new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),  
  41.                 new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),  
  42.                 new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),  
  43.                 new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),  
  44.                 new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),  
  45.                 new UserInfo("李四", "18909876545", PinyinUtils.getAlpha("李四")),  
  46.                 new UserInfo("王小二", "18909876545", PinyinUtils.getAlpha("王小二")),  
  47.                 new UserInfo("张三丰", "18909876545", PinyinUtils.getAlpha("张三丰")),  
  48.                 new UserInfo("郭靖", "18909876545", PinyinUtils.getAlpha("郭靖")),  
  49.                 new UserInfo("张无忌", "18909876545", PinyinUtils.getAlpha("张无忌")),  
  50.                 new UserInfo("黄小贤", "18909876545", PinyinUtils.getAlpha("黄小贤")) };  
  51.         Arrays.sort(userinfoArray, new PinyinComparator());  
  52.         userInfos = Arrays.asList(userinfoArray);  
  53.     }  
  54.     private Handler handler = new Handler() {  
  55.     };  
  56.     private class OverlayThread implements Runnable {  
  57.         public void run() {  
  58.             overlay.setVisibility(View.GONE);  
  59.         }  
  60.     }  
  61.     @Override  
  62.     public void onTouchingLetterChanged(String s) {  
  63.         Log.i("coder", "s:" + s);  
  64.         overlay.setText(s);  
  65.         overlay.setVisibility(View.VISIBLE);  
  66.         handler.removeCallbacks(overlayThread);  
  67.         handler.postDelayed(overlayThread, 1000);  
  68.         if (alphaIndexer(s) > 0) {  
  69.             int position = alphaIndexer(s);  
  70.             Log.i("coder", "position:" + position);  
  71.             lvShow.setSelection(position);  
  72.         }  
  73.     }  
  74.     public int alphaIndexer(String s) {  
  75.         int position = 0;  
  76.         for (int i = 0; i < userInfos.size(); i++) {  
  77.             if (userInfos.get(i).getPy().startsWith(s)) {  
  78.                 position = i;  
  79.                 break;  
  80.             }  
  81.         }  
  82.         Log.i("coder", "i" + position + userInfos.get(position));  
  83.         return position;  
  84.     }  
  85. }  

好了这样的一效果就算完成了,也希望大家能分享更多的东西出来,大家一起成长一起学习!

本文系转载,原文出处http://blog.csdn.net/jiahui524/article/details/7785606,若不慎冒犯作者,请与我联系,谢谢!