Android仿微信通讯录滑动快速定位功能
程序员文章站
2023-12-05 13:36:46
先给大家展示下效果图:
实现代码如下:
下面简单说下实现原理。
public class indexbar extends linearlayout im...
先给大家展示下效果图:
实现代码如下:
下面简单说下实现原理。
public class indexbar extends linearlayout implements view.ontouchlistener { private static final string[] indexes = 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"}; private static final int touched_background_color = 0x40000000; private onindexchangedlistener mlistener; public void setonindexchangedlistener(onindexchangedlistener listener) { mlistener = listener; } public indexbar(context context) { this(context, null); } public indexbar(context context, attributeset attrs) { this(context, attrs, 0); } public indexbar(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(attrs); } private void init(attributeset attrs) { typedarray ta = getcontext().obtainstyledattributes(attrs, r.styleable.indexbar); float indextextsize = ta.getdimension(r.styleable.indexbar_indextextsize, utils.sp2px(getcontext(), 12)); int indextextcolor = ta.getcolor(r.styleable.indexbar_indextextcolor, 0xff616161); ta.recycle(); setorientation(vertical); setontouchlistener(this); for (string index : indexes) { textview text = new textview(getcontext()); text.settext(index); text.settextsize(typedvalue.complex_unit_px, indextextsize); text.settextcolor(indextextcolor); text.setgravity(gravity.center); linearlayout.layoutparams params = new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, 0, 1); text.setlayoutparams(params); addview(text); } } @override public boolean ontouch(view v, motionevent event) { switch (event.getaction()) { case motionevent.action_down: setbackgroundcolor(touched_background_color); handle(v, event); return true; case motionevent.action_move: handle(v, event); return true; case motionevent.action_up: setbackgroundcolor(color.transparent); handle(v, event); return true; } return super.ontouchevent(event); } private void handle(view v, motionevent event) { int y = (int) event.gety(); int height = v.getheight(); int position = indexes.length * y / height; if (position < 0) { position = 0; } else if (position >= indexes.length) { position = indexes.length - 1; } string index = indexes[position]; boolean showindicator = event.getaction() != motionevent.action_up; if (mlistener != null) { mlistener.onindexchanged(index, showindicator); } } public interface onindexchangedlistener { void onindexchanged(string index, boolean showindicator); } }
使用
public class companyactivity extends baseactivity implements indexbar.onindexchangedlistener { @bind(r.id.lv_company) listview lvcompany; @bind(r.id.ib_indicator) indexbar ibindicator; @bind(r.id.tv_indicator) textview tvindicator; private list<companyentity> mcompanylist = new arraylist<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_company); // ... } @override public void onindexchanged(string index, boolean showindicator) { int position = -1; for (companyentity company : mcompanylist) { if (textutils.equals(company.getname(), index)) { position = mcompanylist.indexof(company); break; } } if (position != -1) { lvcompany.setselection(position); } tvindicator.settext(index); tvindicator.setvisibility(showindicator ? view.visible : view.gone); } }
继承自linearlayout,添加了26个字母索引textview,当手指滑动时通知activity更新界面。
核心是ontouchlistener,手指滑动的时候根据当前y坐标计算出手指所在的索引位置,要注意临界值。
以上所述是小编给大家介绍的android仿微信通讯录滑动快速定位功能,希望对大家有所帮助