Android開發(fā)實現(xiàn)根據(jù)字母快速定位側(cè)邊欄
按首字母對List排列,并根據(jù)首字母快速定位的實現(xiàn),在Android開發(fā)中被大量應(yīng)用,今天我也來親自實現(xiàn)一下,將這個控件封裝起來,也方便以后的使用。大體上可以分為兩步來實現(xiàn)這個控件:首先使自己的控件繼承于View,并進(jìn)行圖形繪制;然后根據(jù)觸摸位置計算當(dāng)前觸摸的字母,并實現(xiàn)回調(diào)接口的方法。
下面來進(jìn)行實踐:
1.創(chuàng)建自己的控件類并繼承于View,注意:不能只聲明含有一個構(gòu)造參數(shù)Context的構(gòu)造函數(shù),這樣我們的控件無法在xml文件中調(diào)用,因為Android中xml調(diào)用控件之間的參數(shù)傳遞是通過構(gòu)造參數(shù)中的AttributeSet參數(shù)來進(jìn)行的,沒有這個參數(shù)我們的控件不能在xml中使用。在這里我添加了父類View的三個構(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方法來實現(xiàn),并通過Paint來來繪制
1)首先聲明一個成員變量來保存我們的字符數(shù)組,并初始化一個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ù)組的長度來得到每一個字符的高度,然后循環(huán)遍歷整個數(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);
}
//計算字符的繪制的位置
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方法來實現(xiàn)
1)首先我們要設(shè)計一個回調(diào)接口,當(dāng)我們觸摸的字符發(fā)生改變時可以執(zhí)行該回調(diào)接口的方法
public interface OnTouchLetterChangedListener {
public void touchLetterChanged(String s);
}
2)當(dāng)發(fā)生按下事件或移動事件時,我們根據(jù)觸摸點的位置計算出當(dāng)前觸摸的字符,如果和我們顯示的字符不相同則執(zhí)行回調(diào)接口的方法,并進(jìn)行View的重繪;當(dāng)發(fā)生抬起事件時我們將當(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);
}
// 計算字符的繪制的位置
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 shell命令行中過濾adb logcat輸出的幾種方法
本文主要介紹Android shell命令行中過濾adb logcat輸出的方法,這里整理了幾種方法,并詳細(xì)的說明,有需要的朋友可以參考下2016-08-08
Android實現(xiàn)獲取應(yīng)用程序相關(guān)信息列表的方法
這篇文章主要介紹了Android實現(xiàn)獲取應(yīng)用程序相關(guān)信息列表的方法,是應(yīng)用管理器常用的功能,需要的朋友可以參考下2014-07-07
Android 8.0系統(tǒng)中應(yīng)用圖標(biāo)的適配微技巧
這篇文章主要介紹了Android 8.0系統(tǒng)中應(yīng)用圖標(biāo)的適配微技巧 ,需要的朋友可以參考下2018-04-04
Android使用Messenger實現(xiàn)service與activity交互
這篇文章主要介紹了android使用Messenger實現(xiàn)service與activity交互的相關(guān)資料,需要的朋友可以參考下2016-06-06
Android來電監(jiān)聽和去電監(jiān)聽實現(xiàn)代碼
本文是關(guān)于來點監(jiān)聽和去電監(jiān)聽展開問題,通過實例代碼講解,對android來電監(jiān)聽和去電監(jiān)聽的相關(guān)知識感興趣的朋友一起看看吧2017-06-06
Android在kts中使用navigation及Args的方法
在Android項目中使用Kotlin腳本(kts)替代Groovy配置navigation和Args,需添加相關(guān)依賴,并在build.gradle中進(jìn)行配置,文章詳細(xì)介紹了如何在kts中使用navigation進(jìn)行頁面導(dǎo)航和參數(shù)傳遞,介紹了使用Bundle和Safe Args兩種方式安全傳遞參數(shù)2024-10-10

