快速索引是众多app中常用的功能,在及时通讯、用户列表等功能中能够快速定位,在此将我项目中使用到的索引抽取出来交流分享,效果如下:
一、绘制IndexBar
先自定义IndexBar继承View;
public class IndexBar extends View {
public IndexBar(Context context) {
super(context);
}
public IndexBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public IndexBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
定义所需的属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="IndexrecyclerviewIndexBar">
<!--index文字大小-->
<attr name="indexTextSize" format="dimension"></attr>
<!--index按下的颜色-->
<attr name="pressBackground" format="color|reference"></attr>
<!--index按下时文字的颜色-->
<attr name="pressTextColor" format="color|reference"></attr>
<!--index文字的颜色-->
<attr name="indexTextColor" format="color|reference"></attr>
<!--index文字选中的颜色-->
<attr name="selectTextColor" format="color|reference"></attr>
</declare-styleable>
</resources>
修改完善IndexBar;
public class IndexBar extends View {
private Context mContext;
/**
* 默认索引
*/
private static final String[] DEFAULT_INDEX = 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", "#"};
/**
* 每个index的高度
*/
private int indexHeght;
/**
* view宽度
*/
private int mWidth;
/**
* view高度
*/
private int mHeight;
/**
* 画笔
*/
private Paint mPaint;
/**
* 按下时的背景颜色
*/
private int mPressBackground;
/**
* 文字颜色
*/
private int mTextColor;
/**
* 按下时文字的颜色
*/
private int mPressTextColor;
/**
* 文字选中的颜色
*/
private int mSelectTextColor;
/**
* 字体大小
*/
private int textSize;
private int DEFAULT_PRESS_COLOR = Color.GRAY;
private int DEFAULT_BACKGROUND = Color.TRANSPARENT;
List<String> indexDatas;
public IndexBar(Context context) {
super(context);
init(context, null, -1);
}
public IndexBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs, -1);
}
public IndexBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
this.mContext = context;
//默认的TextSize
int DEFAULT_SIZE = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics());
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IndexrecyclerviewIndexBar);
if (typedArray != null) {
textSize = typedArray.getDimensionPixelSize(R.styleable.IndexrecyclerviewIndexBar_indexTextSize, DEFAULT_SIZE);
mPressBackground = typedArray.getColor(R.styleable.IndexrecyclerviewIndexBar_pressBackground, DEFAULT_BACKGROUND);
mTextColor = typedArray.getColor(R.styleable.IndexrecyclerviewIndexBar_indexTextColor, DEFAULT_PRESS_COLOR);
mPressTextColor = typedArray.getColor(R.styleable.IndexrecyclerviewIndexBar_pressTextColor, mTextColor);
mSelectTextColor = typedArray.getColor(R.styleable.IndexrecyclerviewIndexBar_selectTextColor, mTextColor);
}
initPaint();
initDatas();
}
private void initDatas() {
indexDatas = Arrays.asList(DEFAULT_INDEX);
}
private void initPaint() {
mPaint = new Paint();
mPaint.setTextSize(textSize);
mPaint.setAntiAlias(true);
}
boolean isPress;
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isPre