主要步驟 |
1.橫向繪制出26個字母和# |
2.事件滑動獲取對應(yīng)的字母 |
3.監(jiān)聽回調(diào)出對應(yīng)字母 |
public class SideBar extends View{
private static final String TAG = "SideBar";
private static String minue="A";
/**
* 觸摸事件
*/
private ITouchingLetterChangedListener onTouchingLetterChangedListener;
/**
* 側(cè)邊欄顯示字母
*/
private String[] words = { "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 int chooseup=-1;
/**
* 相應(yīng)的畫筆
*/
private Paint paint;
/**
* 構(gòu)造函數(shù) 數(shù)據(jù)初始化
* @param context 上下文對象
* @param attrs 屬性列表
* @param defStyleAttr 默認(rèn)樣式
*/
public SideBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* 初始化相應(yīng)的數(shù)據(jù)
*/
private void init() {
paint = new Paint();
}
/**
* 構(gòu)造函數(shù) 數(shù)據(jù)初始化
* @param context 上下文對象
* @param attrs 屬性列表
*/
public SideBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* 構(gòu)造函數(shù) 數(shù)據(jù)初始化
* @param context 上下文對象
*/
public SideBar(Context context) {
this(context, null);
}
/**
* 繪制列表控件的方法
* 將要繪制的字母以從上到下的順序繪制在一個指定區(qū)域
* 如果是進行選中的字母就進行高亮顯示
*/
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int height = getHeight();// 獲取對應(yīng)高度
int width = getWidth(); // 獲取對應(yīng)寬度
int singleWidth = width / words.length;// 獲取每一個字母的寬度
for (int i = 0; i < words.length; i++) {
paint.setColor(getResources().getColor(R.color.indexadc));
// paint.setColor(Color.WHITE);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setTextSize(20f);
// 選中的狀態(tài)
if (i == choose) {
chooseup=choose;
paint.setColor(getResources().getColor(R.color.indexabc));
paint.setFakeBoldText(true);
}
// x坐標(biāo)等于中間-字符串寬度的一半.
//float yPos = height / 2.0f - paint.measureText(words[i]) / 2.0f;
float yPos = height / 2.0f - paint.measureText(words[0]) / 2.0f;
float xPos = singleWidth * i + singleWidth/2;
if (words[i]=="I") { //解決當(dāng)繪制字母I時會左右距離不同問題
xPos = singleWidth * i + singleWidth/2+paint.measureText(words[0]) / 2.0f;
}
canvas.drawText(words[i], xPos, yPos, paint);
paint.reset();// 重置畫筆
}
}
boolean isup=false;
/**
* 處理觸摸事件的方法
* 用戶按下時候,整個控件背景變化
* 根據(jù)按下x坐標(biāo) 判斷究竟用戶按下那個字母
* 當(dāng)前字母高亮顯示 將其字母顯示屏幕中央
*/
@SuppressWarnings("deprecation")
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
final float x = event.getX();// 點擊x坐標(biāo)
final int oldChoose = choose;
final ITouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (x / getWidth() * words.length);// 點擊x坐標(biāo)所占總寬度的比例*b數(shù)組的長度就等于點擊b中的個數(shù).
switch (action) {
case MotionEvent.ACTION_UP:
isup=true;
FragmentContacts.hideADCtip();
setBackgroundDrawable(new ColorDrawable(0x00000000));
choose = -1;//
invalidate();
break;
default:
isup=false;
//setBackgroundResource(R.drawable.sidebar_background);
if (oldChoose != c) {
if (c >= 0 && c < words.length) {
if (listener != null) {
listener.OnTouchingLetterChanged(words[c]);
}
choose = c;
invalidate();
}
}
break;
}
return true;
}
//listView滑動可見第一個字母
public void setPosition(String position){
Log.i(TAG, "position"+position);
//isshow=true;
minue=position;
}
/**
* 接口
*/
public interface ITouchingLetterChangedListener {
void OnTouchingLetterChanged(String cString);
}
/**
* 字母改變的監(jiān)聽器
* @return 獲取字母改變的監(jiān)聽器
*/
public ITouchingLetterChangedListener getOnTouchingLetterChangedListener() {
return onTouchingLetterChangedListener;
}
/**
* 設(shè)置改變的監(jiān)聽器
* @param onTouchingLetterChangedListener 設(shè)置字母改變的監(jiān)聽器
*/
public void setOnTouchingLetterChangedListener(
ITouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者