Android實現仿通訊錄側邊欄滑動SiderBar效果代碼_Android

來源:互聯網
上載者:User

本文執行個體講述了Android實現仿通訊錄側邊欄滑動SiderBar效果代碼。分享給大家供大家參考,具體如下:

之前看到某些應用的側邊欄做得不錯,想想自己也弄一個出來,現在分享出來,當然裡面還有不足的地方,請大家多多包涵。

先上圖:

具體實現的代碼如下:

package com.freesonfish.listview_index; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class MySideBar extends View {   private OnTouchingLetterChangedListener touchListener;   // 26個字母   public static 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" };   private boolean showBkg = false;   int choose = -1;   int scrollChoose = -1;   Paint paint = new Paint();   Paint rectPaint = new Paint();   float rectWidth = 0f;   public MySideBar(Context context, AttributeSet attrs, int defStyle) {     super(context, attrs, defStyle);     init();   }   public MySideBar(Context context, AttributeSet attrs) {     super(context, attrs);     init();   }   public MySideBar(Context context) {     super(context);     init();   }   private void init() {     rectPaint.setColor(Color.parseColor("#CCCCCC"));     rectWidth = paint.measureText("#");   }   /**    * 重寫這個方法    */   protected void onDraw(Canvas canvas) {     super.onDraw(canvas);     if (showBkg) {       canvas.drawColor(Color.parseColor("#CCCCCC"));     }     final int height = getHeight();     final int width = getWidth();     final int singleHeight = height / b.length;     final float xRectPos = ((float) width - paint.measureText("#")) / 2.0f - rectWidth;     final float xRectPos2 = xRectPos + 3.0f * rectWidth;     for (int i = 0; i < b.length; i++) {       paint.setFakeBoldText(true);       paint.setAntiAlias(true);       final float xPos = ((float) width - paint.measureText(b[i])) / 2.0f;      final float yPos = singleHeight * i + singleHeight;       if (i == choose) {         paint.setColor(Color.RED);         canvas.drawRect(xRectPos, yPos - singleHeight / 2.0f, xRectPos2, yPos + rectWidth, rectPaint);       }       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 c = (int) (y / getHeight() * b.length);     switch (action) {     case MotionEvent.ACTION_DOWN:       showBkg = true;       if (choose != c && touchListener != null) {         doOnActionDown(c);       }       break;     case MotionEvent.ACTION_MOVE:       if (choose != c && touchListener != null) {         doOnActionDown(c);       }       break;     case MotionEvent.ACTION_UP:       showBkg = false;       invalidate();       break;     }     return true;   }   /**    * listview滾動時候調用它    *    * @param c    */   public void setColorWhenListViewScrolling(int c) {     if (scrollChoose != c) {       scrollChoose = c;       String string = ListContantsUtil.AbcList.get(c);       for (int i = c; i < b.length; ++i) {         if (string.equals(b[i])) {           choose = i;           invalidate();           break;         }       }     }   }   /**    * 當側邊欄被按下的動作    * @param c    */   private void doOnActionDown(int c) {     if (c > 0 && c < b.length) {       if (ListContantsUtil.indexPositionMap.containsKey(b[c])) {         touchListener.onTouchingLetterChanged(b[c]);         choose = c;         invalidate();       } else {         c = c - 1;         doOnActionDown(c);       }     }   }   @Override   public boolean onTouchEvent(MotionEvent event) {     return super.onTouchEvent(event);   }   public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener touchListener) {     this.touchListener = touchListener;   }   /**    * 用來通知activity顯示選中的字母    * @author freeson    *    */   public interface OnTouchingLetterChangedListener {     public void onTouchingLetterChanged(String s);   } }

然後ListContantsUtil類是儲存通訊錄名字的拼音等的類,具體也如下:

package com.freesonfish.listview_index; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class ListContantsUtil {   static final List<Integer> indexPositionList = new ArrayList<Integer>();   static final List<String> AbcList = new ArrayList<String>();   static final HashMap<String, Integer> indexPositionMap = new HashMap<String, Integer>();   static void putNameIndexToMap(List<String> list, HashMap<String, String> nameAndPinyin) {     int lenght = list.size();     for (int i = 0; i < lenght; ++i) {       String name = nameAndPinyin.get(list.get(i)).substring(0, 1);       // 判斷該字元是屬於字母還是數字或其他的       int ascii = name.toCharArray()[0];       if (ascii >= 65 && ascii <= 90) {         if (!indexPositionMap.containsKey(name)) {           indexPositionMap.put(name, i);           AbcList.add(name);           indexPositionList.add(i);         }       } else {         if (!indexPositionMap.containsKey("#")) {           indexPositionMap.put("#", i);           AbcList.add("#");           indexPositionList.add(i);         }       }     }   } }

注意,上面的程式還是有些小問題的,請大家注意最佳化解決。

完整執行個體代碼代碼點擊此處本站下載。

希望本文所述對大家Android程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.