Android实现仿通讯录侧边栏滑动SiderBar效果代码
程序员文章站
2023-11-23 21:17:04
本文实例讲述了android实现仿通讯录侧边栏滑动siderbar效果代码。分享给大家供大家参考,具体如下:
之前看到某些应用的侧边栏做得不错,想想自己也弄一个出来,现在...
本文实例讲述了android实现仿通讯录侧边栏滑动siderbar效果代码。分享给大家供大家参考,具体如下:
之前看到某些应用的侧边栏做得不错,想想自己也弄一个出来,现在分享出来,当然里面还有不足的地方,请大家多多包涵。
先上图:
具体实现的代码如下:
package com.freesonfish.listview_index; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.util.attributeset; import android.view.motionevent; import android.view.view; public class mysidebar extends view { private ontouchingletterchangedlistener touchlistener; // 26个字母 public static string[] b = { "#", "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 boolean showbkg = false; int choose = -1; int scrollchoose = -1; paint paint = new paint(); paint rectpaint = new paint(); float rectwidth = 0f; public mysidebar(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } public mysidebar(context context, attributeset attrs) { super(context, attrs); init(); } public mysidebar(context context) { super(context); init(); } private void init() { rectpaint.setcolor(color.parsecolor("#cccccc")); rectwidth = paint.measuretext("#"); } /** * 重写这个方法 */ protected void ondraw(canvas canvas) { super.ondraw(canvas); if (showbkg) { canvas.drawcolor(color.parsecolor("#cccccc")); } final int height = getheight(); final int width = getwidth(); final int singleheight = height / b.length; final float xrectpos = ((float) width - paint.measuretext("#")) / 2.0f - rectwidth; final float xrectpos2 = xrectpos + 3.0f * rectwidth; for (int i = 0; i < b.length; i++) { paint.setfakeboldtext(true); paint.setantialias(true); final float xpos = ((float) width - paint.measuretext(b[i])) / 2.0f; final float ypos = singleheight * i + singleheight; if (i == choose) { paint.setcolor(color.red); canvas.drawrect(xrectpos, ypos - singleheight / 2.0f, xrectpos2, ypos + rectwidth, rectpaint); } canvas.drawtext(b[i], xpos, ypos, paint); paint.reset(); } } @override public boolean dispatchtouchevent(motionevent event) { final int action = event.getaction(); final float y = event.gety(); final int c = (int) (y / getheight() * b.length); switch (action) { case motionevent.action_down: showbkg = true; if (choose != c && touchlistener != null) { doonactiondown(c); } break; case motionevent.action_move: if (choose != c && touchlistener != null) { doonactiondown(c); } break; case motionevent.action_up: showbkg = false; invalidate(); break; } return true; } /** * listview滚动时候调用它 * * @param c */ public void setcolorwhenlistviewscrolling(int c) { if (scrollchoose != c) { scrollchoose = c; string string = listcontantsutil.abclist.get(c); for (int i = c; i < b.length; ++i) { if (string.equals(b[i])) { choose = i; invalidate(); break; } } } } /** * 当侧边栏被按下的动作 * @param c */ private void doonactiondown(int c) { if (c > 0 && c < b.length) { if (listcontantsutil.indexpositionmap.containskey(b[c])) { touchlistener.ontouchingletterchanged(b[c]); choose = c; invalidate(); } else { c = c - 1; doonactiondown(c); } } } @override public boolean ontouchevent(motionevent event) { return super.ontouchevent(event); } public void setontouchingletterchangedlistener(ontouchingletterchangedlistener touchlistener) { this.touchlistener = touchlistener; } /** * 用来通知activity显示选中的字母 * @author freeson * */ public interface ontouchingletterchangedlistener { public void ontouchingletterchanged(string s); } }
然后listcontantsutil类是存储通讯录名字的拼音等的类,具体也如下:
package com.freesonfish.listview_index; import java.util.arraylist; import java.util.hashmap; import java.util.list; public class listcontantsutil { static final list<integer> indexpositionlist = new arraylist<integer>(); static final list<string> abclist = new arraylist<string>(); static final hashmap<string, integer> indexpositionmap = new hashmap<string, integer>(); static void putnameindextomap(list<string> list, hashmap<string, string> nameandpinyin) { int lenght = list.size(); for (int i = 0; i < lenght; ++i) { string name = nameandpinyin.get(list.get(i)).substring(0, 1); // 判断该字符是属于字母还是数字或其他的 int ascii = name.tochararray()[0]; if (ascii >= 65 && ascii <= 90) { if (!indexpositionmap.containskey(name)) { indexpositionmap.put(name, i); abclist.add(name); indexpositionlist.add(i); } } else { if (!indexpositionmap.containskey("#")) { indexpositionmap.put("#", i); abclist.add("#"); indexpositionlist.add(i); } } } } }
注意,上面的程序还是有些小问题的,请大家注意优化解决。
完整实例代码代码点击此处本站下载。
希望本文所述对大家android程序设计有所帮助。