Contacts--Quick navigation (SideBar)

Source: Internet
Author: User

Reprint Please specify source : http://blog.csdn.net/forwardyzk/article/details/42914555

When we look at contacts, contacts, we'll see a quick nav sidebar on the side (ABCCEFG ...). z#), here is an introduction to this demo

Start by customizing a sliding menu sidebar that can be quickly and rapidly positioned according to the initial letter

public class SideBar extends View {//Touch event private Ontouchingletterchangedlistener ontouchingletterchangedlistener;//  26-letter 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", "#"};p rivate int choose = -1;//Select private Paint paint = new paint ();p rivate TEXTV Iew mtextdialog;public void Settextview (TextView mtextdialog) {this.mtextdialog = Mtextdialog;} Public SideBar (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} Public SideBar (context context, AttributeSet Attrs) {Super (context, attrs);} Public SideBar (Context context) {super (context);} /** * Override this method */protected void OnDraw (canvas canvas) {super.ondraw (canvas);//Get focus to change the background color. int height = getheight ();//Get corresponding height int width = getwidth (); Gets the corresponding width int singleheight = height/b.length;//Gets the height of each letter for (int i = 0; i < b.length; i++) {Paint.setcolor (Color.rgb (33, 65, 98)); /Paint.setcolor (Color.White);p aint.Settypeface (Typeface.default_bold);p Aint.setantialias (True);p aint.settextsize (20);//Selected state if (i = = choose) { Paint.setcolor (Color.parsecolor ("#3399ff"));p Aint.setfakeboldtext (true); The x-coordinate is equal to the middle-half of the string width. float XPos = width/2-Paint.measuretext (B[i])/2;float YPos = singleheight * i + SINGLEHEIGHT;CANV As.drawtext (B[i], XPos, YPos, paint);p aint.reset ();//Reset Brush}} @Overridepublic Boolean dispatchtouchevent (motionevent Event) {Final int action = Event.getaction (); final float y = event.gety ();//Click y-coordinate final int oldchoose = choose;final Ontou Chingletterchangedlistener listener = ontouchingletterchangedlistener;final int c = (int) (Y/getheight () * b.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:setBackgroundDrawable (new colordrawable (0x00000000)); Choose = -1;//invalidate (); if (mtextdialog! = null) {mtextdialog.setvisibility (view.invisible);} break;default://Set Right Alphabetical list [a,b,c,d,e ....] The background color of Setbackgroundresource (R.drawable.sortlistview_sidebar_background);(Oldchoose! = c) {if (c >= 0 && C < b.length) {if (listener! = null) {listener.ontouchingletterchanged (b[c]);} if (mtextdialog! = null) {Mtextdialog.settext (b[c]); mtextdialog.setvisibility (view.visible);} Choose = C;invalidate ();}} break;} return true;} /** * Outward-exposed method * * @param ontouchingletterchangedlistener */public void Setontouchingletterchangedlistener ( Ontouchingletterchangedlistener ontouchingletterchangedlistener) {This.ontouchingletterchangedlistener = Ontouchingletterchangedlistener;} /** * Interface */public interface Ontouchingletterchangedlistener {public void ontouchingletterchanged (String s);}


Gets the length and width of the control, based on the length, width, and number of letters to obtain the coordinates of the display letter.

The x-coordinate of the letter: the width of the control/2-the letter width/2, the y-coordinate of the letter: the height of each letter * (position of the letter +1), and then only use the brush to draw this letter, Canvas.drawtext (B[i], XPos, YPos, paint), depending on whether the current letter is pressed, To change the color of the brush


public boolean dispatchtouchevent (Motionevent event) {Final int action = Event.getaction (); final float y = event.gety ();// Click the y-coordinate final int oldchoose = choose;final Ontouchingletterchangedlistener listener = Ontouchingletterchangedlistener; Final int c = (int) (Y/getheight () * b.length);//Click the ratio 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.actio N_up:setbackgrounddrawable (colordrawable); choose = -1;//invalidate (); if (mtextdialog! = null) { Mtextdialog.setvisibility (view.invisible);} break;default://Set Right Alphabetical list [a,b,c,d,e ....] The background color setbackgroundresource (r.drawable.sortlistview_sidebar_background); if (oldchoose! = c) {if (c >= 0 && C & Lt B.length) {if (listener! = null) {listener.ontouchingletterchanged (b[c]);} if (mtextdialog! = null) {Mtextdialog.settext (b[c]); mtextdialog.setvisibility (view.visible);} Choose = C;invalidate ();}} break;} return true;}


Handling sliding monitoring in the dispatchtouchevent

public boolean dispatchtouchevent (Motionevent event) {Final int action = Event.getaction (); final float y = event.gety ();// Click the y-coordinate final int oldchoose = choose;final Ontouchingletterchangedlistener listener = Ontouchingletterchangedlistener; Final int c = (int) (Y/getheight () * b.length);//Click the ratio 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.actio N_up:setbackgrounddrawable (colordrawable); choose = -1;//invalidate (); if (mtextdialog! = null) { Mtextdialog.setvisibility (view.invisible);} break;default://Set Right Alphabetical list [a,b,c,d,e ....] The background color setbackgroundresource (r.drawable.sortlistview_sidebar_background); if (oldchoose! = c) {if (c >= 0 && C & Lt B.length) {if (listener! = null) {listener.ontouchingletterchanged (b[c]);} if (mtextdialog! = null) {Mtextdialog.settext (b[c]); mtextdialog.setvisibility (view.visible);} Choose = C;invalidate ();}} break;} return true;}



when pressed or moved, the current operation of the letter is passed out, according to the position of the letter position= the number of coordinates/control height * Letter, you can calculate the current finger under the letter array position, The listener.ontouchingletterchanged (B[c]) is then passed through the interface, and the control is redrawn, and the pressed letters are displayed in the TextView in the middle of the screen.

The finger left is, change the control background and the middle TextView setting is not displayed.



Control whether the caption amount is displayed in the adapter of the ListView

Public View GetView (final int position, view view, ViewGroup arg2) {Viewholder Viewholder = null;final Sortmodel mcontent = List.get (position), if (view = = null) {Viewholder = new Viewholder (); view = Layoutinflater.from (Mcontext). Inflate ( R.layout.item_sort_listview, null); Viewholder.tvtitle = (TextView) View.findviewbyid (r.id.title); Viewholder.tvletter = (TextView) View.findviewbyid (R.id.catalog); View.settag (Viewholder);} else {Viewholder = (Viewholder) View.gettag ();} The char ASCII value of the first letter of the classification is obtained according to position int section = getsectionforposition (position);//If the current position equals the position of the char of the first letter of the classification, it is considered the first occurrence if (Position = = getpositionforsection (section)) {viewHolder.tvLetter.setVisibility (view.visible); ViewHolder.tvLetter.setText (Mcontent.getsortletters ());} Else{viewholder.tvletter.setvisibility (View.gone);} ViewHolder.tvTitle.setText (This.list.get (position). GetName ()); return view;} Final Static class Viewholder {TextView tvletter; TextView Tvtitle;} /** * Gets the char ASCII value of the first letter of the classification according to the current position of the ListView */public int Getsectionforposition (int position) {return list.get (position). Getsortletters (). charAt (0);} /** * Gets the first occurrence of the first letter of Char ASCII value based on the first letter of the classification */public int getpositionforsection (int section) {for (int i = 0; i < GetCount (); i++) {String sortstr = List.get (i). Getsortletters (); char firstchar = Sortstr.touppercase (). charAt (0); if (Firstchar = = section) {return i;}} return-1;}


The basis of judgment in GetView is: whether this title has been shown before (that is, whether it is first displayed), if it is displayed for the first time, it is displayed, otherwise it is not displayed, of course, the beginning is the eradication of the title (that is, the first letter is sorted).

The comparator is:

public class Pinyincomparator implements comparator<sortmodel> {public int compare (Sortmodel O1, Sortmodel O2) {if ( O1.getsortletters (). Equals ("@") | | O2.getsortletters (). Equals ("#")) {return-1;} else if (O1.getsortletters (). Equals ("#") | | o2.getsortletters (). Equals ( "@")) {return 1;} else {return o1.getsortletters (). CompareTo (O2.getsortletters ());}}}

Title Information entity class
public class Sortmodel {private string name;//data displayed private string sortletters;//Display Data phonetic initials public string GetName () {RET Urn name;} public void SetName (String name) {this.name = name;} Public String Getsortletters () {return sortletters;} public void Setsortletters (String sortletters) {this.sortletters = Sortletters;}}


The data in the fill adapter

Private list<sortmodel> Filleddata (string[] date) {list<sortmodel> msortlist = new Arraylist<sortmodel > (); for (int i = 0; i < date.length; i++) {Sortmodel Sortmodel = new Sortmodel (); Sortmodel.setname (Date[i]);//Kanji converted to Pinyin string pinyin = Characterparser.getselling (Date[i]); String sortstring = pinyin.substring (0, 1). toUpperCase ();//Regular expression to determine if the first letter is an English letter if (Sortstring.matches ("[A-z]")) { Sortmodel.setsortletters (Sortstring.touppercase ());} else {sortmodel.setsortletters ("#");} Msortlist.add (Sortmodel);} return msortlist;}

First convert the Chinese characters into pinyin, then get the first letter of the pinyin, and finally set the corresponding header information.


Set the quick swipe menu to listen

Set Right Touch monitor Sidebar.setontouchingletterchangedlistener (new Ontouchingletterchangedlistener () {@Overridepublic void Ontouchingletterchanged (String s) {//The first occurrence of this letter is the position int position = adapter.getpositionforsection (S.charat (0)); Position! =-1) {sortlistview.setselection (position);}});

according to the letter that was pressed, and then quickly get the display data in the adapter the first occurrence of this letter is the title of the position, and then quickly navigate to its location.


There is also a custom edittext that matches the data displayed in the ListView, depending on the change in focus and content, and when it is empty, adds a shaking animation,

private void Filterdata (String filterstr) {list<sortmodel> filterdatelist = new arraylist<sortmodel> (); Textutils.isempty (FILTERSTR)) {filterdatelist = Sourcedatelist;} else {filterdatelist.clear (); for (Sortmodel sortmodel:sourcedatelist) {String name = Sortmodel.getname (); if (Name.indexof (filterstr.tostring ())! = -1| | Characterparser.getselling (name). StartsWith (Filterstr.tostring ())) {Filterdatelist.add (Sortmodel);}}} Sort by A-Z collections.sort (filterdatelist, Pinyincomparator); Adapter.updatelistview (filterdatelist);}


This is based on the data entered by the EditText, displaying the data in the adapter to match and then showing it. This edittext no longer detailed, please refer to the source code.

source Download: http://download.csdn.net/detail/forwardyzk/8378451

:




Contacts--Quick navigation (SideBar)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.