实现的效果图:
通过自定义View,然后在xml文件中引用可以达到需求。
public SiderBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
private String[] sections = 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 void init(){
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.DKGRAY);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(DensityUtil.sp2px(context, 10));
}
首先使用带有两个参数的构造器,然后初始化数组和初始化画笔。
在onDraw方法中,“写出”每个字母的值:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float center = getWidth() / 2;
height = getHeight() / sections.length;
for (int i = sections.length - 1; i > -1; i--) {
canvas.drawText(sections[i], center, height * (i+1), paint);
}
}
宽度是这个View的宽度的一般,每个高度是View的总高度除以数组的长度(每个字母的高度就确定了),这样就“写上”了每个字母。
接下来的sectionForPoint实现每个数组元素的索引,如下:
private int sectionForPoint(float y) {
LogUtil.d(TAG, "y :" + y + "");
LogUtil.d(TAG, "height :" + height + "");
int index = (int) (y / height);
LogUtil.d(TAG, "index :" + index + "");
if(index < 0) {
index = 0;
}
if(index > sections.length - 1){
LogUtil.d(TAG, "sections.length - 1:" + (sections.length - 1) + "");
index = sections.length - 1;
}
return index;
}
整个View的高度不变:
需要考虑下边界的问题。
接着在onTouchEvent处理响应的四个事件,按下,抬起,滑动,取消。
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:{
if(header == null){
header = (TextView) ((View)getParent()).findViewById(R.id.floating_header);
}
setHeaderTextAndscroll(event);
header.setVisibility(View.VISIBLE);
setBackgroundResource(R.drawable.sidebar_background_pressed);
return true;
}
case MotionEvent.ACTION_MOVE:{
setHeaderTextAndscroll(event);
return true;
}
case MotionEvent.ACTION_UP:
header.setVisibility(View.INVISIBLE);
setBackgroundColor(Color.TRANSPARENT);
return true;
case MotionEvent.ACTION_CANCEL:
header.setVisibility(View.INVISIBLE);
setBackgroundColor(Color.TRANSPARENT);
return true;
}
return super.onTouchEvent(event);
}
private void setHeaderTextAndscroll(MotionEvent event){
if (mListView == null) {
//check the mListView to avoid NPE. but the mListView shouldn't be null
//need to check the call stack later
return;
}
String headerString = sections[sectionForPoint(event.getY())];
LogUtil.d(TAG, " event.getY():" + event.getY());
header.setText(headerString);
ContactAdapter adapter = (ContactAdapter) mListView.getAdapter();
String[] adapterSections = (String[]) adapter.getSections();
try {
for (int i = adapterSections.length - 1; i > -1; i--) {
if(adapterSections[i].equals(headerString)){
mListView.setSelection(adapter.getPositionForSection(i));
break;
}
}
} catch (Exception e) {
LogUtil.d(TAG, e.getMessage());
}
}