RecyclerView自定义目录快速索引

快速索引是众多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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值