本demo技术要点
效果图
![效果图](https://img-blog.csdn.net/20170710134015979?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQW5keV9sMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
快速索引控件.
package cn.evun.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import cn.evun.javebean.Cheeses;
/**
* 自定义快速索引
*/
public class QuickIndexBar extends View {
private Paint paint;
private float cellWidth;
private float cellHeight;
public QuickIndexBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.GRAY);
paint.setTextSize(30);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < Cheeses.LETTERS.length; i++) {
String letter = Cheeses.LETTERS[i];
Rect rect = new Rect();
paint.getTextBounds(letter, 0, 1, rect);
int textWidth = rect.width();
int textHeight = rect.height();
float x = cellWidth * 0.5f - textWidth * 0.5f;
float y = cellHeight * 0.5f + textHeight * 0.5f + i * cellHeight;
canvas.drawText(letter, x, y, paint);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
cellWidth = this.getMeasuredWidth();
cellHeight = this.getMeasuredHeight() * 1.0f / Cheeses.LETTERS.length;
}
private int currentIndex = -1;
private int lastIndex = -1;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (listener != null) {
lastIndex = currentIndex;
float y = event.getY();
currentIndex = (int) (y / cellHeight);
if (currentIndex > Cheeses.LETTERS.length - 1) {
currentIndex = Cheeses.LETTERS.length - 1;
} else if (currentIndex < 0) {
currentIndex = 0;
}
if (lastIndex != currentIndex) {
String letter = Cheeses.LETTERS[currentIndex];
listener.onLetterChanged(letter);
}
}
break;
case MotionEvent.ACTION_UP:
if(listener!=null){
listener.onLetterDismiss();
}
break;
}
return true;
}
private OnLetterChangedListener listener;
public interface OnLetterChangedListener {
void onLetterChanged(String letter);
void onLetterDismiss();
}
public void setOnLetterChangedListener(OnLetterChangedListener listener) {
this.listener = listener;
}
}
package cn.evun.view;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
*/
public class NoScrollViewPager extends ViewPager {
private boolean canScroll =true;
public NoScrollViewPager(Context context) {
super(context);
}
public NoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return this.canScroll&&super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return this.canScroll&&super.onInterceptTouchEvent(ev);
}
public void isCanScroll(boolean b){
this.canScroll =b;
}
}
源码传送
CSDN下载
Github源码