Android開發(fā)之自定義view實現(xiàn)通訊錄列表A~Z字母提示效果【附demo源碼下載】
本文實例講述了Android開發(fā)之自定義view實現(xiàn)通訊錄列表A~Z字母提示效果。分享給大家供大家參考,具體如下:
開發(fā)工具:eclipse
運行環(huán)境:htc G9 android2.3.3
話不多說,先看效果圖


其實左右邊的A~Z是一個自定義的View,它直接覆蓋在ListView上。
MyLetterListView:
public class MyLetterListView extends View {
OnTouchingLetterChangedListener onTouchingLetterChangedListener;
String[] b = {"#","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"};
int choose = -1;
Paint paint = new Paint();
boolean showBkg = false;
public MyLetterListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyLetterListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLetterListView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(showBkg){
canvas.drawColor(Color.parseColor("#40000000"));
}
int height = getHeight();
int width = getWidth();
int singleHeight = height / b.length;
for(int i=0;i<b.length;i++){
paint.setColor(Color.WHITE);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
if(i == choose){
paint.setColor(Color.parseColor("#3399ff"));
paint.setFakeBoldText(true);
}
float xPos = width/2 - paint.measureText(b[i])/2;
float yPos = singleHeight * i + singleHeight;
canvas.drawText(b[i], xPos, yPos, paint);
paint.reset();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y/getHeight()*b.length);
switch (action) {
case MotionEvent.ACTION_DOWN:
showBkg = true;
if(oldChoose != c && listener != null){
if(c > 0 && c< b.length){
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_MOVE:
if(oldChoose != c && listener != null){
if(c > 0 && c< b.length){
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
showBkg = false;
choose = -1;
invalidate();
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
public interface OnTouchingLetterChangedListener{
public void onTouchingLetterChanged(String s);
}
}
然后我在Activity中OnTouchingLetterChangedListener中監(jiān)聽手指觸摸到了哪個字母,然后讓列表跳轉(zhuǎn)到對應(yīng)的位置,
彈出首字母提示框:
private class LetterListViewListener implements OnTouchingLetterChangedListener{
@Override
public void onTouchingLetterChanged(final String s) {
if(alphaIndexer.get(s) != null) {
int position = alphaIndexer.get(s);
personList.setSelection(position);
overlay.setText(sections[position]);
overlay.setVisibility(View.VISIBLE);
handler.removeCallbacks(overlayThread);
//延遲一點五秒后執(zhí)行,讓overlay為不可見
handler.postDelayed(overlayThread, 1500);
}
}
}
延遲一秒讓彈出的首字母提示框變?yōu)椴豢梢?,也就是那個首字母提示框只會顯示一秒鐘的時間:
//設(shè)置overlay不可見
private class OverlayThread implements Runnable {
@Override
public void run() {
overlay.setVisibility(View.GONE);
}
}
還有關(guān)于解析漢子的首字母拼音的問題,我這里是查的系統(tǒng)數(shù)據(jù)庫,里面正好有sort_key這一列,比如名字是張三,那么他對應(yīng)的sort_key就是:ZHANG張SAN三,這樣一來就容易多了:
//獲得漢語拼音首字母
private String getAlpha(String str) {
if (str == null) {
return "#";
}
if (str.trim().length() == 0) {
return "#";
}
char c = str.trim().substring(0, 1).charAt(0);
// 正則表達式,判斷首字母是否是英文字母
Pattern pattern = Pattern.compile("^[A-Za-z]+{1}quot;);
if (pattern.matcher(c + "").matches()) {
return (c + "").toUpperCase();
} else {
return "#";
}
}
如果你的數(shù)據(jù)不是從聯(lián)系人表中查的,那可以使用第三方j(luò)ar包,就是pinyin4j-2.5.0。
activity代碼和布局文件比較長,我就不在這里貼了。
附:demo源碼點擊此處本站下載。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android來電監(jiān)聽和去電監(jiān)聽實現(xiàn)代碼
本文是關(guān)于來點監(jiān)聽和去電監(jiān)聽展開問題,通過實例代碼講解,對android來電監(jiān)聽和去電監(jiān)聽的相關(guān)知識感興趣的朋友一起看看吧2017-06-06
Android Studio報:“Attribute application@theme or @ icon ”問題的解
這篇文章主要給大家介紹了關(guān)于Android Studio報:“Attribute application@theme or @ icon ”問題的解決方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Android編程使用加速度傳感器實現(xiàn)搖一搖功能及優(yōu)化的方法詳解
這篇文章主要介紹了Android編程使用加速度傳感器實現(xiàn)搖一搖功能及優(yōu)化的方法,結(jié)合實例形式分析了Android傳感器的調(diào)用方法、參數(shù)含義及具體使用技巧,需要的朋友可以參考下2017-08-08
Kotlin協(xié)程flowOn與線程切換超詳細示例介紹
這篇文章主要介紹了Kotlin協(xié)程flowOn與線程切換,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
Android RecyclerView上拉加載更多功能回彈實現(xiàn)代碼
這篇文章主要介紹了Android RecyclerView上拉加載更多功能回彈實現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02

