Android Alphabet index for ListView positioning

Source: Internet
Author: User

Recently busy, nothing to do, in the time of the mobile phone to see the Android system comes with the Address book software to contact the quick location function. Feel this function is also more practical oneself to try to achieve.

Although the network still have Daniel closed the frame, but if you come to realize it is also good, personal more like to write things, others write good things can be used for reference, or not recommended to see directly take

To use, the code can be copied, the author's ideas need to slowly experience.

Basic Introduction:

First of all, Android itself has provided an interface to achieve fast positioning, the Sectionindexer interface has a total of three methods.

   

Object[] GetSections (); Returns all the sections

    

int getpositionforsection (int sectionindex); Returns a position based on the section index

int getsectionforposition (int position); Contrary to the above method.

A section can be understood as part of a ListView, such as grouping the initials into the same group as the contacts, and each group is a section.

Basic design:

I look at the list of letters as a view that contains a member that implements the Sectionindexer interface. and define a callback interface to notify the update ListView when the index changes.

Override the Onmeasure (Int,int) method to calculate the width height of the view.

  

@Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {if (Msectionindex = = null) { Super.onmeasure (Widthmeasurespec, heightmeasurespec); return;} if (msections = = null) Msections =  msectionindex.getsections (); int  measureheight; int measurewidth; int height< c2/>=  (int) (Sectionheight () *  msections.length) + (Getpaddingtop () + getpaddingbottom ()); int width = sectionw Idth () + getpaddingleft () + getpaddingright ();/** * Sets the width height of the view according to the layout parameters. * If the height or width of the layout parameter is Layoutparams.wrap_content * Then the width height of the view is width, height * Otherwise it is set directly according to the value of the layout parameter  */LAYOUTPARAMSLP  = Getlayoutparams (); if (lp.height! = layoutparams.wrap_content)  height = lp.height;if (lp.width!  = layoutparams.wrap_content) width = lp.width;/**     

Override the OnLayout method by overriding the method because all indexes fill the view. You do not have to rewrite the OnLayout method, as long as the view can get the height and then calculate it.

@Overrideprotected void OnLayout (Boolean changed, int left, int top, int right,int bottom) {/**  * view is greater than the height of the list display, Add a few intervals between each letter, so that   each letter is aligned and fills the entire view.   */  int viewheight = getheight ()-  (Getpaddingtop () + getpaddingbottom ());  int   originalheight = mheight * MSECTIONS.LENGTH;    int overheight = viewheight-originalheight;     if (overheight <= 0) return;    Malphainterval = overheight/(msections.length);}

Rewrite the OnDraw method, this method does not have to say, we all know what to do, directly on the code.

@Overrideprotected void OnDraw (canvas canvas) {if (Msectionindex = = null) return; int height = getheight (); int widht = Getwi DTH ();//Draw background if (mbackground) {rectfround = new RECTF (0, 0, widht, height); Canvas.drawrect (round,  mbackgroundpaint);} Draw the letter  float textheight  = malphapaint.descent ()-malphapaint.ascent (); float  y =  textheight/1.5f +  getpaddingtop ();//first letter offset.  float  x =  getpaddingleft (); for (int i = 0; i < msections.length; i++) {if (mcurrentsection = = i) malphapaint.setc Olor (Color.Blue); Elsemalphapaint.setcolor (Color.White); Y + = malphapadding + Malphainterval;canvas.drawtext (msections[i].tostring ()  , x, y,  malphapaint); y + = Malphapadding + TextHeight  ;}}

:

  

Finally, there are some auxiliary methods, not one by one examples of people download the source code to see.

SOURCE Download:

Http://pan.baidu.com/s/1gdw1gyf

Android Alphabet index for ListView positioning

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.