Qq friend group for Android (expandablelistview ).

Source: Internet
Author: User

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?
  1. Public class exlistview extends activity {
  2. Private Static final string group_text = "group_text"; // key of map members
  3. Private Static final string child_text1 = "child_text1"; // the first key of the group member Map
  4. Private Static final string child_text2 = "child_text2"; // The second key of the group member Map
  5. List <Map <string, string> groupdata = new arraylist <Map <string, string> (); // large group members
  6. List <list <Map <string, string >>> childdata = new arraylist <list <Map <string, string >>>(); // group member
  7. Exadapter adapter;
  8. Expandablelistview exlist; // scalable listview
  9. @ Override
  10. Public void oncreate (bundle savedinstancestate ){
  11. Super. oncreate (savedinstancestate );
  12. Setcontentview (R. layout. Main );
  13. // Add data to a large group
  14. For (INT I = 1; I <6; I ++ ){
  15. Map <string, string> curgroupmap = new hashmap <string, string> ();
  16. Groupdata. Add (curgroupmap );
  17. Curgroupmap. Put (group_text, "" + I + "");
  18. List <Map <string, string> Children = new arraylist <Map <string, string> ();
  19. For (Int J = 1; j <6; j ++ ){
  20. Map <string, string> curchildmap = new hashmap <string, string> ();
  21. Children. Add (curchildmap );
  22. Curchildmap. Put (child_text1, "th" + J + "group ");
  23. Curchildmap. Put (child_text2, "th" + J + "Group Signature ");
  24. }
  25. Childdata. Add (children );
  26. }
  27. Adapter = new exadapter (exlistview. This );
  28. Exlist = (expandablelistview) findviewbyid (R. Id. List );
  29. Exlist. setadapter (adapter );
  30. Exlist. setgroupindicator (null); // do not set the big group indicator icon, because we have set
  31. Exlist. setdivider (null); // you can specify the image that can be stretched.
  32. }
  33. // The key code is this extensible listview adapter.
  34. Class exadapter extends baseexpandablelistadapter {
  35. Context context;
  36. Public exadapter (context ){
  37. Super ();
  38. This. Context = context;
  39. }
  40. // Obtain the view of the group members
  41. Public View getgroupview (INT groupposition, Boolean isexpanded,
  42. View convertview, viewgroup parent ){
  43. View view = convertview;
  44. If (view = NULL ){
  45. Layoutinflater Inflater = (layoutinflater) getsystemservice (context. layout_inflater_service );
  46. View = Inflater. Inflate (R. layout. member_listview, null );
  47. }
  48. Textview Title = (textview) view. findviewbyid (R. Id. content_001 );
  49. Title. settext (getgroup (groupposition). tostring (); // you can specify a group name.
  50. Imageview image = (imageview) view. findviewbyid (R. Id. tubiao); // whether to expand the arrow icon of a large group
  51. If (isexpanded) // when a large group is expanded
  52. Image. setbackgroundresource (R. drawable. btn_browser2 );
  53. Else // when merging large groups
  54. Image. setbackgroundresource (R. drawable. btn_browser );
  55. Return view;
  56. }
  57. // Obtain the group ID.
  58. Public long getgroupid (INT groupposition ){
  59. Return groupposition;
  60. }
  61. // Obtain the group name
  62. Public object getgroup (INT groupposition ){
  63. Return groupdata. Get (groupposition). Get (group_text). tostring ();
  64. }
  65. // Obtain the total number of group members
  66. Public int getgroupcount (){
  67. Return groupdata. Size ();
  68. }
  69. // Get the view of the group member
  70. Public View getchildview (INT groupposition, int childposition,
  71. Boolean islastchild, view convertview, viewgroup parent ){
  72. View view = convertview;
  73. If (view = NULL ){
  74. Layoutinflater Inflater = layoutinflater. From (context );
  75. View = Inflater. Inflate (R. layout. member_childitem, null );
  76. }
  77. Final textview Title = (textview) view
  78. . Findviewbyid (R. Id. child_text );
  79. Title. settext (childdata. Get (groupposition). Get (childposition)
  80. . Get (child_text1). tostring (); // large title
  81. Final textview title2 = (textview) view
  82. . Findviewbyid (R. Id. child_text2 );
  83. Title2.settext (childdata. Get (groupposition). Get (childposition)
  84. . Get (child_text2). tostring (); // subtitle
  85. Return view;
  86. }
  87. // Obtain the group member ID
  88. Public long getchildid (INT groupposition, int childposition ){
  89. Return childposition;
  90. }
  91. // Obtain the group member name
  92. Public object getchild (INT groupposition, int childposition ){
  93. Return childdata. Get (groupposition). Get (childposition). Get (child_text1)
  94. . Tostring ();
  95. }
  96. // Obtain the number of group members
  97. Public int getchildrencount (INT groupposition ){
  98. Return childdata. Get (groupposition). Size ();
  99. }
  100. /**
  101. * Indicates whether the child and group IDs are stable when SS changes to
  102. * Underlying data.
  103. * Indicates whether the underlying data is stably changed by the big group and group ID.
  104. * @ Return whether or not the same ID always refers to the same object
  105. * @ See adapter # hasstableids ()
  106. */
  107. Public Boolean hasstableids (){
  108. Return true;
  109. }
  110. // Obtain whether the group member is selected
  111. Public Boolean ischildselectable (INT groupposition, int childposition ){
  112. Return true;
  113. }
  114. }
  115. }

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.