1悔详,右邊的檢索SideBar
源碼:
public class SideBarextends View {
private char[]l;
? ? private SectionIndexersectionIndexter =null;
? ? private ListViewlist;
? ? private TextViewmDialogText;
? ? Bitmapmbitmap;
? ? private int type =1;
? ? private int color =0xff858c94;
? ? 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','#'};
? ? ? ? mbitmap = BitmapFactory.decodeResource(getResources(),
? ? ? ? ? ? ? ? R.drawable.scroll_bar_search_icon);
? ? }
public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
? ? ? ? init();
? ? }
public void setListView(ListView _list) {
list = _list;
? ? ? ? ChooseCityAdapter ad = (ChooseCityAdapter) _list.getAdapter(); //listview適配器
? ? ? ? sectionIndexter = ad;
? ? }
public void setTextView(TextView mDialogText) {
this.mDialogText = mDialogText;
? ? }
@Override
? ? public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
? ? ? ? int i = (int) event.getY();
? ? ? ? int idx = i / (getMeasuredHeight() /l.length);
? ? ? ? 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) {
setBackgroundResource(R.drawable.scrollbar_bg);
? ? ? ? ? ? mDialogText.setVisibility(View.VISIBLE);
? ? ? ? ? ? if (idx ==0) {
mDialogText.setText("Search");
? ? ? ? ? ? ? ? mDialogText.setTextSize(16);
? ? ? ? ? ? }else {
mDialogText.setText(String.valueOf(l[idx]));
? ? ? ? ? ? ? ? mDialogText.setTextSize(34);
? ? ? ? ? ? }
if (sectionIndexter ==null) {
sectionIndexter = (SectionIndexer)list.getAdapter();
? ? ? ? ? ? }
int position =sectionIndexter.getPositionForSection(l[idx]);
? ? ? ? ? ? if (position == -1) {
return true;
? ? ? ? ? ? }
list.setSelection(position);
? ? ? ? }else {
mDialogText.setVisibility(View.INVISIBLE);
? ? ? ? }
if (event.getAction() == MotionEvent.ACTION_UP) {
setBackgroundDrawable(new ColorDrawable(0x00000000));
? ? ? ? }
return true;
? ? }
@Override
? ? protected void onDraw(Canvas canvas) {
Paint paint =new Paint();
? ? ? ? paint.setColor(color);
? ? ? ? paint.setTextSize(24);
? ? ? ? paint.setStyle(Paint.Style.FILL);
? ? ? ? paint.setTextAlign(Paint.Align.CENTER);
? ? ? ? float widthCenter = getMeasuredWidth() /2;
? ? ? ? if (l.length >0) {
float height = getMeasuredHeight() /l.length;
? ? ? ? ? ? for (int i =0; i
if (i ==0 &&type !=2) {
canvas.drawBitmap(mbitmap, widthCenter -7, (i +1)
* height - height /2, paint);
? ? ? ? ? ? ? ? }else
? ? ? ? ? ? ? ? ? ? canvas.drawText(String.valueOf(l[i]), widthCenter,
? ? ? ? ? ? ? ? ? ? ? ? ? ? (i +1) * height, paint);
? ? ? ? ? ? }
}
this.invalidate();
? ? ? ? super.onDraw(canvas);
? ? }
}
2佩憾,適配器的更改
適配器實(shí)現(xiàn)SectionIndexer接口救斑,實(shí)現(xiàn)它的方法
@Override
public Object[]getSections() {
return new Object[0];
}
public int getSectionForPosition(int position) {
String letter = FirstLetterUtil.getFirstLetter(lists.get(position).getName()).toUpperCase();
? return letter.charAt(0);
}
public int getPositionForSection(int section) {
for (int i =0; i < getCount(); i++) {
String letter = FirstLetterUtil.getFirstLetter(lists.get(i).getName()).toUpperCase();
? ? ? char firstChar = letter.toUpperCase().charAt(0);
? ? ? if (firstChar == section) {
return i;
? ? ? }
}
return -1;
}
3,關(guān)聯(lián)listview與SideBar
sideBar.setListView(lv_listview);
sideBar.setTextView(tv_textview);
4.其他
1岸霹,獲取漢字名稱首字母
public class FirstLetterUtil {
????private static int BEGIN =45217;
? ? private static int END =63486;
? ? // 按照聲母表示栖忠,這個(gè)表是在GB2312中的出現(xiàn)的第一個(gè)漢字蝌戒,也就是說(shuō)“啊”是代表首字母a的第一個(gè)漢字召烂。
? ? // i, u, v都不做聲母, 自定規(guī)則跟隨前面的字母
? ? private static char[]chartable = {'啊', '芭', '擦', '搭', '蛾', '發(fā)', '噶', '哈',
? ? ? ? ? ? '哈', '擊', '喀', '垃', '媽', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌',
? ? ? ? ? ? '塌', '挖', '昔', '壓', '匝', };
? ? // 二十六個(gè)字母區(qū)間對(duì)應(yīng)二十七個(gè)端點(diǎn)
? ? // GB2312碼漢字區(qū)間十進(jìn)制表示
? ? private static int[]table =new int[27];
? ? // 對(duì)應(yīng)首字母區(qū)間表
? ? private static char[]initialtable = {'a', 'b', 'c', 'd', 'e', 'f', 'g',
? ? ? ? ? ? 'h', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
? ? ? ? ? ? 't', 't', 'w', 'x', 'y', 'z', };
? ? // 初始化
? ? static {
????????for (int i =0; i <26; i++) {
????????????table[i] =gbValue(chartable[i]);// 得到GB2312碼的首字母區(qū)間端點(diǎn)表碱工,十進(jìn)制。
? ? ? ? }
????????table[26] =END;// 區(qū)間表結(jié)尾
? ? }
/**
? ? * 根據(jù)一個(gè)包含漢字的字符串返回一個(gè)漢字拼音首字母的字符串 最重要的一個(gè)方法奏夫,思路如下:一個(gè)個(gè)字符讀入怕篷、判斷、輸出
? ? */
? ? public static StringgetFirstLetter(String sourceStr) {
????????String result ="";
? ? ? ? String str = sourceStr.toLowerCase();
? ? ? ? int StrLength = str.length();
? ? ? ? int i;
? ? ? ? try {
????????????for (i =0; i < StrLength; i++) {
????????????????result +=Char2Initial(str.charAt(i));
? ? ? ? ? ? }
????????}catch (Exception e) {
????????????result ="";
? ? ? ? }
????return result;
? ? }
/**
? ? * 輸入字符,得到他的聲母,英文字母返回對(duì)應(yīng)的大寫字母,其他非簡(jiǎn)體漢字返回 '0'
*/
? ? private static char Char2Initial(char ch) {
// 對(duì)英文字母的處理:小寫字母轉(zhuǎn)換為大寫酗昼,大寫的直接返回
? ? ? ? if (ch >='a' && ch <='z') {
????????????return ch;
? ? ? ? }
????????if (ch >='A' && ch <='Z') {
????????????return ch;
? ? ? ? }
// 對(duì)非英文字母的處理:轉(zhuǎn)化為首字母廊谓,然后判斷是否在碼表范圍內(nèi),
? ? ? ? // 若不是麻削,則直接返回蒸痹。
? ? ? ? // 若是,則在碼表內(nèi)的進(jìn)行判斷碟婆。
? ? ? ? int gb =gbValue(ch);// 漢字轉(zhuǎn)換首字母
? ? ? ? if ((gb END))// 在碼表區(qū)間之前电抚,直接返回
? ? ? ? {
????????????return ch;
? ? ? ? }
????????int i;
? ? ? ? for (i =0; i <26; i++) {// 判斷匹配碼表區(qū)間,匹配到就break,判斷區(qū)間形如“[,)”
? ? ? ? ? ? if ((gb >=table[i]) && (gb
????????????????break;
? ? ? ? ? ? }
????????}
????????if (gb ==END) {// 補(bǔ)上GB2312區(qū)間最右端
? ? ? ? ? ? i =25;
? ? ? ? }
????????return initialtable[i]; // 在碼表區(qū)間中竖共,返回首字母
? ? }
/**
? ? * 取出漢字的編碼 cn 漢字
? ? */
? ? private static int gbValue(char ch) {// 將一個(gè)漢字(GB2312)轉(zhuǎn)換為十進(jìn)制表示。
? ? ? ? String str =new String();
? ? ? ? str += ch;
? ? ? ? try {
????????????byte[] bytes = str.getBytes("GB2312");
? ? ? ? ? ? if (bytes.length <2) {
????????????????return 0;
? ? ? ? ? ? }
????????????return (bytes[0] <<8 &0xff00) + (bytes[1] &0xff);
? ? ? ? }catch (Exception e) {
????????????return 0;
? ? ? ? }
}
}
2俺祠,listview數(shù)據(jù)排序
Collections.sort(cityLists, new Comparator() {
@Override
? ? public int compare(CityBean lhs, CityBean rhs) {
????????String letter1 = FirstLetterUtil.getFirstLetter(lhs.getName()).toUpperCase();
? ? ? ? String letter2 = FirstLetterUtil.getFirstLetter(rhs.getName()).toUpperCase();
? ? ? ? //這里主要是用來(lái)對(duì)ListView里面的數(shù)據(jù)根據(jù)ABCDEFG...來(lái)排序
? ? ? ? if (letter1.equals("@") || letter2.equals("#")) {
????????????return -1;
? ? ? ? }else if (letter1.equals("#")????|| letter2.equals("@")) {
????????????return 1;
? ? ? ? }else {
? ? ? ? ? ? ? ?return letter1.compareTo(letter2);
? ? ? ? }
}
});
3公给,適配器字母控件的顯示隱藏
String currentStr = FirstLetterUtil.getFirstLetter(cityBean.getName()).toUpperCase().substring(0,1);
String index =null;
if (position ==0) {
index = currentStr;
}else {
String lastStr = FirstLetterUtil.getFirstLetter(lists.get(position -1).getName()).toUpperCase().substring(0,1);
? if (!TextUtils.equals(lastStr,currentStr)) {
index = currentStr;
? }
}
viewHolder.tv_letter.setText(currentStr);
//首字母相同的姓氏只顯示第一個(gè)名字上面的首字母,其他的隱藏掉
viewHolder.tv_letter.setVisibility(index ==null ? View.GONE : View.VISIBLE);
**********************END*********************
我是靜染星辰,私人微信:azxy986753
歡迎添加微信蜘渣,互相學(xué)習(xí)淌铐,互相進(jìn)步!