SideBar 可动态设置数据

网上很多SideBar都是写死的26个字母,不能根据名称来动态显示,自己改了一下,分享给大家参考:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SideBar extends View {
    // 触摸事件
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;

    // 26个字母
    private String[] b = {};
    private int choose = -1;// 选中
    private Paint paint = new Paint();

    private Context mContext;
    private TextView mTextDialog;

    private float centerY;
    private float itemHeight;
    private List<Float> listHeight = new ArrayList<>();

    /**
     * 为SideBar设置显示字母的TextView
     *
     * @param mTextDialog
     */
    public void setTextView(TextView mTextDialog) {
        this.mTextDialog = mTextDialog;
    }

    public SideBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        centerY = QMUIDisplayHelper.getScreenHeight(mContext) / 2;
        itemHeight = centerY / 27;
    }

    public SideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        centerY = QMUIDisplayHelper.getScreenHeight(mContext) / 2;
        itemHeight = centerY / 27;
    }

    public SideBar(Context context) {
        super(context);
        this.mContext = context;
        centerY = QMUIDisplayHelper.getScreenHeight(mContext) / 2;
        itemHeight = centerY / 27;
    }

    private int totalHeight = 0;
    private int textHeight = 0;

    /**
     * 重写这个方法
     */
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (b.length > 0) {
            if (textHeight <= 0) {
                Rect rect = new Rect();
                paint.getTextBounds(b[0], 0, b[0].length(), rect);
                textHeight = rect.height();
            }
            int width = getWidth(); // 获取对应宽度
            int center = b.length / 2;

            setCenterText(canvas, center, width, centerY);
            if (center > 0) {
                for (int i = center - 1; i >= 0; i--) {
                    setCenterText(canvas, i, width, centerY - (center - i) * 20 - itemHeight * (center - i));
                }
                for (int i = center + 1; i < b.length; i++) {
                    setCenterText(canvas, i, width, centerY + (i - center) * 20 + itemHeight * (i - center));
                }
            }
            Collections.sort(listHeight);
            totalHeight = (int) ((listHeight.get(listHeight.size() - 1)) - listHeight.get(0));
        }
    }

    private void setCenterText(Canvas canvas, int i, int width, float yPos2) {
        paint.setColor(Color.parseColor("#666666"));
        paint.setTypeface(Typeface.DEFAULT);
        paint.setAntiAlias(true);
        paint.setTextSize(30);
        // 选中的状态
        if (i == choose) {
            paint.setColor(Color.parseColor("#fc280c"));
            paint.setFakeBoldText(true);
        }
        // x坐标等于中间-字符串宽度的一半.
        float xPos1 = width / 2 - paint.measureText(b[i]) / 2;
        canvas.drawText(b[i], xPos1, yPos2, paint);
        if (!listHeight.contains(yPos2)) {
            listHeight.add(yPos2);
        }
        paint.reset();// 重置画笔
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();// 点击y坐标
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
        final int c = (int) ((y - listHeight.get(0)) / totalHeight * b.length);

        switch (action) {
            case MotionEvent.ACTION_UP:
                setBackgroundDrawable(new ColorDrawable(0x00000000));
                choose = -1;//
                invalidate();
                if (mTextDialog != null) {
                    mTextDialog.setVisibility(View.INVISIBLE);
                }
                break;
            default:
                // setBackgroundResource(R.drawable.sidebar_background);
                if (oldChoose != c) {
                    if (c >= 0 && c < b.length) {
                        if (listener != null) {
                            listener.onTouchingLetterChanged(b[c]);
                        }
                        if (mTextDialog != null) {
                            mTextDialog.setText(b[c]);
                            mTextDialog.setVisibility(View.VISIBLE);
                        }
                        choose = c;
                        invalidate();
                    }
                }
                break;
        }
        return true;
    }

    /**
     * 向外公开的方法
     *
     * @param onTouchingLetterChangedListener
     */
    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    public void setData(String[] b) {
        this.b = b;
        invalidate();
    }

    /**
     * 接口
     *
     * @author coder
     */

    public interface OnTouchingLetterChangedListener {
        public void onTouchingLetterChanged(String s);
    }

}

使用方法

   //重写BaseQuickAdapter中的setList()方法
   override fun setList(list: Collection<ContactsBean>?) {
        val contactsList = list as MutableList<ContactsBean>
        Collections.sort(contactsList, PinyinContactsComparator())
        if (null != slideBar && !contactsList.isNullOrEmpty()) {
            val tempList = mutableListOf<String>()
            for (bean in contactsList) {
                val sortStr =
                    CharacterParser.getInstance().getSelling(bean.contactsName).substring(0, 1)
                        .toUpperCase()
                if (!tempList.contains(sortStr)) {
                    tempList.add(sortStr)
                }
            }
            //不加星号也可
            tempList.add("#")
            slideBar.setData(tempList.toTypedArray())
        }
        super.setList(contactsList)
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值