Android Learning Series (9)-group listview of App list

Source: Internet
Author: User

Attracting users' attention is our constant pursuit;
Presenting the most valuable information in the first place is concise and generous, telling the customer how wise your choice is. This is exactly what you have been looking for a long time.

There are still many application scenarios for grouping, where data sets are often displayed in groups;
There are also many grouping forms, the most common is embedded in the list, a lot of expandlistview is also said on the Internet.
AndroidThe contacts in the built-in Address Book follow the first letter of the pinyin alphabet.(A, B, C, D ......)Group classification:
We want to achieve this similar effect today.

1. Sample Data:
To highlight the key points, we provide a sorted data sample:

// List: Data Set private list <string> List = new arraylist <string> (); // listtag: tag set, where tag is the classification label, headerprivate list <string> listtag = new arraylist <string> (); Public void setdata () {list. add ("A"); listtag. add ("A"); For (INT I = 0; I <3; I ++) {list. add ("Avatar" + I);} List. add ("B"); listtag. add ("B"); For (INT I = 0; I <3; I ++) {list. add ("bit Storm" + I);} List. add ("C"); listtag. add ("C"); For (INT I = 0; I <30; I ++) {list. add ("Charlie Fengyun" + I );}}

2. Prepare the activity layout:
Place a listview to present data.
Group_list_activity.xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <! -- Simple list display --> <listview Android: Id = "@ + ID/group_list" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: cachecolorhint = "#00000000"/> </linearlayout>

3. Custom adapter (this article inherits arrayadapter ):
This is the focus and core of this article.
The adapter interface builds an access bridge between the data and the interface. The most important thing is the getview () method. With this method, we can customize the interface to a certain extent.
Arrayadapter indirectly implements the adapter interface. For simplicity, the data source only provides a single string array.

Private Static class grouplistadapter extends arrayadapter <string> {// a list of tags to be stored. It is used to determine the type of a data item. // if the data item is in the tag list, it is a tag item, otherwise, the data item is private list <string> listtag = NULL; Public grouplistadapter (context, list <string> objects, list <string> tags) {super (context, 0, objects ); this. listtag = tags;} @ override public view getview (INT position, view convertview, viewgroup parent ){.......}}

Let's take a look at the getview method:

 
// This method lists the organizations of a row in the order of the adapter. // position indicates the row number, that is, the position of the current row in the adapter, // convertview: Specifies the row of viewview getview (INT position, view convertview, viewgroup parent );

Now we need to override the getview method to embed group tags in the list.
The Group tag is also one of the List data items and is painted by one row, but it is inconsistent with the UI of other data items. Therefore, we need to prepare two sets of data item layout templates:
Data item template group_list_item.xml:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: padding = "5dip"> <! -- Image and text --> <! -- Just put an image and beautify it. --> <imageview Android: src = "@ drawable/list_icon" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <textview Android: Id = "@ + ID/group_list_item_text" Android: layout_width = "wrap_content" Android: layout_height = "fill_parent" Android: paddingleft = "5dip" Android: gravity = "center_vertical"/> </linearlayout>

Tag item template group_list_item_tag.xml:

 
<! -- Only text, but the height of the shop, the background color is set to 555555 gray --> <? XML version = "1.0" encoding = "UTF-8"?> <Linkearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Background = "#555555" Android: paddingleft = "10dip"> <textview Android: Id = "@ + ID/group_list_item_text" Android: layout_width = "wrap_content" Android: layout_height = "20dip" Android: textcolor = "# ffffff" Android: gravity = "center_vertical"/> </linearlayout>

Okay. Now we apply the two templates to the getview method:

@ Override public view getview (INT position, view convertview, viewgroup parent) {view = convertview; // If (listtag. contains (getitem (position) {// if it is a tag item view = layoutinflater. from (getcontext ()). inflate (R. layout. group_list_item_tag, null);} else {// otherwise, the data item is view = layoutinflater. from (getcontext ()). inflate (R. layout. group_list_item, null);} // display name textview = (textview) view. findviewbyid (R. id. group_list_item_text); textview. settext (getitem (position); // return the overwritten view return view ;}

4. Disable tag Item Response events:
The isenable () method is provided in the baseadapter of the parent class of arrayadapter. Let's look at this method:

// By default, if this method is not a delimiter, true is returned. // The separator is unselected and does not have any click events. // you do not want to change the position item as a separator, if you want to, false is returned. Otherwise, truepublic Boolean isenabled (INT position) is returned)

This method is used to disable the response events of tag items. The specific implementation is as follows:

 
@ Override public Boolean isenabled (INT position) {If (listtag. Contains (getitem (position) {return false;} return Super. isenabled (position );}

Now the label item will not have any touch effect, just like a piece of dead board.

5. CompleteCode:
The code for the entire activity and adapter is as follows:

Public class grouplistactivity extends activity {private grouplistadapter adapter = NULL; private listview = NULL; private list <string> List = new arraylist <string> (); private list <string> listtag = new arraylist <string> (); @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. group_list_activity); setdata (); adapter = new grouplistadapter (this, list, listtag); listview = (listview) findviewbyid (R. id. group_list); listview. setadapter (adapter);} public void setdata () {list. add ("A"); listtag. add ("A"); For (INT I = 0; I <3; I ++) {list. add ("Avatar" + I);} List. add ("B"); listtag. add ("B"); For (INT I = 0; I <3; I ++) {list. add ("bit Storm" + I);} List. add ("C"); listtag. add ("C"); For (INT I = 0; I <30; I ++) {list. add ("Charlie Fengyun" + I) ;}} Private Static class grouplistadapter extends arrayadapter <string> {private list <string> listtag = NULL; Public grouplistadapter (context, list <string> objects, list <string> tags) {super (context, 0, objects); this. listtag = tags;} @ override public Boolean isenabled (INT position) {If (listtag. contains (getitem (position) {return false;} return Super. isenabled (position) ;}@ override public view getview (INT position, view convertview, viewgroup parent) {view = convertview; If (listtag. contains (getitem (position) {view = layoutinflater. from (getcontext ()). inflate (R. layout. group_list_item_tag, null);} else {view = layoutinflater. from (getcontext ()). inflate (R. layout. group_list_item, null);} textview = (textview) view. findviewbyid (R. id. group_list_item_text); textview. settext (getitem (position); Return view ;}}}

6. Final effect:

Related Article

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.