The ListView with indexed columns, which is very common in Android development, makes it easy for users to index letters, just like in the Address book:
Today, we implement this ListView with an indexed column from zero to one.
How to implement this control, we should comb out a way of thinking.
① first, you should inherit the letter's index column with a control and draw the letter by OnDraw method.
② then we should listen to the Ontouch event of the letter control to determine if the user actually pressed the letter.
③ is the linkage between this index bar and the ListView, which is to slide the ListView to the position where the letter is pressed.
The general flowchart is as follows:
With the front cushion, we lead this article-code.
First, the index bar is the code that controls how letters are drawn on the control:
/** * Sidebar Display letters */private 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 "," # "}; /** * How to draw a list control * The letters to be drawn are drawn in a specified area from top to bottom * If the selected letter is highlighted */@Override protected void OnDraw (Ca Nvas canvas) {//TODO auto-generated Method stub super.ondraw (canvas); int height = getheight ();//Gets the corresponding height int width = getwidth (); Gets the corresponding width int singleheight = height/words.length;//Gets the height of each letter for (int i = 0; i < words.length; i++) { Paint.setcolor (Color.rgb (33, 65, 98)); Paint.setcolor (Color.White); Paint.settypeface (Typeface.default_bold); Paint.setantialias (TRUE); Paint.settextsize (20f); The selected state if (Isdown) {Paint.setcolor (Color.parsecolor ("#ffffff")); Paint.setfakeboldtext (TRUE); The//x coordinate is equal to the middle-half of the string width. float XPos = width/2-Paint.measuretext (Words[i])/2; float YPos = singleheight * i + singleheight; Canvas.drawtext (Words[i], XPos, YPos, paint); Paint.reset ();//Reset Brush}}
With the above code, we can draw the following conclusion: the letters to be drawn are drawn in a specified area from top to bottom, each letter has the same x coordinate, and the X coordinate is half the width of the control. If the current letter is selected, the highlight is displayed. Ideas:
Next, we come to the second step, to determine exactly what the user click is the letter, the code is as follows:
/** * How to handle touch events * The user presses the time, the entire control background changes * According to the Y coordinate to determine exactly what the user pressed the letter * The current letter highlighting the letter displayed in the center of the listview * * @Override public boolean dispatchtouchevent (Motionevent event) {int action = event.getaction (); Final float y = event.gety ();//Click y-coordinate final int oldchoose = choose; Final Itouchingletterchangedlistener listener = Ontouchingletterchangedlistener; Final int c = (int) (Y/getheight () * words.length);//Click the percentage of the total height of the y-coordinate *b the length of the array is equal to the number of hits in B. Switch (action) {case motionevent.action_up:isdown=false; Setbackgroundresource (Android. R.color.transparent); Choose = -1;//invalidate (); if (textviewdialog! = null) {textviewdialog.setvisibility (view.invisible); } break; Default:isdown=true; Setbackgroundresource (R.color.sidebar_bg_color); if (oldchoose! = c) {if (c >= 0 && C < words. length) {if (listener! = NULL) {listener. Ontouchingletterchanged (Words[c]); } if (Textviewdialog! = null) {Textviewdialog.settext (words[c]); Textviewdialog.setvisibility (view.visible); } choose = C; Invalidate (); }} break; } return true; }
Through the above code, we can summarize: when the user presses, the entire control background changes, according to the user pressed the y-coordinate to determine whether the user is pressed the letter, and will press the letter to display the center of the screen, as follows:
Finally, move the ListView to the corresponding position of the letter, with the following code:
/** * Move the ListView to the appropriate location according to the user clicking the letter * /Sidebar.setontouchingletterchangedlistener (new Itouchingletterchangedlistener () { @Override public void ontouchingletterchanged (String cString) { int Position =-1; if (cstring.length () > 0) { position = myadapter.getpositionforsection (cString . CharAt (0)); } if (Position! =-1) { listview.setselection (position); } else if (Cstring.contains ("#")) { Listview.setselection (0);}} );
Great length said so much, the effect of the control is:
The source code address is:
Http://pan.baidu.com/s/1dDMDjhR
Go to Android custom control two--Custom index bar ListView