Use of expandablelistview in Android

Source: Internet
Author: User


Expandablelistview is a drop-down list control that can be implemented in Android. The specific implementation method is as follows:

First, define an expandablelistview in the XML file of layout.

 

  1. <Linearlayout
  2. Android: Id = "@ + ID/linearlayout"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Androidrientation = "vertical"
  6. >
  7. <Expandablelistview
  8. Android: Id = "@ + ID/expandablelistview"
  9. Android: layout_width = "fill_parent"
  10. Android: layout_height = "wrap_content"
  11. />
  12. </Linearlayout>

 

 

Define two lists to store strings in group/child controls.

 

  1. Private list <string> grouparray;
  2. Private list <string> childarray;

 

 

Initialize the two lists and insert some data.

 

  1. Grouparray = new arraylist <string> ();
  2. Childarray = new arraylist <list <string> ();
  3. Grouparray. Add ("first line ");
  4. Grouparray. Add ("second line ");
  5. List <string> temparray = new arraylist <string> ();
  6. Temparray. Add ("first ");
  7. Temparray. Add ("second ");
  8. Temparray. Add ("Article 3 ");
  9. For (INT Index = 0; index <grouparray. Size (); ++ index)
  10. {
  11. Childarray. Add (temparray );
  12. }

 

Define the adapter of expandablelistview

 

  1. // Expandablelistview Adapter
  2. Public class expandableadapter extends baseexpandablelistadapter
  3. {
  4. Activity activity;
  5. Public expandableadapter (Activity)
  6. {
  7. Activity =;
  8. }
  9. Public object getchild (INT groupposition, int childposition)
  10. {
  11. Return childarray. Get (groupposition). Get (childposition );
  12. }
  13. Public long getchildid (INT groupposition, int childposition)
  14. {
  15. Return childposition;
  16. }
  17. Public int getchildrencount (INT groupposition)
  18. {
  19. Return childarray. Get (groupposition). Size ();
  20. }
  21. Public View getchildview (INT groupposition, int childposition,
  22. Boolean islastchild, view convertview, viewgroup parent)
  23. {
  24. String string = childarray. Get (groupposition). Get (childposition );
  25. Return getgenericview (string );
  26. }
  27. // Group method stub
  28. Public object getgroup (INT groupposition)
  29. {
  30. Return grouparray. Get (groupposition );
  31. }
  32. Public int getgroupcount ()
  33. {
  34. Return grouparray. Size ();
  35. }
  36. Public long getgroupid (INT groupposition)
  37. {
  38. Return groupposition;
  39. }
  40. Public View getgroupview (INT groupposition, Boolean isexpanded,
  41. View convertview, viewgroup parent)
  42. {
  43. String string = grouparray. Get (groupposition );
  44. Return getgenericview (string );
  45. }
  46. // View stub to create group/Children's View
  47. Public textview getgenericview (string)
  48. {
  49. // Layout parameters for the expandablelistview
  50. Abslistview. layoutparams = new abslistview. layoutparams (
  51. Viewgroup. layoutparams. fill_parent, 64 );
  52. Textview text = new textview (activity );
  53. Text. setlayoutparams (layoutparams );
  54. // Center the text vertically
  55. Text. setgravity (gravity. center_vertical | gravity. Left );
  56. // Set the text starting position
  57. Text. setpadding (36, 0, 0, 0 );
  58. Text. settext (string );
  59. Return text;
  60. }
  61. Public Boolean hasstableids ()
  62. {
  63. Return false;
  64. }
  65. Public Boolean ischildselectable (INT groupposition, int childposition)
  66. {
  67. Return true;
  68. }
  69. }

 

Finally, add the adapter to the defined expandablelistview.

 

  1. Expandablelistview = (expandablelistview) findviewbyid (R. Id. expandablelistview );
  2. Expandablelistview. setadapter (New expandableadapter (main. This ));

 

Running is visible ~~~

 

 

 

 

 

 

 

Expandablelistview)

Let's take a look at the effect first.

 

 

Source code download: http://files.cnblogs.com/salam/WidgetDemo.rar

 

  

Expandablelistview is an accordion in Android, and I think it works quite well.

1. Introduction to expandablelistview

A vertical scroll shows the views of two levels (child, group) list items. The list items are from expandablelistadapter. The Group can be expanded separately.

1. Important Methods

Expandgroup (INT grouppos): opens a group in the group list view,

Setselectedgroup (INT groupposition): sets the group to be selected.

Setselectedchild (INT groupposition, int childposition, Boolean shouldexpandgroup): Set to select the specified sub-item.

Getpackedpositiongroup (long packedposition): returns the selected group.

Getpackedpositionforchild (INT groupposition, int childposition): returns the selected subitem.

Getpackedpositiontype (long packedposition): return the type of the selected item (child, Group)

Isgroupexpanded (INT groupposition): determines whether the group is expanded.

2. Code:

Expandablelistcontextmenuinfo menuinfo = (expandablelistcontextmenuinfo) item. getmenuinfo ();
String title((textview)menuinfo.tar getview). gettext (). tostring ();
Int type = expandablelistview. getpackedpositiontype (menuinfo. packedposition );

If (type = expandablelistview. packed_position_type_child ){
Int grouppos = expandablelistview. getpackedpositiongroup (menuinfo. packedposition );
Int childpos = expandablelistview. getpackedpositionchild (menuinfo. packedposition );

Ii. expandablelistadapter

An interface that links basic data to an expandablelistview. The implementation of this interface will provide data for accessing child (classified by groups) and instantiate child and group.

1. Important Methods

Getchildid (INT groupposition, int childposition) obtains data related to the child given in the given group.

Getchildrencount (INT groupposition) returns the number of children in the specified group.

2. Code

Public class myexpandablelistadapter extends baseexpandablelistadapter {
// Sample data set. Children [I] contains the Children (string []) for groups [I].
Public String [] groups = {"my friends", "Xinjiang Students", "Relatives", "colleagues "};
Public String [] [] children = {
{"Hu Zhenglin", "Zhang Junfeng", "Wang Zhijun", "two persons "},
{"Li xiuting", "Cai Qiao", "Don't go high", "Yu Yin "},
{"Spread new", "Zhang aiming "},
{"Ma Chao", "Si Daoguang "}
};

Public object getchild (INT groupposition, int childposition ){
Return children [groupposition] [childposition];
}

Public long getchildid (INT groupposition, int childposition ){
Return childposition;
}

Public int getchildrencount (INT groupposition ){
Return children [groupposition]. length;
}

Public textview getgenericview (){
// Layout parameters for the expandablelistview
Abslistview. layoutparams Lp = new abslistview. layoutparams (
Viewgroup. layoutparams. match_parent, 64 );

Textview = new textview (expandablelistdemo. This );
Textview. setlayoutparams (LP );
// Center the text vertically
Textview. setgravity (gravity. center_vertical | gravity. Left );
// Set the text starting position
Textview. setpadding (36, 0, 0, 0 );
Return textview;
}

Public View getchildview (INT groupposition, int childposition, Boolean islastchild,
View convertview, viewgroup parent ){
Textview = getgenericview ();
Textview. settext (getchild (groupposition, childposition). tostring ());
Return textview;
}

Public object getgroup (INT groupposition ){
Return groups [groupposition];
}

Public int getgroupcount (){
Return groups. length;
}

Public long getgroupid (INT groupposition ){
Return groupposition;
}

Public View getgroupview (INT groupposition, Boolean isexpanded, view convertview,
Viewgroup parent ){
Textview = getgenericview ();
Textview. settext (getgroup (groupposition). tostring ());
Return textview;
}

Public Boolean ischildselectable (INT groupposition, int childposition ){
Return true;
}

Public Boolean hasstableids (){
Return true;
}

}

 

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.