首先想要做出快速检索的效果主要的是思路
1.快速检索是每个字母都能点 而且是竖向排序 所以我们首先想到的做法 就是listview 但是用系统的listview是实现不了我们的效果的 所以实现这个功能是自定义的listview。
创建一个类继承listview
重写view的三个构造方法
一个参数的构造方法在new 对象时候调用
二个参数构造方法是在布局中声明此控件的时候调用
三个参数构造方法是在布局中声明并且用到style属性时调用
完成之后我们就该进行下一步操作了
在init()中创建画笔
重写onFinishInflate()、onSizeChanged()、onLayout()、onMeasure()、onDraw()、onTouchEvent()方法。用于测量、绘制、监听手势等功能
具体代码如下:
@Override
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
for (int i =0; i
int x =width /2;
float y =callHeight/2+getTextHeight(list[i])/2+i*callHeight;
canvas.drawText(list[i],x,y,paint);
}
super.onDraw(canvas);
}
private float getTextHeight(String s) {
Rect rect =new Rect();
paint.getTextBounds(s,0,1,rect);
return rect.height();
}
private int mIndex=-1;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
float y = event.getY();
int index = (int) (y /callHeight);
if(mIndex!=index){
String s =list[index];
if(getData!=null){
getData.getData(s);
}
}
mIndex=index;
invalidate();
break;
case MotionEvent.ACTION_UP:
mIndex=-1;
break;
}
return super.onTouchEvent(event);
}
public interface GetData{
void getData(String s);
}
其中getdata接口为回掉接口用于回掉手势事件
好一个简单的自定义view快速检索就实现了!