【笔记】通讯录使用侧栏字母索引

侧边字母索引在通讯录、地区选择之类的长listview中比较实用。

自定义View

public class SectionIndexBar extends View {
    //索引
    private String[] indexs = {"#", "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"};

    private int selectIndex = -1;

    private Paint paintNormal;

    private Paint paintSelect;
    //普通色
    private int colorNormal = Color.WHITE;
    //选中色
    private int colorSelect = Color.BLACK;
    //字体大小
    private float textSize = 40f;

    private int mWidth;

    private int itemHeight;
    //背景色
    private int colorBackground = Color.GRAY;

    private OnIndexListener onIndexListener;

    public void setOnIndexListener(OnIndexListener onIndexListener) {
        this.onIndexListener = onIndexListener;
    }

    public interface OnIndexListener {
        void onIndexSelect(String str);

        void onIndexChange(String str);
    }

    public SectionIndexBar(Context context) {
        this(context, null);
    }

    public SectionIndexBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setClickable(true);

        paintNormal = new Paint();
        paintNormal.setColor(colorNormal);
        paintNormal.setAntiAlias(true);
        paintNormal.setTextSize(textSize);

        paintSelect = new Paint();
        paintSelect.setColor(colorSelect);
        paintNormal.setAntiAlias(true);
        paintSelect.setTextSize(textSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置背景
        setBackgroundColor(colorBackground);
        mWidth = getWidth();
        itemHeight = getHeight() / indexs.length;

        drawChars(canvas);

        invalidate();
    }

    /**
     * 绘制字母
     *
     * @param canvas
     */
    private void drawChars(Canvas canvas) {
        for (int i = 0; i < indexs.length; i++) {
            if (selectIndex == i) {
                //绘制选中字母
                canvas.drawText(indexs[i], (mWidth - paintNormal.measureText(indexs[i])) / 2, (i + 1) * itemHeight, paintSelect);
            } else {
                //绘制普通字母
                canvas.drawText(indexs[i], (mWidth - paintSelect.measureText(indexs[i])) / 2, (i + 1) * itemHeight, paintNormal);
            }
        }
    }

    /**
     * 手势控制
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int count = (int) event.getY() / itemHeight;
        count = count < 0 ? 0 : (count >= indexs.length ? indexs.length - 1 : count);
        selectIndex = count;
        if (onIndexListener != null) {
            onIndexListener.onIndexChange(indexs[selectIndex]);
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (onIndexListener != null) {
                onIndexListener.onIndexSelect(indexs[selectIndex]);
            }
        }
        return super.onTouchEvent(event);
    }
}

获取联系人

public class ContactUtil {
    private static final String[] PHONES_PROJECTION = {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER
    };
    /**
     * 联系人显示名称
     **/
    private static final int PHONES_DISPLAY_NAME_INDEX = 0;
    /**
     * 电话号码
     **/
    private static final int PHONES_NUMBER_INDEX = 1;

    /**
     * 得到所有的手机号码联系人信息
     **/
    public static List<PhoneContact> getAllPhoneContacts(Context context) {
        List<PhoneContact> phoneContactList = getPhoneContact(context);
        List<PhoneContact> simContactList = getSIMContacts(context);
        for (PhoneContact contact : simContactList) {
            if (!isContainObject(phoneContactList, contact)) {
                phoneContactList.add(contact);
            }
        }
        return phoneContactList;
    }

    /*
     * 某个联系人是否已经存在于联系人列表中
    */
    public static boolean isContainObject(List<PhoneContact> phoneContactList, PhoneContact contact) {
        for (int i = 0; i < phoneContactList.size(); i++) {
            PhoneContact curContact = phoneContactList.get(i);
            if (curContact.getMobile().equals(contact.getMobile()) && curContact.getName().equals(contact.getName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取手机通讯录联系人
     *
     * @param context
     * @return
     */
    public static List<PhoneContact> getPhoneContact(Context context) {
        List<PhoneContact> list = new ArrayList<PhoneContact>();
        try {
            ContentResolver contentResolver = context.getContentResolver();
            Uri contentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            Cursor cursor = contentResolver.query(contentUri, PHONES_PROJECTION, null, null, null);

            while (cursor != null ? cursor.moveToNext() : false) {
                String name = cursor.getString(0);
                String mobile = cursor.getString(1).replace("+86", "").replace(" ", "").replace("-", "");
                String namePinYin = PinYinUtil.getPinYin(name).toUpperCase(Locale.US);
                PhoneContact contactInfo = new PhoneContact(name, mobile, namePinYin);
                list.add(contactInfo);
            }
            cursor.close();
        } catch (Exception e) {
        }
        return list;
    }

    /**
     * 得到手机SIM卡联系人人信息
     **/
    private static List<PhoneContact> getSIMContacts(Context context) {
        List<PhoneContact> list = new ArrayList<PhoneContact>();
        try {
            ContentResolver contentResolver = context.getContentResolver();
            Uri uri = Uri.parse("content://icc/adn");
            Cursor cursor = contentResolver.query(uri, PHONES_PROJECTION, null, null, null);
            while (cursor != null ? cursor.moveToNext() : false) {
                String name = cursor.getString(PHONES_DISPLAY_NAME_INDEX);
                String mobile = cursor.getString(PHONES_NUMBER_INDEX).replace("+86", "").replace(" ", "").replace("-", "");
                String namePinYin = PinYinUtil.getPinYin(name).toUpperCase(Locale.US);
                PhoneContact contactInfo = new PhoneContact(name, mobile, namePinYin);
                list.add(contactInfo);
            }
            cursor.close();
        } catch (Exception ex) {

        }
        return list;
    }
}

源码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值