android 快速導航條

http://blog.csdn.net/qq_30379689/article/details/52684312

鏈接:http://pan.baidu.com/s/1nvwYZLB 密碼:wn0s

1、首先我們封裝一個QuickIndexBar 繼承View:

public class QuickIndexBar extends View {

    private Paint mPaint;
    private float mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, getResources().getDisplayMetrics());
    private static final String[] LETTERS = new String[]{
        "↑", "☆", "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 int mCellWidth;
    private float mCellHeight;
    private int mTouchIndex = -1;//用于記錄當前觸摸的索引值

    //暴露一個字母的監(jiān)聽
    public interface OnLetterUpdateListener {
        void onLetterUpdate(String letter);

        void onLetterCancel();
    }

    private OnLetterUpdateListener mListener;

    public OnLetterUpdateListener getOnLetterUpdateListener() {
        return mListener;
    }

    public void setOnLetterUpdateListener(OnLetterUpdateListener listener) {
        mListener = listener;
    }

    public QuickIndexBar(Context context) {
        this(context, null);
    }

    public QuickIndexBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QuickIndexBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(getResources().getColor(R.color.side_bar));
        mPaint.setTextSize(mTextSize);
        //setBackgroundColor(Color.WHITE);
        //mPaint.setTypeface(Typeface.DEFAULT_BOLD);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        //獲取單元格的寬度和高度
        mCellWidth = getMeasuredWidth();
        mCellHeight = getMeasuredHeight() * 1.0f / LETTERS.length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setBackgroundColor(Color.TRANSPARENT);

        for (int i = 0; i < LETTERS.length; i++) {
            String text = LETTERS[i];
            //計算坐標
            //x坐標為單元格寬度的一半 減去 文字寬度的一半
            int x = (int) (mCellWidth / 2.0f - mPaint.measureText(text) / 2.0f);

            Rect bounds = new Rect();
            mPaint.getTextBounds(text, 0, text.length(), bounds);
            //文本的高度
            int textHeight = bounds.height();

            //y坐標為單元格高度的一半 + 文字高度的一半 + 上面有多少個單元格的高度(index * mCellHeight)
            int y = (int) (mCellHeight / 2.0f + textHeight / 2.0f + i * mCellHeight);

            //mPaint.setColor(mTouchIndex == i ? Color.GRAY : Color.WHITE);//被選擇到的字母變成灰色
            //繪制文本A-Z,此處的x,y坐標是字母左上方的坐標
            canvas.drawText(text, x, y, mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = -1;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                index = (int) (event.getY() / mCellHeight);//   y值/每個單元格的高度 = 當前單元格的索引
                if (index >= 0 && index < LETTERS.length) {
                    if (index != mTouchIndex) {
                        if (mListener != null) {
                            mListener.onLetterUpdate(LETTERS[index]);
                            mTouchIndex = index;
                        }
                    }
                }
                setBackgroundColor(getResources().getColor(R.color.side_bar_pressed));
                break;
            case MotionEvent.ACTION_MOVE:
                index = (int) (event.getY() / mCellHeight);
                if (index >= 0 && index < LETTERS.length) {
                    if (index != mTouchIndex) {
                        if (mListener != null) {
                            mListener.onLetterUpdate(LETTERS[index]);
                            mTouchIndex = index;
                        }
                    }
                }
                setBackgroundColor(getResources().getColor(R.color.side_bar_pressed));
                break;
            case MotionEvent.ACTION_UP:
                mTouchIndex = -1;
                if (mListener != null) {
                    mListener.onLetterCancel();
                }
                setBackgroundColor(Color.TRANSPARENT);
                break;
        }
//        invalidate();//重新調(diào)用onDraw方法實現(xiàn)選中的字母更改顏色
        return true;
    }
}

2、在colors.xml中添加

<!--遮蓋層顏色-->
<color name="mask">#32000000</color>

<!--快速索引bar顏色-->
<color name="side_bar">#919091</color>
<color name="side_bar_pressed">#BFBFBF</color>

3、在layout加入試圖

     <!--快速導航條-->
    <com.lqr.wechat.widget.QuickIndexBar
        android:id="@+id/qib"
        android:layout_width="40px"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_below="@id/llTop" />

4、在類中調(diào)用

mQib.setOnLetterUpdateListener(new QuickIndexBar.OnLetterUpdateListener() {
            @Override
            public void onLetterUpdate(String letter) {
                //滑動到第一個對應字母開頭的聯(lián)系人
                if ("↑".equalsIgnoreCase(letter)) {
                   //跳轉(zhuǎn)到首行
                } else if ("☆".equalsIgnoreCase(letter)) {
                    //跳轉(zhuǎn)到首行
                } else {
                    //跳轉(zhuǎn)到指定的行
                }
            }

            @Override
            public void onLetterCancel() {
                //隱藏對話框
                hideLetter();
            }
        });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,558評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,257評論 4 61
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,554評論 0 17
  • 文/居里社 星幣首牌 主色系:白,黃色,藍色,綠色 牌面物質(zhì):手,星幣,花園,鮮花,遠山 場景描述及隱藏含義:一只...
    居里葉閱讀 1,989評論 0 8
  • Some feel the rain. Others just get wet.--Roger Miller 周日...
    Alpha_Omega閱讀 172評論 0 1