Android開發(fā)實現(xiàn)根據(jù)字母快速定位側(cè)邊欄
按首字母對List排列,并根據(jù)首字母快速定位的實現(xiàn),在Android開發(fā)中被大量應(yīng)用,今天我也來親自實現(xiàn)一下,將這個控件封裝起來,也方便以后的使用。大體上可以分為兩步來實現(xiàn)這個控件:首先使自己的控件繼承于View,并進行圖形繪制;然后根據(jù)觸摸位置計算當前觸摸的字母,并實現(xiàn)回調(diào)接口的方法。
下面來進行實踐:
1.創(chuàng)建自己的控件類并繼承于View,注意:不能只聲明含有一個構(gòu)造參數(shù)Context的構(gòu)造函數(shù),這樣我們的控件無法在xml文件中調(diào)用,因為Android中xml調(diào)用控件之間的參數(shù)傳遞是通過構(gòu)造參數(shù)中的AttributeSet參數(shù)來進行的,沒有這個參數(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ù)總的高度除以字符串數(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進行相關(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變量表示當前觸摸的字符位置,若沒有觸摸則為-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)接口,當我們觸摸的字符發(fā)生改變時可以執(zhí)行該回調(diào)接口的方法
public interface OnTouchLetterChangedListener {
public void touchLetterChanged(String s);
}
2)當發(fā)生按下事件或移動事件時,我們根據(jù)觸摸點的位置計算出當前觸摸的字符,如果和我們顯示的字符不相同則執(zhí)行回調(diào)接口的方法,并進行View的重繪;當發(fā)生抬起事件時我們將當前顯示的字符更新為-1,表示當前沒有字符顯示,并進行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進行相關(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變量表示當前顯示的字符位置,若沒有觸摸則為-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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio 3.0 gradle提示版本太老
這篇文章主要介紹了Android Studio 3.0 gradle提示版本太老的配置和解決方法。2017-11-11
Android中的應(yīng)用認領(lǐng)總結(jié)
這篇文章主要介紹了Android中的應(yīng)用認領(lǐng)總結(jié),本文講解了如何認領(lǐng)、對未簽名包簽名、需要替換的簽名值、驗證簽名等內(nèi)容,需要的朋友可以參考下2015-01-01
Android實現(xiàn)觸發(fā)html頁面的Button控件點擊事件方式
這篇文章主要介紹了Android實現(xiàn)觸發(fā)html頁面的Button控件點擊事件方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Flutter通過Container實現(xiàn)時間軸效果
時間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過Container實現(xiàn),感興趣的朋友可以了解下2021-05-05
Android相冊效果(使用C#和Java分別實現(xiàn))
這篇文章主要介紹了Android相冊效果(使用C#和Java分別實現(xiàn)),原來C#也可以開發(fā)APP,小編第一次見了~感覺不錯,因為小編暫時不喜歡Java,所以,需要的朋友可以參考下2015-06-06
Android編程實現(xiàn)ActionBar的home圖標動畫切換效果
這篇文章主要介紹了Android編程實現(xiàn)ActionBar的home圖標動畫切換效果,涉及Android布局、樣式、Activity及菜單相關(guān)操作技巧,需要的朋友可以參考下2017-01-01

