Android Development notes-expandable listview with icons (by rewriting the adapter)

Source: Internet
Author: User

Today, I would like to share with you some of my experiences on rewriting the adapter in expandable listview yesterday.

Effect: the background is rounded. Each line displays text and images (more complex if necessary ). First:

First, let's look at the implementation of the rounded corner. In fact, this is relatively simple. You only need to add an XML file under the drawble file:

 

<Br/> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <shape xmlns: Android = "http://schemas.android.com/apk/res/android"> </P> <p> <gradient Android: endcolor = "# ffffff" Android: startcolor = "# ffffff"> </P> <p> <corners Android: bottomleftradius = "6dip" Android: bottomrightradius = "6dip" Android: topleftradius = "6dip" Android: toprightradius = "6dip"> </P> <p> </corners> </gradient> </shape> <! -- Four corners --> <br/>

For some of the attributes in the description, refer ~~~ (This is also suitable for other controls. You only need to add it in the background attribute)

 

 

 <Br/> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <shape xmlns: Android = "http://schemas.android.com/apk/res/android"> </P> <p> <! -- Rounded corner --> <br/> <corners Android: radius = "9dp" Android: topleftradius = "2dp" Android: toprightradius = "2dp" Android: bottomleftradius = "2dp" Android: bottomrightradius = "2dp"> <! -- Set the radius of the rounded corner --> </P> <p> <! -- Gradient --> <br/> <gradient Android: startcolor = "@ Android: color/White" Android: centercolor = "@ Android: color/Black" Android: endcolor = "@ Android: color/Black" Android: uselevel = "true" Android: angle = "45" Android: TYPE = "RADIAL" Android: centerx = "0" Android: centery = "0" Android: gradientradius = "90"> </P> <p> <! -- Interval --> <br/> <padding Android: Left = "2dp" Android: Top = "2dp" Android: Right = "2dp" Android: bottom = "2dp"> <! -- Interval in each direction --> </P> <p> <! -- Size --> <br/> <size Android: width = "50dp" Android: Height = "50dp"> <! -- Width and height --> </P> <p> <! -- Fill --> <br/> <solid Android: color = "@ Android: color/White"> <! -- Fill color --> </P> <p> <! -- Stroke --> <br/> <stroke Android: width = "2dp" Android: color = "@ Android: color/Black" Android: dashwidth = "1dp" Android: dashgap = "2dp"> </P> <p> </stroke> </solid> </size> </padding> </gradient> </corners> </shape>

Then, we have to start doing expandable listview... Like other controls, you need to add controls to the layout file. Here, it is worth noting that the assignment of the property Android: ID

Android: Id = "@ + ID/Android: List" otherwise, an error will be reported ~~~ The layout file is very simple, so I will not post it out...

Then I declare and define something in the activity... The source code is provided below:

 <Br/> package Jenn. expandable; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. map; </P> <p> Import android. OS. bundle; <br/> Import android. app. activity; <br/> Import android. app. expandablelistactivity; <br/> Import android. view. menu; <br/> Import android. widget. expandablelistview; </P> <p>/** <br/> * this activity is about a expandable list view with icons and corner background <br/> * @ author Jenn <br/> * <br/> */<br/> public class expandableactivity extends expandablelistactivity {<br/>/** expandable list view */<br/> private expandablelistview; </P> <p>/** array list for parent */<br/> Public static arraylist <Map <String object = ""> marrlistparent = new arraylist <Map <string object = "" >> (); <br/>/** array list for all the children */<br/> Public static arraylist <Map <String object = ""> marrlistchild = new arraylist <arraylist <Map <String object = "" >>> (); <br/>/** array list for first child */<br/> Public static arraylist <Map <String object = ""> marrlistfirchild = new arraylist <Map <String object = ""> (); <br/>/** array list for second child */<br/> Public static arraylist <Map <String object = ""> marrlistsecchild = new arraylist <Map <String object = ""> (); <br/>/** array list for third child */<br/> Public static arraylist <Map <String object = ""> marrlistthichild = new arraylist <Map <String object = ""> (); </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. activity_expandable); </P> <p> mexpandablelistview = (expandablelistview) findviewbyid (Android. r. id. list); </P> <p> inite (); </P> <p>/** my own expandable list view */<br/> myexpandablelistadapter expan_adapter = new myexpandablelistadapter (<br/> This); <br/> mexpandablelistview. setadapter (expan_adapter); </P> <p> // todo: you can add some listener here if necessary <br/>}</P> <p>/** <br/> * set the data for the expandable list <br/> * /<br/> Public void inite () {<br/> // --------------- for the parent ------------------ <br/> Map <String object = ""> map_parent1 = new hashmap <String object = ""> (); <br/> map_parent1.put ("title", "group1"); <br/> map_parent1.put ("title_icon", R. drawable. tb_xia); </P> <p> Map <String object = ""> map_parent2 = new hashmap <String object = ""> (); <br/> map_parent2.put ("title", "Group2"); <br/> map_parent2.put ("title_icon", R. drawable. tb_xia); </P> <p> Map <String object = ""> map_parent3 = new hashmap <String object = ""> (); <br/> map_parent3.put ("title", "Group3"); <br/> map_parent3.put ("title_icon", R. drawable. tb_xia); </P> <p> marrlistparent. add (map_parent1); <br/> marrlistparent. add (map_parent2); <br/> marrlistparent. add (map_parent3 ); </P> <p> // ---------------- for first child --------------- <br/> Map <String object = ""> map_fchild1 = new hashmap <String object = ""> (); <br/> map_fchild1.put ("icon", R. drawable. alerts); <br/> map_fchild1.put ("text", "alerts "); </P> <p> Map <String object = ""> map_fchild2 = new hashmap <String object = ""> (); <br/> map_fchild2.put ("icon ", r. drawable. blogger); <br/> map_fchild2.put ("text", "blogger "); </P> <p> Map <String object = ""> map_fchild3 = new hashmap <String object = ""> (); <br/> map_fchild3.put ("icon ", r. drawable. blogsearch); <br/> map_fchild3.put ("text", "blogsearch"); </P> <p> marrlistfirchild. add (map_fchild1); <br/> marrlistfirchild. add (map_fchild2); <br/> marrlistfirchild. add (map_fchild3 ); </P> <p> // --------------- for the second child --------------- <br/> Map <String object = ""> map_schild1 = new hashmap <String object = ""> (); <br/> map_schild1.put ("icon", R. drawable. alerts); <br/> map_schild1.put ("text", "alerts "); </P> <p> Map <String object = ""> map_schild2 = new hashmap <String object = ""> (); <br/> map_schild2.put ("icon ", r. drawable. blogger); <br/> map_schild2.put ("text", "blogger "); </P> <p> Map <String object = ""> map_schild3 = new hashmap <String object = ""> (); <br/> map_schild3.put ("icon ", r. drawable. blogsearch); <br/> map_schild3.put ("text", "blogsearch"); </P> <p> marrlistsecchild. add (map_schild1); <br/> marrlistsecchild. add (map_schild2); <br/> marrlistsecchild. add (map_schild3 ); </P> <p> // --------------- for the third child ------------------- <br/> Map <String object = ""> map_tchild1 = new hashmap <String object = ""> (); <br/> map_tchild1.put ("icon", R. drawable. alerts); <br/> map_tchild1.put ("text", "alerts "); </P> <p> Map <String object = ""> map_tchild2 = new hashmap <String object = ""> (); <br/> map_tchild2.put ("icon ", r. drawable. blogger); <br/> map_tchild2.put ("text", "blogger "); </P> <p> Map <String object = ""> map_tchild3 = new hashmap <String object = ""> (); <br/> map_tchild3.put ("icon ", r. drawable. blogsearch); <br/> map_tchild3.put ("text", "blogsearch"); </P> <p> marrlistthichild. add (map_tchild1); <br/> marrlistthichild. add (map_tchild2); <br/> marrlistthichild. add (map_tchild3); </P> <p> marrlistchild. add (marrlistfirchild); <br/> marrlistchild. add (marrlistsecchild); <br/> marrlistchild. add (marrlistthichild ); <br/>}</P> <p >}< br/> </string>> </string>> </string>> </string>

 

Of course, the most important thing is to rewrite the adapter... I will post it first and then explain the specific example...

 <Br/> package Jenn. expandable; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. map; <br/> Import java.util.zip. inflater; </P> <p> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. graphics. bitmap; <br/> Import android. view. layoutinflater; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. baseadapter; <br/> Import android. widget. baseexpandablelistadapter; <br/> Import android. widget. imageview; <br/> Import android. widget. textview; </P> <p> public class myexpandablelistadapter extends baseexpandablelistadapter {<br/> context = NULL; </P> <p>/** <br/> * constructor <br/> * @ Param context <br/> */<br/> Public myexpandablelistadapter (context) {<br/> This. context = context; <br/>}</P> <p>/** <br/> * get child through group position and child position <br/> */<br/> Public object getchild (INT groupposition, int childposition) {<br/> return expandableactivity. marrlistchild. get (groupposition ). get (childposition); <br/>}</P> <p> Public long getchildid (INT groupposition, int childposition) {<br/> return 0; <br/>}</P> <p> Public View getchildview (INT groupposition, int childposition, <br/> Boolean islastchild, view convertview, viewgroup parent) {<br/> If (convertview = NULL) {<br/> convertview = layoutinflater. from (context ). inflate (R. layout. expand_list_child, null); <br/>}< br/> imageview iv_phone = (imageview) convertview. findviewbyid (R. id. expandable_child_icon1); <br/> textview txt_number = (textview) convertview. findviewbyid (R. id. expandable_child_text); </P> <p> final map <String object = ""> child = expandableactivity. marrlistchild. get (groupposition ). get (childposition); </P> <p> int resid = (integer) expandableactivity. marrlistchild. get (groupposition ). get (childposition ). get ("icon"); <br/> iv_phone.setimageresource (resid); </P> <p> txt_number.settext (child. get ("text "). tostring (); </P> <p> return convertview; <br/>}</P> <p> Public int getchildrencount (INT groupposition) {<br/> return expandableactivity. marrlistchild. get (groupposition ). size (); <br/>}</P> <p> Public object getgroup (INT groupposition) {<br/> return expandableactivity. marrlistparent. get (groupposition); <br/>}</P> <p> Public int getgroupcount () {<br/> return expandableactivity. marrlistparent. size (); <br/>}</P> <p> Public long getgroupid (INT groupposition) {<br/> return 0; <br/>}</P> <p> Public View getgroupview (INT groupposition, Boolean isexpanded, <br/> View convertview, viewgroup parent) {<br/> If (convertview = NULL) {<br/> convertview = layoutinflater. from (context ). inflate (R. layout. expand_list_parent, null); <br/>}< br/> textview txt_title = (textview) convertview. findviewbyid (R. id. expandable_parent_text); <br/> imageview iv_icon = (imageview) convertview. findviewbyid (R. id. expandable_parent_icon); </P> <p> txt_title.settext (expandableactivity. marrlistparent. get (groupposition ). get ("title "). tostring (); </P> <p> int resid = (integer) expandableactivity. marrlistparent. get (groupposition ). get ("title_icon"); <br/> iv_icon.setimageresource (resid); </P> <p> return convertview; <br/>}</P> <p> Public Boolean hasstableids () {<br/> return false; <br/>}</P> <p> Public Boolean ischildselectable (INT groupposition, int childposition) {<br/> return true; <br/>}</P> <p >}< br/> </string>

1. Context object: This is used for inflate. Because parent and child have the same layout, layout needs to be obtained. Therefore, a context object is required, specific can look at the user's log: http://lzy3169421.blog.163.com/blog/static/113545277201111392857802/

2. The next step is the constructor and the rewrite method, which is not complex...

3. Two getview Methods: Child and group.

If (convertview = NULL ){
Convertview = layoutinflater. From (context). Inflate (R. layout. expand_list_child, null );
} Is to remove the child Layout

Then, I believe everyone can understand it through ID findview. It is rare to assign values. Since map is used to define data, how can it be changed to the string type and imageview?

Txt_title.settext (expandableactivity. marrlistparent. Get (groupposition). Get ("title"). tostring ());

Int resid = (integer) expandableactivity. marrlistparent. Get (groupposition). Get ("title_icon ");
Iv_icon.setimageresource (resid );

You must understand these two sentences, that is, converting map into string and imageview.

Group and child are similar...

 

PS: My demo is relatively simple, and the icon resources are limited. Because the icons of my item are the same, you will think it is very simple, in fact, by initializing data, you can add different icons or make the layout more complicated. Today, it is mainly to help you understand the logic...

We recommend that you do not blindly copy the code. Understanding is the key !!!

 

In addition, I will upload the project files to csdn. You can download the files if you need them. Thank you! Http://download.csdn.net/download/jenn_lian/4537069

You are also welcome to leave a message ~~~

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.