I believe you have seen such an effect. I believe that you have also found many examples on the Internet, but most of them are incomplete and fail to achieve a real effect, so today I will share with you my completeSource codeI hope it will be helpful to everyone. People who need it can use it directly. As for the technical point, there is actually nothing, and one for getting Pinyin is used. Pinyin4j-2.5.0.jar This Jar Package to help us achieve the effect. Directly, and then go to the source code.
First look at the one next to it 26 Letter control implementation: I drew it myself. 26 Letters.
Public class mysidebar extends view {ontouchingletterchangedlistener; // 26 letters 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"}; int choose =-1; paint = new paint (); Public mysidebar (context, attributeset attrs, int defstyle) {super (con Text, attrs, defstyle);} public mysidebar (context, attributeset attrs) {super (context, attrs);} public mysidebar (context) {super (context );} /*** override this method */protected void ondraw (canvas) {super. ondraw (canvas); If (showbkg) {canvas. drawcolor (color. parsecolor ("#40000000");} int Height = getheight (); int width = getwidth (); int singleheight = height/B. length; For (int I = 0; I <B. length; I ++) {paint. setcolor (color. black); // paint. setcolor (color. white); paint. settypeface (typeface. default_bold); paint. setantialias (true); paint. settextsize (20); if (I = choose) {paint. setcolor (color. parsecolor ("# 3399ff"); paint. setfakeboldtext (true);} float xpos = width/2-paint. measuretext (B [I])/2; float ypos = singleheight * I + singleheight; canvas. drawtext (B [I], XP OS, ypos, paint); paint. reset () ;}} private Boolean showbkg = false; @ override public Boolean dispatchtouchevent (motionevent event) {final int action = event. getaction (); Final float y = event. gety (); Final int oldchoose = choose; Final ontouchingletterchangedlistener listener = ontouchingletterchangedlistener; Final int c = (INT) (y/getheight () * B. length); Switch (Action) {Case motionevent. Action_down: showbkg = true; If (oldchoose! = C & listener! = NULL) {If (C> 0 & C <B. length) {listener. ontouchingletterchanged (B [c]); choose = C; invalidate () ;}} break; Case motionevent. action_move: If (oldchoose! = C & listener! = NULL) {If (C> 0 & C <B. length) {listener. ontouchingletterchanged (B [c]); choose = C; invalidate () ;}} break; Case motionevent. action_up: showbkg = false; choose =-1; invalidate (); break;} return true ;}@ override public Boolean ontouchevent (motionevent event) {return Super. ontouchevent (event);}/*** method for public externally ** @ Param ontouchingletterchangedlistener */Public void listener (ontouchingletterchangedlistener) {This. ontouchingletterchangedlistener = ontouchingletterchangedlistener;}/*** interface ** @ author coder **/public interface ontouchingletterchangedlistener {public void ontouchingletterchanged (string S );}}
let's look at the implementation of the layout file, framelayout is used here
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <framelayout Android: id = "@ + ID/llparent" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <listview Android: id = "@ + ID/lvshow" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"/> <textview Android: Id = "@ + ID/tvletter" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center" Android: Background = "@ drawable/show_head_toast_bg" Android: gravity = "center" Android: maxwidth = "70dip" Android: minwidth = "70dip" Android: padding = "10dip" Android: textcolor = "#99 ffffff" Android: textsize = "50sp"> </textview> <COM. jiahui. view. mysidebar Android: Id = "@ + ID/myview" Android: layout_width = "30dip" Android: layout_height = "fill_parent" Android: layout_gravity = "right"> </COM. jiahui. view. mysidebar> </framelayout> </linearlayout>
next, see activity . For more information, see Code
.
Public class testactivity extends activity implements detail {private listview lvshow; private list <userinfo> userinfos; private textview overlay; private mysidebar myview; private myuserinfoadapter adapter; private overlaythread = new overlaythread (); @ override protected void oncreate (bundle savedinstancestate) {// todo auto-generated method stub super. oncreate (savedinstancestate); setcontentview (R. layout. main); lvshow = (listview) findviewbyid (R. id. lvshow); myview = (mysidebar) findviewbyid (R. id. myview); overlay = (textview) findviewbyid (R. id. tvletter); lvshow. settextfilterenabled (true); overlay. setvisibility (view. invisible); getuserinfos (); log. I ("coder", "userinfos. size "+ userinfos. size (); adapter = new myuserinfoadapter (this, userinfos); lvshow. setadapter (adapter); myview. setontouchingletterchangedlistener (this);} private void getuserinfos () {userinfo [] userinfoarray = new userinfo [] {New userinfo ("Tang Seng", "18765432345", pinyinutils. getalpha ("Tang Miao"), new userinfo ("", "18765432345", pinyinutils. getalpha (""), new userinfo ("", "18765432345", pinyinutils. getalpha ("Stay"), new userinfo ("8899", "18765432345", pinyinutils. getalpha ("8899"), new userinfo ("Sun Wukong", "18765432345", pinyinutils. getalpha ("Sun Wukong"), new userinfo ("A 3", "18765432345", pinyinutils. getalpha ("A 3"), new userinfo ("Zhang 3", "18765432345", pinyinutils. getalpha ("James"), new userinfo ("James B", "18876569008", pinyinutils. getalpha ("Zhang 'er B"), new userinfo ("A 3", "18765432345", pinyinutils. getalpha ("A 3"), new userinfo ("Zhang 3", "18765432345", pinyinutils. getalpha ("James"), new userinfo ("James B", "18876569008", pinyinutils. getalpha ("Zhang 'er B"), new userinfo ("A 3", "18765432345", pinyinutils. getalpha ("A 3"), new userinfo ("Zhang 3", "18765432345", pinyinutils. getalpha ("James"), new userinfo ("James B", "18876569008", pinyinutils. getalpha ("Zhang 'er B"), new userinfo ("A 3", "18765432345", pinyinutils. getalpha ("A 3"), new userinfo ("Zhang 3", "18765432345", pinyinutils. getalpha ("James"), new userinfo ("James B", "18876569008", pinyinutils. getalpha ("Zhang 'er B"), new userinfo ("Li Si", "18909876545", pinyinutils. getalpha (""), new userinfo ("", "18909876545", pinyinutils. getalpha ("Wang xiao'er"), new userinfo ("Zhang Sanfeng", "18909876545", pinyinutils. getalpha ("Zhang Sanfeng"), new userinfo ("Guo Jing", "18909876545", pinyinutils. getalpha ("Guo Jing"), new userinfo ("Zhang Wuji", "18909876545", pinyinutils. getalpha ("Zhang Wuji"), new userinfo ("Huang xiaoxian", "18909876545", pinyinutils. getalpha ("")}; arrays. sort (userinfoarray, new pinyincomparator (); userinfos = arrays. aslist (userinfoarray);} private handler = new handler () {}; private class overlaythread implements runnable {public void run () {overlay. setvisibility (view. gone) ;}@ override public void ontouchingletterchanged (string s) {log. I ("coder", "s:" + S); overlay. settext (s); overlay. setvisibility (view. visible); handler. removecallbacks (overlaythread); handler. postdelayed (overlaythread, 1000); If (alphaindexer (s)> 0) {int position = alphaindexer (s); log. I ("coder", "position:" + position); lvshow. setselection (position) ;}} public int alphaindexer (string s) {int position = 0; For (INT I = 0; I <userinfos. size (); I ++) {If (userinfos. get (I ). getpy (). startswith (s) {position = I; break ;}} log. I ("coder", "I" + Position + userinfos. get (position); Return position ;}}
Now, even if this effect is completed, we hope you can share more things and learn together!
From: http://www.apkbus.com
Welcome to the group discussion with friends who love Android Development.Guangzhou super group 218251417,Nanjing group 220818530,Shenzhen group 260134856,Xi'an group 252746034,Hangzhou group 253603803,Xiamen group 253604146