In project development, we may have encountered nested listview in listview, but Google suggests that we do not do this, so they have written an expandablelistview class, which inherits listview, you can implement the nested listview effect in the listview. Well, let's not talk about it. First:
Main Code:
[HTML]
View plaincopyprint?
- Public class exlistview extends activity {
- Private Static final string group_text = "group_text"; // key of map members
- Private Static final string child_text1 = "child_text1"; // the first key of the group member Map
- Private Static final string child_text2 = "child_text2"; // The second key of the group member Map
- List <Map <string, string> groupdata = new arraylist <Map <string, string> (); // large group members
- List <list <Map <string, string >>> childdata = new arraylist <list <Map <string, string >>>(); // group member
- Exadapter adapter;
- Expandablelistview exlist; // scalable listview
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- // Add data to a large group
- For (INT I = 1; I <6; I ++ ){
- Map <string, string> curgroupmap = new hashmap <string, string> ();
- Groupdata. Add (curgroupmap );
- Curgroupmap. Put (group_text, "" + I + "");
- List <Map <string, string> Children = new arraylist <Map <string, string> ();
- For (Int J = 1; j <6; j ++ ){
- Map <string, string> curchildmap = new hashmap <string, string> ();
- Children. Add (curchildmap );
- Curchildmap. Put (child_text1, "th" + J + "group ");
- Curchildmap. Put (child_text2, "th" + J + "Group Signature ");
- }
- Childdata. Add (children );
- }
- Adapter = new exadapter (exlistview. This );
- Exlist = (expandablelistview) findviewbyid (R. Id. List );
- Exlist. setadapter (adapter );
- Exlist. setgroupindicator (null); // do not set the big group indicator icon, because we have set
- Exlist. setdivider (null); // you can specify the image that can be stretched.
- }
- // The key code is this extensible listview adapter.
- Class exadapter extends baseexpandablelistadapter {
- Context context;
- Public exadapter (context ){
- Super ();
- This. Context = context;
- }
- // Obtain the view of the group members
- Public View getgroupview (INT groupposition, Boolean isexpanded,
- View convertview, viewgroup parent ){
- View view = convertview;
- If (view = NULL ){
- Layoutinflater Inflater = (layoutinflater) getsystemservice (context. layout_inflater_service );
- View = Inflater. Inflate (R. layout. member_listview, null );
- }
- Textview Title = (textview) view. findviewbyid (R. Id. content_001 );
- Title. settext (getgroup (groupposition). tostring (); // you can specify a group name.
- Imageview image = (imageview) view. findviewbyid (R. Id. tubiao); // whether to expand the arrow icon of a large group
- If (isexpanded) // when a large group is expanded
- Image. setbackgroundresource (R. drawable. btn_browser2 );
- Else // when merging large groups
- Image. setbackgroundresource (R. drawable. btn_browser );
- Return view;
- }
- // Obtain the group ID.
- Public long getgroupid (INT groupposition ){
- Return groupposition;
- }
- // Obtain the group name
- Public object getgroup (INT groupposition ){
- Return groupdata. Get (groupposition). Get (group_text). tostring ();
- }
- // Obtain the total number of group members
- Public int getgroupcount (){
- Return groupdata. Size ();
- }
- // Get the view of the group member
- Public View getchildview (INT groupposition, int childposition,
- Boolean islastchild, view convertview, viewgroup parent ){
- View view = convertview;
- If (view = NULL ){
- Layoutinflater Inflater = layoutinflater. From (context );
- View = Inflater. Inflate (R. layout. member_childitem, null );
- }
- Final textview Title = (textview) view
- . Findviewbyid (R. Id. child_text );
- Title. settext (childdata. Get (groupposition). Get (childposition)
- . Get (child_text1). tostring (); // large title
- Final textview title2 = (textview) view
- . Findviewbyid (R. Id. child_text2 );
- Title2.settext (childdata. Get (groupposition). Get (childposition)
- . Get (child_text2). tostring (); // subtitle
- Return view;
- }
- // Obtain the group member ID
- Public long getchildid (INT groupposition, int childposition ){
- Return childposition;
- }
- // Obtain the group member name
- Public object getchild (INT groupposition, int childposition ){
- Return childdata. Get (groupposition). Get (childposition). Get (child_text1)
- . Tostring ();
- }
- // Obtain the number of group members
- Public int getchildrencount (INT groupposition ){
- Return childdata. Get (groupposition). Size ();
- }
- /**
- * Indicates whether the child and group IDs are stable when SS changes to
- * Underlying data.
- * Indicates whether the underlying data is stably changed by the big group and group ID.
- * @ Return whether or not the same ID always refers to the same object
- * @ See adapter # hasstableids ()
- */
- Public Boolean hasstableids (){
- Return true;
- }
- // Obtain whether the group member is selected
- Public Boolean ischildselectable (INT groupposition, int childposition ){
- Return true;
- }
- }
- }