Development Tool: Eclipse running environment: HTC G9 android2.3.3
Let's not talk about it much. Read it first.
In fact, the left and right sides of ~ Z is a custom view that is directly overwritten on the listview.
Myletterlistview:
[Java]
View plaincopy
- Public class myletterlistview extends view {
- Ontouchingletterchangedlistener;
- 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 paint = new paint ();
- Boolean showbkg = false;
- Public myletterlistview (context, attributeset attrs, int defstyle ){
- Super (context, attrs, defstyle );
- }
- Public myletterlistview (context, attributeset attrs ){
- Super (context, attrs );
- }
- Public myletterlistview (context ){
- Super (context );
- }
- @ Override
- 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. White );
- Paint. settypeface (typeface. default_bold );
- Paint. setantialias (true );
- 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], xpos, ypos, paint );
- Paint. Reset ();
- }
- }
- @ 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 );
- }
- Public void setontouchingletterchangedlistener (
- Ontouchingletterchangedlistener ){
- This. ontouchingletterchangedlistener = ontouchingletterchangedlistener;
- }
- Public interface ontouchingletterchangedlistener {
- Public void ontouchingletterchanged (string S );
- }
- }
Then, I listened to the letter that the finger touched in the ontouchingletterchangedlistener of the activity and asked the list to jump to the corresponding position,
The initial letter prompt box is displayed:
[Java]
View plaincopy
- Private class letterlistviewlistener implements ontouchingletterchangedlistener {
- @ Override
- Public void ontouchingletterchanged (final string s ){
- If (alphaindexer. Get (s )! = NULL ){
- Int position = alphaindexer. Get (s );
- Personlist. setselection (position );
- Overlay. settext (sections [position]);
- Overlay. setvisibility (view. Visible );
- Handler. removecallbacks (overlaythread );
- // Wait 1.5 seconds to make overlay invisible
- Handler. postdelayed (overlaythread, 1500 );
- }
- }
- }
One second delay makes the pop-up initial prompt box invisible, that is, the initial prompt box will only display for one second:
[Java]
View plaincopy
- // Set overlay to be invisible
- Private class overlaythread implements runnable {
- @ Override
- Public void run (){
- Overlay. setvisibility (view. Gone );
- }
- }
There is also a question about parsing man's initials in pinyin. I am checking the system database here. There is a sort_key column in it. For example, if the name is James, then the corresponding sort_key is: zhang zhangsan 3 makes it much easier:
[Java]
View plaincopy
- // Obtain the first letter of Chinese pinyin
- Private string getalpha (string Str ){
- If (STR = NULL ){
- Return "#";
- }
- If (Str. Trim (). Length () = 0 ){
- Return "#";
- }
- Char c = Str. Trim (). substring (0, 1). charat (0 );
- // Regular expression to determine whether the first letter is an English letter
- Pattern pattern = pattern. Compile ("^ [A-Za-Z] + {1} quot ;);
- If (pattern. matcher (C + ""). Matches ()){
- Return (C + ""). touppercase ();
- } Else {
- Return "#";
- }
- }
If your data is not queried from the contact table, you can use a third-party jar package, that is, the pinyin4j-2.5.0.
The activity code and layout file are long and I will not paste them here.
I uploaded a project. If you are interested, download it and check it out. I hope you will not be able to write well enough.
Source code (0 resource points): http://download.csdn.net/detail/luck_apple/3629137