Android Custom view-Alphabetical Index Table (i)

Source: Internet
Author: User

In some Android apps, for quick positioning, you'll often see an alphabetical index table on the right side of the screen, which today attempts to implement the basic layout of an indexed table using a custom view.

The Alphabet Index table is styled as shown below,

At this point we need to know at least the following parameter values: 1. Letter size; 2. Width of the area where the single letter is located; 3. Height of the area where the individual letters are located. Now look at how to achieve:

/**
* 26 English letters and a # character, #字符是为了索引非英文字母的内容, such as a phone number.
*/

Privatestring[] Malphabettable = { "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", "#" }; /*** Size of individual letters*/ Private intMalphabetsize = 0; /*** Width of the alphabet*/ Private intMwidth = 0; /*** height of the alphabet*/ Private intMheight = 0; /*** Window Height*/ Private intMdisplayheight; /*** Height of the area where the individual letters are located*/ Private intMsteppixel; Private intMfastscrollviewheight = 0; Privaterect[] Malphabetrect =NewRect[27]; PrivateRect Mpointrectall =NewRect (); Private Static intmalphabetleftpadding = 0; Private Boolean[] Mpoint =New Boolean[27]; PublicAlphabetfastscorll (Context context) { This(Context,NULL); } PublicAlphabetfastscorll (Context context, AttributeSet attrs) {Super(context, attrs); This. Setalpha (1.0f);//Set Transparency 1 displaymetrics metric=NewDisplaymetrics (); ((WindowManager) GetContext (). Getsystemservice (GetContext (). Window_service). Getdefaultdisplay (). Getmetrics (Metric); Mdisplayheight=Metric.heightpixels; //Get window height }

In the constructor, only the transparency and initialization mdisplayheight parameters of the index table are set. Most of the work on the index table is done in OnDraw ().

@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Msteppixel=GetContext (). Getresources (). Getdimensionpixeloffset (R.dimen.fast_scroll_size); Mfastscrollviewheight= Msteppixel * 27;
// Initialize 27 letters each letter corresponds to the size range of the area Initalphabetrect ();

// Initialize the background size of the alphabetical index table initpointrect (mfastscrollviewheight, Msteppixel);
//Set Background colorPaint PAINTB =NewPaint (); Paintb.setcolor (Contextcompat.getcolor (GetContext (), r.color.coloraccent)); //paintb.setalpha (0);Canvas.drawrect (Mpointrectall, PAINTB);
     // set letters in size, color, position, etc. Paint Paint
=NewPaint (); floatTextSize =GetContext (). Getresources (). Getdimensionpixeloffset (R.dimen.asus_index_size); Paint.setcolor (Contextcompat.getcolor (GetContext (), R.color.fast_scroll_text_color)); Paint.settextalign (Align.center); Paint.settextsize (textSize); Paint.setantialias (true); for(inti = 0; i < malphabetrect.length; i++) { intx = (Malphabetrect[i].left + malphabetrect[i].right)/2; inty =Malphabetrect[i].bottom; Y= y-(int) (0.5 * (Msteppixel-textSize));
// letters are drawn in the Malphabetrect[i] area in the middle canvas.drawtext (Malphabettable[i], x, y, paint); } }
Private voidInitalphabetrect () { for(inti = 0; i < malphabetrect.length; i++) {Malphabetrect[i]=NewRect (); Malphabetrect[i].left=malphabetleftpadding; Malphabetrect[i].right= Malphabetleftpadding + This. getwidth (); //log.d (TAG, "initalphabetrect right =" + malphabetrect[i].right); if(i = = 0) {Malphabetrect[i].top= 0; Malphabetrect[i].bottom=Msteppixel; } Else{malphabetrect[i].top= Malphabetrect[i-1].top +Msteppixel; Malphabetrect[i].bottom= Malphabetrect[i].top +Msteppixel; } } } Private voidInitpointrect (intFastscrollviewheight,intsteppixel) { intCanvaswidth =GetContext (). Getresources (). Getdimensionpixeloffset (R.dimen.asus_index_canvas_width); Mpointrectall=NewRect (); Mpointrectall.left= 0; Mpointrectall.right=Canvaswidth; Mpointrectall.top= 0; Mpointrectall.bottom= This. GetHeight (); }

Here are a few things to do in OnDraw ():

1, initialize the range of the area in which the Index table letter is located: Creates a new Rect object, setting its left and right and up and down sizes. The width size is provided by the layout file, and the height is msteppixel.

2, initialize index Table background range: width = asus_index_canvas_width, height = this . GetHeight ();

3, already know each letter area range as well as the index background range, new two paint on canves to draw;

The Index Table control can be implemented simply by the steps above, and it is certainly meaningless to display an index table in light. The index table is generally combined with the ListView to enable quick positioning. The next article is ready to mimic the contact app, which allows you to quickly navigate to a contact by using the Index table.

Android Custom view-Alphabetical Index Table (i)

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.