Android仿微信联系人列表字母侧滑控件
程序员文章站
2023-12-03 12:14:40
仿微信联系人列表字母侧滑控件, 侧滑控件参考了以下博客:
android实现listview的a-z字母排序和过滤搜索功能
首先分析一下字母侧滑控件应该如何实现,根据侧...
仿微信联系人列表字母侧滑控件, 侧滑控件参考了以下博客:
android实现listview的a-z字母排序和过滤搜索功能
首先分析一下字母侧滑控件应该如何实现,根据侧滑控件的高度和字母的数量来平均计算每个字母应该占据的高度。
在view的ondraw()方法下绘制每一个字母
protected void ondraw(canvas canvas) { super.ondraw(canvas); int height = getheight();// 获取对应高度 int width = getwidth(); // 获取对应宽度 int singleheight = height / getdata().size();// 获取每一个字母的高度 for (int i = 0; i < getdata().size(); i++) { mpaint.setcolor(getlettercolor());//绘制字母的颜色 mpaint.settypeface(typeface.default); mpaint.setantialias(true); mpaint.settextsize(singleheight); // 如果是选中的状态 if (i == mposition) { mpaint.setcolor(getletterpressedcolor()); mpaint.setfakeboldtext(true); } // x坐标等于总体宽度中间的位置减去字符串宽度的一半. float xpos = width / 2 - mpaint.measuretext(getdata().get(i)) / 2; float ypos = singleheight * i + singleheight; canvas.drawtext(getdata().get(i), xpos, ypos, mpaint); mpaint.reset();// 重置画笔 } }
然后再看一下触控事件的拦截处理
@override public boolean dispatchtouchevent(motionevent event) { final int action = event.getaction(); final float y = event.gety();// 点击y坐标 final int lastposition = mposition;//记录上一次选中字母的位置 final int position = (int) (y / getheight() * getdata().size());// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数. switch (action) { //当手指离开 case motionevent.action_up: //设置侧滑控件的背景色 setbackgroundcolor(getbackgroundnormalcolor()); mposition = -1; invalidate(); if (getontouchletterlistener() != null) { //回调事件,告知当前手指已经离开当前区域 getontouchletterlistener().ontouchoutside(); } break; default: //更改当字母为选中状态时控件的背景色 setbackgroundcolor(getbackgroundpressedcolor()); //如果选中字母的位置不等于上一次选中的位置 if (lastposition != position) { if (position >= 0 && position < getdata().size()) { if (getontouchletterlistener() != null) { //回调事件,返回当前选中的字母 getontouchletterlistener().ontouchletter(getdata().get(position)); } mposition = position; invalidate(); } } break; } return true; } public interface ontouchletterlistener { /** * 当接触到某个key的时候会调用; * @param s */ void ontouchletter(string s); /** * 当离开控件可触摸区域时会调用; */ void ontouchoutside(); }
侧滑控件完成后, 再分析一下分组界面是怎么实现的,不同分组由不同的字母作为标题,用listview就可以实现,listview里使用的adapter里面有一个方法getitemviewtype()方法用于区分返回多种类型的view,这里我们就两种, 一个是标题,一个是联系人信息;顶部里那些新的朋友、群聊等可以用listview的addheaderview()实现。但是用最sdk自带的baseadapter实现分组的话也不方便,实际上我们可以进一步包装;
首先看一下最基本的adapter封装:
public abstract class simpleadapter<t> extends baseadapter { protected context mcontext; protected list<t> mdata; public simpleadapter(){} public simpleadapter(context context, list<t> data){ init(context, data); } public void init(context context, list<t> data){ this.mcontext = context; this.mdata = data; } @override public int getcount() { return mdata.size(); } @override public t getitem(int position) { if(checkpositionisoutofrange(position)){ return null; } return mdata.get(position); } @override public long getitemid(int position) { return position; } @override public abstract view getview(int position, view convertview, viewgroup parent); public void refresh(list<t> data){ if(data == null){ this.mdata.clear(); }else{ this.mdata = data; } notifydatasetchanged(); } public boolean checkpositionisoutofrange(int position){ if(0 <= position && position < mdata.size()){ return false; } return true; } public context getcontext(){ return mcontext; } public list<t> getdata(){ return mdata; } }
这个simpleadapter实现了数据基于list的最基本方法的实现,使得每次继承baseadapter不用再实现一些基本的方法,接下来再看一下用于更好实现分组的adapter的进一步封装:
public abstract class sortadapter<k extends sortkey, v, vh_g extends viewholder, vh_c extends viewholder> extends simpleadapter<object> { public final static int view_type_group = 0; public final static int view_type_child = 1; private hashmap<sortkey, integer> mkeyindex = new hashmap<>(); public sortadapter(context context, map<k, list<v>> map) { init(context, convertmaptolist(map)); } public sortadapter(context context, list<object> list) { init(context, list); } /** * 转换分组数据为list,并且更新键值的索引 * @param map * @return */ public list<object> convertmaptolist(map<k, list<v>> map) { list<object> mdata = new arraylist<>(); mkeyindex.clear(); for (map.entry<k, list<v>> entry : map.entryset()) { mdata.add(entry.getkey()); mkeyindex.put(entry.getkey(), mdata.size() - 1); for (v v : entry.getvalue()) { mdata.add(v); } } return mdata; } public void refresh(map<k, list<v>> map) { super.refresh(convertmaptolist(map)); } @override public void refresh(list<object> data) { super.refresh(data); mkeyindex.clear(); } /** * 得到键值的索引值 * @param k * @return */ public int getkeyindex(k k){ integer integer = mkeyindex.get(k); if(integer == null){ return getkeyindexfromlist(k); } return integer; } public int getkeyindexfromlist(k k){ for(int i = 0; i < getcount(); i++){ object obj = getitem(i); if(obj != null && obj instanceof sortkey){ if(obj.equals(k)){ mkeyindex.put(k, i); return i; } } } return -1; } @override public int getitemviewtype(int position) { object obj = getitem(position); if (obj instanceof sortkey) { return view_type_group; } return view_type_child; } @override public int getviewtypecount() { return 2; } @override public int getcount() { return mdata.size(); } @override public object getitem(int position) { if (0 <= position && position < mdata.size()) { return mdata.get(position); } return null; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { int viewtype = getitemviewtype(position); view view = null; object obj = getitem(position); switch (viewtype) { case view_type_group: view = getgroupview((k)obj, position, convertview, parent); break; case view_type_child: view = getchildview((v)obj, position, convertview, parent); break; } return view; } public view getgroupview(k key, int position, view convertview, viewgroup parent){ vh_g vh; if(convertview == null){ convertview = layoutinflater.from(mcontext).inflate(getgrouplayoutid(), null); vh = oncreategroupviewholder(convertview, parent); convertview.settag(vh); }else{ vh = (vh_g)convertview.gettag(); } onbindgroupviewholder(vh, key, position); return convertview; } public view getchildview(v value, int position, view convertview, viewgroup parent){ vh_c vh; if(convertview == null){ convertview = layoutinflater.from(mcontext).inflate(getchildlayoutid(), null); vh = oncreatechildviewholder(convertview, parent); convertview.settag(vh); }else{ vh = (vh_c)convertview.gettag(); } onbindchildviewholder(vh, value, position); return convertview; } public abstract @layoutres int getgrouplayoutid(); public abstract vh_g oncreategroupviewholder(view convertview, viewgroup parent); public abstract void onbindgroupviewholder(vh_g vh, k key, int position); public abstract @layoutres int getchildlayoutid(); public abstract vh_c oncreatechildviewholder(view convertview, viewgroup parent); public abstract void onbindchildviewholder(vh_c vh, v value, int position); public interface sortkey { } public static class viewholder{ public view mparent; public viewholder(view parent){ mparent = parent; } public view findviewbyid(@idres int id){ return mparent.findviewbyid(id); } }
本项目github地址(基于androidstudio构建):
https://github.com/yuhengye/lettersort
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。