Android Learning Note 13--expandablelistview

Source: Internet
Author: User

Expandablelistview==> list components that can be expanded

= = "

Expandablelistview is a subclass of the ListView that extends the list items in the app into groups, with multiple list items in each group;

The usage of Expandablelistview is very similar to the ListView, except that the list items shown should be provided by Expandablelistadapter;

Additional properties supported by Expandablelistview:

Android:childdivider Specifies the separator bar between each sub-list item within each group
Android:childindicator Drawable object displayed next to a child list item
Android:groupindicator Display the Drawable object next to the group list item

Instance:

Layout file = = "<linearlayout xmlns:android=" http://schemas.android.com/apk/res/android "android:layout_width=" Fill_ Parent "android:layout_height=" fill_parent "android:orientation=" vertical "> <expandablelistview and Roid:id= "@+id/list" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Android:ba Ckground= "#ffffff" android:cachecolorhint= "#00000000" android:listselector= "#00000000" > </expandab lelistview></linearlayout> implementation Code = = "Package Com.example.myexpandablelistview;import android.app.Activity; Import Android.graphics.color;import android.os.bundle;import Android.view.gravity;import Android.view.Menu;import Android.view.view;import Android.view.viewgroup;import Android.view.window;import Android.widget.AbsListView; Import Android.widget.baseexpandablelistadapter;import Android.widget.expandablelistadapter;import Android.widget.expandablelistview;import Android.widget.ExpandableListView.OnChildClickListener;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.TextView;import Android.widget.toast;public class Mainactivity extends activity{//set picture of group view private int[] Logos = new int[]{R.drawable.on E, R.drawable.two, R.drawable.three};//Set the display text of the group View private string[] Generalstypes = new string[]{"One", "one", "three"}; Child view display text private string[][] Generals = new string[][]{{"Watermelon", "cherry", "Strawberry"},{"grape", "pear", "Green apple"},{"banana", "orange", "Mango"}};/ /Child View picture public int[][] Generallogos = new int[][]{{R.drawable.one, R.drawable.one, R.drawable.one},{r.drawable.two, R.dr Awable.two, R.drawable.two,},{r.drawable.three, R.drawable.three, R.drawable.three}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_ TITLE); Setcontentview (R.layout.activity_main); final Expandablelistadapter adapter = new Baseexpandablelistadapter () {//define yourself a method for obtaining text information TextView Gettextview () {@SuppressWarnings ("Deprecation ") Abslistview.layoutparams LP = new Abslistview.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, 64); TextView TextView = new TextView (mainactivity.this); textview.setlayoutparams (LP); Textview.setgravity ( gravity.center_vertical); textview.setpadding (0, 0, 0); textview.settextsize; Textview.settextcolor ( Color.Black); return textView;} Override each method in Expandablelistadapter @overridepublic int getgroupcount () {return generalstypes.length;} @Overridepublic Object getgroup (int groupposition) {return generalstypes[groupposition];} @Overridepublic long getgroupid (int groupposition) {return groupposition;} @Overridepublic int getchildrencount (int groupposition) {return generals[groupposition].length;} @Overridepublic Object getchild (int groupposition, int childposition) {return generals[groupposition][childposition];} @Overridepublic long Getchildid (int groupposition, int childposition) {return childposition;} @Overridepublic Boolean hasstableids () {return true;} @Overridepublic View Getgroupview (intGroupposition, Boolean isexpanded, View convertview,viewgroup parent) {LinearLayout LL = new LinearLayout ( Mainactivity.this); ll.setorientation (0); ImageView logo = new ImageView (mainactivity.this); Logo.setimageresource ( Logos[groupposition]); logo.setpadding (0, 0, 0); Ll.addview (logo); TextView TextView = Gettextview (); Textview.settextcolor (Color.Black); Textview.settext (Getgroup (groupposition). ToString ()); Ll.addview (TextView); return ll;} @Overridepublic View getchildview (int groupposition, int childposition, Boolean islastchild,view Convertview, ViewGroup Parent) {LinearLayout LL = new LinearLayout (mainactivity.this); ll.setorientation (0); ImageView Generallogo = new ImageView (Mainactivity.this); Generallogo.setimageresource (Generallogos[groupposition][childposition]); Ll.addview (Generallogo); TextView TextView = Gettextview () textview.settext (Getchild (Groupposition, childposition). toString ()); Ll.addview ( TextView); return ll;} @Overridepublic boolean ischildselectable (int groupposition, int childPosition) {return true;}}; Expandablelistview Expandablelistview = (expandablelistview) Findviewbyid (r.id.list); expandableListView.setAdapter (adapter);//Set Item click Listener Expandablelistview.setonchildclicklistener (New Onchildclicklistener () {@Overridepublic Boolean Onchildclick (expandablelistview parent, View v, int groupposition,int childposition, long id) {Toast.maketext ( Mainactivity.this, "you clicked" + Adapter.getchild (groupposition, childposition), Toast.length_short). Show (); return false;});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

Implementation results:

Android Learning Note 13--expandablelistview

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.