Android開發(fā)實(shí)現(xiàn)根據(jù)字母快速定位側(cè)邊欄
按首字母對List排列,并根據(jù)首字母快速定位的實(shí)現(xiàn),在Android開發(fā)中被大量應(yīng)用,今天我也來親自實(shí)現(xiàn)一下,將這個(gè)控件封裝起來,也方便以后的使用。大體上可以分為兩步來實(shí)現(xiàn)這個(gè)控件:首先使自己的控件繼承于View,并進(jìn)行圖形繪制;然后根據(jù)觸摸位置計(jì)算當(dāng)前觸摸的字母,并實(shí)現(xiàn)回調(diào)接口的方法。
下面來進(jìn)行實(shí)踐:
1.創(chuàng)建自己的控件類并繼承于View,注意:不能只聲明含有一個(gè)構(gòu)造參數(shù)Context的構(gòu)造函數(shù),這樣我們的控件無法在xml文件中調(diào)用,因?yàn)锳ndroid中xml調(diào)用控件之間的參數(shù)傳遞是通過構(gòu)造參數(shù)中的AttributeSet參數(shù)來進(jìn)行的,沒有這個(gè)參數(shù)我們的控件不能在xml中使用。在這里我添加了父類View的三個(gè)構(gòu)造函數(shù),這里只需要調(diào)用父類的構(gòu)造函數(shù)即可,不需要額外的操作。
public QuicLocationBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub } public QuicLocationBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public QuicLocationBar(Context context) { super(context); // TODO Auto-generated constructor stub }
2.繪制字符:繪制的部分通過復(fù)寫父類的onDraw方法來實(shí)現(xiàn),并通過Paint來來繪制
1)首先聲明一個(gè)成員變量來保存我們的字符數(shù)組,并初始化一個(gè)Paint類的成員變量來幫助我們繪制字符
private String characters[] = { "#", "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 Paint paint = new Paint();
2)根據(jù)總的高度除以字符串?dāng)?shù)組的長度來得到每一個(gè)字符的高度,然后循環(huán)遍歷整個(gè)數(shù)組來繪制字符
@Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); int width = getWidth(); int height = getHeight(); int singleHeight = height / characters.length; for (int i = 0; i < characters.length; i++) { //對paint進(jìn)行相關(guān)的參數(shù)設(shè)置 paint.setColor(getResources().getColor(R.color.myblack)); paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setAntiAlias(true); paint.setTextSize(20); if (i == choose) {//choose變量表示當(dāng)前觸摸的字符位置,若沒有觸摸則為-1 paint.setColor(getResources().getColor(R.color.myred)); paint.setFakeBoldText(true); } //計(jì)算字符的繪制的位置 float xPos = width / 2 - paint.measureText(characters[i]) / 2; float yPos = singleHeight * i + singleHeight; //在畫布上繪制字符 canvas.drawText(characters[i], xPos, yPos, paint); paint.reset();//每次繪制完成后不要忘記重制Paint } }
注意:不要忘記在每次繪制完成后重置Paint
3.處理觸摸事件:通過復(fù)寫父類的dispatchTouchEvent方法來實(shí)現(xiàn)
1)首先我們要設(shè)計(jì)一個(gè)回調(diào)接口,當(dāng)我們觸摸的字符發(fā)生改變時(shí)可以執(zhí)行該回調(diào)接口的方法
public interface OnTouchLetterChangedListener { public void touchLetterChanged(String s); }
2)當(dāng)發(fā)生按下事件或移動(dòng)事件時(shí),我們根據(jù)觸摸點(diǎn)的位置計(jì)算出當(dāng)前觸摸的字符,如果和我們顯示的字符不相同則執(zhí)行回調(diào)接口的方法,并進(jìn)行View的重繪;當(dāng)發(fā)生抬起事件時(shí)我們將當(dāng)前顯示的字符更新為-1,表示當(dāng)前沒有字符顯示,并進(jìn)行View的重繪。
@Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); float y = event.getY(); int c = (int) (y / getHeight() * characters.length); switch (action) { case MotionEvent.ACTION_UP: choose = -1;// setBackgroundColor(0x0000); invalidate(); break; case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: setBackgroundColor(getResources().getColor(R.color.darkgray)); if (choose != c) { if (c >= 0 && c < characters.length) { if (mOnTouchLetterChangedListener != null) { mOnTouchLetterChangedListener .touchLetterChanged(characters[c]); } choose = c; invalidate(); } } break; } return true;//返回true表示觸摸事件不在向下分發(fā) }
附上整體源碼:
package com.example.test.widget; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; import com.example.gymapp.R; public class QuicLocationBar extends View { private String characters[] = { "#", "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 choose = -1; private Paint paint = new Paint(); private OnTouchLetterChangedListener mOnTouchLetterChangedListener; public QuicLocationBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub } public QuicLocationBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public QuicLocationBar(Context context) { super(context); // TODO Auto-generated constructor stub } public void setOnTouchLitterChangedListener( OnTouchLetterChangedListener onTouchLetterChangedListener) { this.mOnTouchLetterChangedListener = onTouchLetterChangedListener; } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); int width = getWidth(); int height = getHeight(); int singleHeight = height / characters.length; for (int i = 0; i < characters.length; i++) { // 對paint進(jìn)行相關(guān)的參數(shù)設(shè)置 paint.setColor(getResources().getColor(R.color.myblack)); paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setAntiAlias(true); paint.setTextSize(20); if (i == choose) {// choose變量表示當(dāng)前顯示的字符位置,若沒有觸摸則為-1 paint.setColor(getResources().getColor(R.color.myred)); paint.setFakeBoldText(true); } // 計(jì)算字符的繪制的位置 float xPos = width / 2 - paint.measureText(characters[i]) / 2; float yPos = singleHeight * i + singleHeight; // 在畫布上繪制字符 canvas.drawText(characters[i], xPos, yPos, paint); paint.reset();// 每次繪制完成后不要忘記重制Paint } } @Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); float y = event.getY(); int c = (int) (y / getHeight() * characters.length); switch (action) { case MotionEvent.ACTION_UP: choose = -1;// setBackgroundColor(0x0000); invalidate(); break; case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: setBackgroundColor(getResources().getColor(R.color.darkgray)); if (choose != c) { if (c >= 0 && c < characters.length) { if (mOnTouchLetterChangedListener != null) { mOnTouchLetterChangedListener .touchLetterChanged(characters[c]); } choose = c; invalidate(); } } break; } return true; } public interface OnTouchLetterChangedListener { public void touchLetterChanged(String s); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio 3.0 gradle提示版本太老
這篇文章主要介紹了Android Studio 3.0 gradle提示版本太老的配置和解決方法。2017-11-11Android中的應(yīng)用認(rèn)領(lǐng)總結(jié)
這篇文章主要介紹了Android中的應(yīng)用認(rèn)領(lǐng)總結(jié),本文講解了如何認(rèn)領(lǐng)、對未簽名包簽名、需要替換的簽名值、驗(yàn)證簽名等內(nèi)容,需要的朋友可以參考下2015-01-01Android實(shí)現(xiàn)觸發(fā)html頁面的Button控件點(diǎn)擊事件方式
這篇文章主要介紹了Android實(shí)現(xiàn)觸發(fā)html頁面的Button控件點(diǎn)擊事件方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Flutter通過Container實(shí)現(xiàn)時(shí)間軸效果
時(shí)間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過Container實(shí)現(xiàn),感興趣的朋友可以了解下2021-05-05Android相冊效果(使用C#和Java分別實(shí)現(xiàn))
這篇文章主要介紹了Android相冊效果(使用C#和Java分別實(shí)現(xiàn)),原來C#也可以開發(fā)APP,小編第一次見了~感覺不錯(cuò),因?yàn)樾【帟簳r(shí)不喜歡Java,所以,需要的朋友可以參考下2015-06-06Android編程實(shí)現(xiàn)ActionBar的home圖標(biāo)動(dòng)畫切換效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)ActionBar的home圖標(biāo)動(dòng)畫切換效果,涉及Android布局、樣式、Activity及菜單相關(guān)操作技巧,需要的朋友可以參考下2017-01-01