Android expandablelistview adds menus for group and child

Source: Internet
Author: User

There are two ways to add different menus for the group and child of expandablelistview:

1. showdialog

This method adds a tag for the view through the adapter and obtains the tag of the view in onitemlongclicklistener of the expandablelistview to determine whether it is group or child. Then, a different menu dialog box is displayed.

CodeAs follows:

  1. @ Override
  2. PublicView getchildview (Final IntArg0,Final IntArg1,BooleanArg2, view,
  3. Viewgroup arg4 ){
  4. If(View =Null){
  5. View = Inflater. Inflate (R. layout. team_layout_main_list_item,Null);
  6. }
  7. Textview = (textview) view. findviewbyid (R. Id. team_main_list_username );
  8. Imageview = (imageview) view. findviewbyid (R. Id. team_main_list_head );
  9. Imageview. setimageresource (prortaitutils. conversionidtores (userinfo) getchild (arg0, arg1). getportrait ()));
  10. View. settag (R. Id. team_singlechat_id_send, arg0 );
  11. View. settag (R. Id. team_singlechat_id_close, arg1 );// Note 1
  12. Textview. settext (userinfo) getchild (arg0, arg1). GetUserName ());
  13. Textview. settextcolor (color. Black );
  14. View. setbackgroundcolor (color. RGB (247,243,247));
  15. View. setbackgroundresource (R. drawable. team_item_bg_selector );
  16. ReturnView;
  17. }

 

  1. @ Override
  2. PublicView getgroupview (IntArg0,BooleanArg1, view groupview, viewgroup arg3 ){
  3. If(Groupview =Null){
  4. Groupview = Inflater. Inflate (R. layout. team_layout_main_list_group,Null);
  5. }
  6. Textview grouptextview = (textview) groupview. findviewbyid (R. Id. team_main_list_teamname );
  7. Imageview groupimageview = (imageview) groupview. findviewbyid (R. Id. team_main_list_into );
  8. Groupimageview. setimageresource (R. drawable. team_1_1 );
  9. If(Arg1 ){
  10. Groupimageview. setimageresource (R. drawable. team_sources 2 );
  11. }
  12. Groupview. settag (R. Id. team_singlechat_id_send, arg0 );
  13. Groupview. settag (R. Id. team_singlechat_id_close ,-1);// Note 2
  14. Grouptextview. settext (getgroup (arg0). tostring ());
  15. Grouptextview. settextcolor (color. Black );
  16. ReturnGroupview;
  17. }

 

  1. // Long-pressed entries, including group and child
  2. Onlinelist. setonitemlongclicklistener (NewOnitemlongclicklistener (){
  3. @ Override
  4. Public BooleanOnitemlongclick (adapterview <?> Arg0,FinalView arg1,
  5. IntArg2,LongArg3 ){
  6. Longclicklistiteam (arg1 );
  7. Return True;
  8. }
  9. });
  10. /** 
  11. * Long press an entry in the list 
  12. */
  13. Private VoidLongclicklistiteam (FinalView arg1 ){
  14. Final IntGrouppos = (integer) arg1.gettag (R. Id. team_singlechat_id_send );
  15. Final IntChildpos = (integer) arg1.gettag (R. Id. team_singlechat_id_close );
  16. Log. I (tag,"Grouppos :"+ Grouppos +", Childpos :"+ Childpos );
  17. If(Childpos =-1){// If the value of the Child location is-1, the operation group
  18. Log. I (tag,"Operate group components");
  19. Updateteam (arg1 );
  20. }Else{
  21. Log. I (tag,"Operate child components");// Otherwise, operate child
  22. FinalList <userinfo> Users = getteamuser (grouppos );
  23. // Determine whether the long-pressed button is your own. If it is your own, a dialog box is displayed. If it is your own, view the information.
  24. If(Dicqconstant. defaultmac. equalsignorecase (users. Get (childpos). getusermac ())){
  25. Intent it =NewIntent (teammainactivity.This, Userinfoactivity.Class);
  26. It. putextra ("Ismyself",True);
  27. Teammainactivity.This. Startactivity (it );
  28. }Else{
  29. Builder =NewAlertdialog. Builder (teammainactivity.This);
  30. Builder. settitle ("Operation user");
  31. Builder. setitems (NewString [] {"View materials","View Sharing","Move to group","Permission settings"},NewDialoginterface. onclicklistener (){
  32. Public VoidOnclick (dialoginterface dialog,IntWhich ){
  33. If(Which =0){
  34. Intent it =NewIntent (teammainactivity.This, Userinfoactivity.Class);
  35. It. putextra ("Ismyself",False);
  36. It. putextra ("Userinfo", Users. Get (childpos ));
  37. Teammainactivity.This. Startactivity (it );
  38. }Else If(Which =1){
  39. Intent it1 =NewIntent (teammainactivity.This, File1_activity.Class);
  40. It1.putextra ("Userinfo", Users. Get (childpos ));
  41. Teammainactivity.This. Startactivity (it1 );
  42. }Else If(Which =2){
  43. Updatechild (grouppos, childpos );
  44. }Else If(Which =3){
  45. Updateuserprivilege (grouppos, childpos );
  46. }
  47. }
  48. }). Show ();
  49. }
  50. }
  51. }

The second method is contextmenu.

In this wayExpandablelistcontextmenuinfo to determine whether it is group or child, and then create different menus

The Code is as follows:

  1. @ Override
  2. Public void oncreatecontextmenu (contextmenu menu, view V, contextmenuinfo menuinfo ){
  3. Expandablelistview. expandablelistcontextmenuinfo = (expandablelistview. expandablelistcontextmenuinfo) menuinfo;
  4. Int type = expandablelistview. getpackedpositiontype (info. packedposition );
  5. Int group = expandablelistview. getpackedpositiongroup (info. packedposition );
  6. If (type = expandablelistview. packed_position_type_child & group! = 0 ){
  7. Menu. setheadertitle ("sample menu ");
  8. Menu. Add (0, 0, 0, R. String. expandable_list_sample_action );
  9. }
  10. }

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.