城市ListView 按字母分类展示,右侧是A-Z 滑动选择,类似于iphone通讯之类的效果。

解决方案 »

  1.   

    public class SideBar extends View {
        private char[] l;
        private SectionIndexer sectionIndexter = null;
        private ListView list;
        private final int m_nItemHeight = 22;    public SideBar(Context context) {
            super(context);
            init();
        }    public SideBar(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }    private void init() {
            l = new char[] { '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' };
            setBackgroundColor(0x44FFFFFF);
        }    public SideBar(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }    public void setListView(ListView _list) {
            list = _list;
            sectionIndexter = (SectionIndexer) _list.getAdapter();
        }    public boolean onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);
            int i = (int) event.getY();
            int idx = i / m_nItemHeight;
            if (idx >= l.length) {
                idx = l.length - 1;
            } else if (idx < 0) {
                idx = 0;
            }
            if (event.getAction() == MotionEvent.ACTION_DOWN
                    || event.getAction() == MotionEvent.ACTION_MOVE) {
                if (sectionIndexter == null) {
                    sectionIndexter = (SectionIndexer) list.getAdapter();
                }
                int position = sectionIndexter.getPositionForSection(l[idx]);
                if (position == -1) {
                    return true;
                }
                list.setSelection(position);
            }
            return true;
        }    protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setColor(0xFFA6A9AA);
            paint.setTextSize(16);
            paint.setTextAlign(Paint.Align.CENTER);
            float widthCenter = getMeasuredWidth() / 2;
            for (int i = 0; i < l.length; i++) {
                canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight
                        + (i * m_nItemHeight), paint);
            }
            super.onDraw(canvas);
        }
    }
    public class HzToPinyin {
        /**
         * 
         * [汉字转拼音]
         * 
         * @param src
         *            欲转换的汉字
         * @return [汉字的拼音的首字母]
         * @createTime 2012-1-4 下午03:56:31
         */
        public static String getPingYin(String src) {        char[] t1 = null;
            t1 = src.toCharArray();
            String[] t2 = new String[t1.length];
            HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
            t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            t3.setVCharType(HanyuPinyinVCharType.WITH_V);
            String t4 = "";
            int t0 = t1.length;
            try {
                for (int i = 0; i < t0; i++) {
                    // 判断是否为汉字字符
                    if (java.lang.Character.toString(t1[i]).matches(
                            "[\\u4E00-\\u9FA5]+")) {
                        t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                        t4 += t2[0];
                    } else
                        t4 += java.lang.Character.toString(t1[i]);
                }
                System.out.println(t4);
                return t4;
            } catch (BadHanyuPinyinOutputFormatCombination e1) {
                e1.printStackTrace();
            }
            return t4;
        }    // 返回中文的首字母
        public static String getPinYinHeadChar(String str) {        String convert = "";
            for (int j = 0; j < str.length(); j++) {
                char word = str.charAt(j);
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
                if (pinyinArray != null) {
                    convert += pinyinArray[0].charAt(0);
                } else {
                    convert += word;
                }
            }
            return convert;
        }    // 将字符串转移为ASCII码
        public static String getCnASCII(String cnStr) {
            StringBuffer strBuf = new StringBuffer();
            byte[] bGBK = cnStr.getBytes();
            for (int i = 0; i < bGBK.length; i++) {
                // System.out.println(Integer.toHexString(bGBK[i]&0xff));
                strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
            }
            return strBuf.toString();
        }}  <com.wisedu.hanlinyuan.widget.SideBar
            android:id="@+id/sideBar"
            android:layout_width="20dip"
            android:layout_marginRight="10dip"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/new_movement_select_all" />再去下载个pinyin4j.jar就ok了