您好,登錄后才能下訂單哦!
本文實例講述了Android開發之自定義view實現通訊錄列表A~Z字母提示效果。分享給大家供大家參考,具體如下:
開發工具:eclipse
運行環境: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中監聽手指觸摸到了哪個字母,然后讓列表跳轉到對應的位置,
彈出首字母提示框:
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); //延遲一點五秒后執行,讓overlay為不可見 handler.postDelayed(overlayThread, 1500); } } }
延遲一秒讓彈出的首字母提示框變為不可見,也就是那個首字母提示框只會顯示一秒鐘的時間:
//設置overlay不可見 private class OverlayThread implements Runnable { @Override public void run() { overlay.setVisibility(View.GONE); } }
還有關于解析漢子的首字母拼音的問題,我這里是查的系統數據庫,里面正好有sort_key這一列,比如名字是張三,那么他對應的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 "#"; } }
如果你的數據不是從聯系人表中查的,那可以使用第三方jar包,就是pinyin4j-2.5.0。
activity代碼和布局文件比較長,我就不在這里貼了。
附:demo源碼點擊此處本站下載。
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android布局layout技巧總結》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android操作json格式數據技巧總結》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。