ExpandableListView 添加边框首字母点击监听代码(索引)

来源:互联网 发布:阿里云企业邮箱的smtp 编辑:程序博客网 时间:2024/06/16 09:37
自定义view
public class AllContactIndexView extends View {    public static final String[] WORDS = {"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 preHeight = 18;//每一个字母的高度    private AllcontactExpandableBaseApdpter allcontactExpandableBaseApdpter;    private ExpandableListView expandableListView;    //设置ListView的set方法  这样可以在main调用    public void setMlistView(ExpandableListView expandableListView) {        this.expandableListView = expandableListView;        allcontactExpandableBaseApdpter = (AllcontactExpandableBaseApdpter) expandableListView.getExpandableListAdapter();//获得ListView关联的Adapter    }    private Paint paint;//画笔    private int blankHeight;//空白的高度  字母和字母之间的距离  //TODO 留着计算    public AllContactIndexView(Context context) {        super(context);        init();    }    public AllContactIndexView(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    //在该生命周期中,会测量控件的宽高    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec); //getMeasuredHeight()的方法是 获取该组件高度        blankHeight = (getMeasuredHeight() - (WORDS.length * preHeight)) / (WORDS.length - 1);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        for (int i = 0; i < WORDS.length; i++) {            //在该组件上画文字            canvas.drawText(WORDS[i], getMeasuredWidth() / 2, (i + 1) * (preHeight + blankHeight), paint);        }    }    //该方法会处理用户的点击事件    @Override    public boolean onTouchEvent(MotionEvent event) {        //当用户按下的时候        if (event.getAction() == MotionEvent.ACTION_DOWN) {            int index = (int) (event.getY() / (preHeight + blankHeight));            if (index >= WORDS.length) {                //做一个修正                index = WORDS.length - 1;            } else if (index < 0) {                index = 0;            }            //TODO 通过字母获得ListView的条数,并滚动到该位置            int pos = allcontactExpandableBaseApdpter.getIndexFromString(WORDS[index]);            if (pos >= 0) {                expandableListView.setSelection(pos);                Log.i("+++++++",pos+"");            }        }        return super.onTouchEvent(event);    }    private void init() {        paint = new Paint();        //文字颜色        paint.setColor(Color.BLACK);        //文字对齐方式        paint.setTextAlign(Paint.Align.CENTER);        //将高度从dp转换成px        preHeight = dpTopx(preHeight, getContext());        //设置字体大小的时候,需要px为单位  所以需要将获得的dp转化成px        paint.setTextSize(preHeight);        paint.setAntiAlias(true);    }    //将dp转换成px 的方法    private int dpTopx(int dp, Context context) {        int px = 0;        //获得资源文件的数据所用到的 需要通过Resiyrces下得DisplayMetrics的destiyDip        // TODO 自己查下        Resources resources = context.getResources();        //屏幕度量  //获取屏幕度量        // TODO 自己查下        DisplayMetrics displayMetrics = resources.getDisplayMetrics();        //dp * displayMetrics.densityDpi / 160f是        // TODO dp和px的倍数关系    0.5f是为了让px取整        px = (int) (dp * displayMetrics.densityDpi / 160f + 0.5f);        return px;    }
}

2.适配器代码:

public class AllcontactExpandableBaseApdpter extends BaseExpandableListAdapter{    private Context context;    private AllContact allContact;    public AllcontactExpandableBaseApdpter(Context context,AllContact allContact) {        this.context = context;        this.allContact = allContact;    }    @Override    public int getGroupCount() {        return allContact.getGroupCount();    }    @Override    public int getChildrenCount(int groupPosition) {        return allContact.getChildcount(groupPosition);    }    @Override    public Object getGroup(int groupPosition) {        return null;    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return null;    }    @Override    public long getGroupId(int groupPosition) {        return 0;    }    @Override    public long getChildId(int groupPosition, int childPosition) {        return 0;    }    @Override    public boolean hasStableIds() {        return false;    }    @Override    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {        GroupViewHolder groupViewHolder;        if(convertView == null){            convertView = LayoutInflater.from(context).inflate(R.layout.item_expandblelistview_all_group,null);            groupViewHolder = new GroupViewHolder(convertView);            convertView.setTag(groupViewHolder);        }        else{            groupViewHolder = (GroupViewHolder) convertView.getTag();        }        groupViewHolder.letterTv.setText(allContact.getGroupName(groupPosition));        return convertView;    }    @Override    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {        ChildViewHolder childViewHolder;        if(convertView == null){            convertView = LayoutInflater.from(context).inflate(R.layout.item_expandablelistview_all_child,null);            childViewHolder = new ChildViewHolder(convertView);            convertView.setTag(childViewHolder);        }        else{            childViewHolder = (ChildViewHolder) convertView.getTag();        }        ContactPersonContent contactPersonContent = allContact.getChild(groupPosition,childPosition);        childViewHolder.nameTV.setText(contactPersonContent.getName());        childViewHolder.numberTv.setText(contactPersonContent.getNumber());        return convertView;    }    class GroupViewHolder{//父列表缓存类        TextView letterTv;        public GroupViewHolder(View view){            letterTv = (TextView) view.findViewById(R.id.item_all_group_name);        }    }    class ChildViewHolder{//子列表缓存类        TextView nameTV,numberTv;        public ChildViewHolder(View view){            nameTV = (TextView) view.findViewById(R.id.item_child_name);            numberTv = (TextView) view.findViewById(R.id.item_child_number);        }    }    @Override    public boolean isChildSelectable(int groupPosition, int childPosition) {        return false;    }    //在数据里    public int getIndexFromString(String s){        for (int i = 0; i < allContact.firstLetter.size(); i++) {            if (allContact.firstLetter.get(i).equals(s)){                //找到该字母,立即返回索引                return i;            }        }        return -1;//没有找到字母,返回-1    }
}



主函数代码: (fragment中)

public class AllContactPersonFragment extends Fragment {    private ExpandableListView expandableListView;    private ContentResolver cr;    private AllContact allContact;//数据类    private AllcontactExpandableBaseApdpter allcontactExpandableBaseApdpter;    private DBTool dbTool;//定义数据库工具类    private Context context;    private com.tianhe.homework.allcontact.AllContactIndexView allContactIndexView;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_contact_person_all,null);        return view;    }    @Override    public void onAttach(Context context) {        super.onAttach(context);        this.context = context;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        allContact = new AllContact();        expandableListView = (ExpandableListView) getActivity().findViewById(R.id.all_expandablelistview);        cr = context.getContentResolver();        allcontactExpandableBaseApdpter = new AllcontactExpandableBaseApdpter(getActivity(), initData());        expandableListView.setAdapter(allcontactExpandableBaseApdpter);        allContactIndexView = (AllContactIndexView) getActivity().findViewById(R.id.index_view);        allContactIndexView.setMlistView(expandableListView);        dbTool = new DBTool(context);//初始化数据库    }    private AllContact initData() {        Cursor cursor = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);        AllContact allContact = new AllContact();        dbTool = new DBTool(context);//进行初始化        if (cursor != null) {            while (cursor.moveToNext()) {                ContactPersonContent content = new ContactPersonContent();                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));//添加通讯录姓名                String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); //添加通讯录电话                content.setName(name);                content.setNumber(number);                allContact.addContact(content);                dbTool.insert(number, name);//将姓名和电话号加入数据库            }        }        cursor.close();        return allContact;    }}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝卖家被买家投诉卖假货怎么办 天猫三天未发货怎么办 天猫申请换货卖家不处理怎么办 天猫新疆不发货怎么办 天猫商城少发货怎么办 下单了卖家不发货怎么办 天猫超市漏发货怎么办 天猫购物几天不发货怎么办 天猫总是不发货怎么办 申请退款后卖家又发货了怎么办 天猫拍后申请退款卖家发货怎么办 淘宝上没下单却收到了货怎么办 被买家投诉三无产品怎么办 阿里巴巴卖家虚假发货怎么办 淘宝捡到便宜但是卖家不发货怎么办 被工商局查到三无产品怎么办 淘宝买到三无产品电器怎么办 天猫商城被投诉怎么办 床板有虫子咬人怎么办 微信充电话费充错怎么办 联通话费充多了怎么办 qq钱包充值要验证码怎么办 在微信qq币充错账号怎么办 微信qq币充错了怎么办 魅蓝e玩游戏卡怎么办 魅蓝5玩游戏卡怎么办 微信qb充错号了怎么办 支付宝qb充错号了怎么办 手机上q币充错了怎么办 q币数值充错了怎么办 微信充值商户电话是假了怎么办 微信冲话费冲错了怎么办 淘宝退款不退邮费怎么办 淘金币买的退款怎么办 淘宝退款不退运费怎么办 拼多多不退运费怎么办 开发商不退团购服务费怎么办 支付宝话费充错了怎么办 电视版本低不支持投屏怎么办 绝地求生刺激战场不支持机型怎么办 手机不支持微信运动怎么办