Android ExpandableListView and androidlistview

Source: Internet
Author: User

Android ExpandableListView and androidlistview

Click Show show to show the effect first:


Start, 1. First create an XML display main interface:

Main_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"  ><ExpandableListView        android:id ="@+id/expandableListView"          android:layout_width ="match_parent"          android:layout_height ="wrap_content"       /></LinearLayout>

2. Enter main_activity to start display:

// Define two lists to control the String in Group and Child; private List <String> groupArray; // private List of Group lists <List <String> childArray; // sublist private ExpandableListView expandableListView_one;

Initialize data:

// Initialize the data private void initdate () {addInfo ("language", new String [] {"Oracle", "Java", "Linux", "Jquery "}); addInfo ("men's needs", new String [] {"money", "career", "power", "Women", "House", "car ", "ball"});} // Add data to the list to private void addInfo (String group, String [] child) {groupArray. add (group); // add it to the external rent List <String> childItem = new ArrayList <String> (); for (int index = 0; index <child. length; index ++) {childItem. add (child [index]);} childArray. add (childItem );}


After initialization, you can perform adapter adaptation:

 expandableListView_one.setAdapter(new ExpandableListViewaAdapter(MainActivity.this));
 class ExpandableListViewaAdapter extends BaseExpandableListAdapter {        Activity activity;         public  ExpandableListViewaAdapter(Activity a)              {                  activity = a;              }         /*-----------------Child */        @Override        public Object getChild(int groupPosition, int childPosition) {            // TODO Auto-generated method stub            return childArray.get(groupPosition).get(childPosition);        }         @Override        public long getChildId(int groupPosition, int childPosition) {            // TODO Auto-generated method stub            return childPosition;        }         @Override        public View getChildView(int groupPosition, int childPosition,                boolean isLastChild, View convertView, ViewGroup parent) {                         String string =childArray.get(groupPosition).get(childPosition);                         return getGenericView(string);        }         @Override        public int getChildrenCount(int groupPosition) {            // TODO Auto-generated method stub            return childArray.get(groupPosition).size();        }       /* ----------------------------Group */        @Override        public Object getGroup(int groupPosition) {            // TODO Auto-generated method stub            return getGroup(groupPosition);        }         @Override        public int getGroupCount() {            // TODO Auto-generated method stub            return groupArray.size();        }         @Override        public long getGroupId(int groupPosition) {            // TODO Auto-generated method stub            return groupPosition;        }         @Override        public View getGroupView(int groupPosition, boolean isExpanded,                View convertView, ViewGroup parent) {                        String   string=groupArray.get(groupPosition);           return getGenericView(string);        }         @Override        public boolean hasStableIds() {            // TODO Auto-generated method stub            return false;        }         @Override        public boolean isChildSelectable(int groupPosition, int childPosition)         {            // TODO Auto-generated method stub            return true;        }                 private TextView  getGenericView(String string )         {              AbsListView.LayoutParams  layoutParams =new AbsListView.LayoutParams(                    ViewGroup.LayoutParams.MATCH_PARENT,                    ViewGroup.LayoutParams.WRAP_CONTENT);                             TextView  textView =new TextView(activity);              textView.setLayoutParams(layoutParams);                             textView.setGravity(Gravity.CENTER_VERTICAL |Gravity.LEFT);                             textView.setPadding(40, 0, 0, 0);              textView.setText(string);              return textView;         }    }



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.