The previous articles introduced Listview, but in actual development, multi-layer Listview is often used to display data, such as display of friends in qq, so this article will take a look at ExpandableListview, the basic idea is roughly the same as that of Listview, so it is more convenient to use.
Implementation:
Program code:
Layout file:
Activity_main.xml:
The layout used by group and child (of course different la s can also be used). The layout is relatively simple, with only one Textview:
List_item:
MainActivity:
Package com. listviewdemo_expandablelistview; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. widget. expandableListView; public class MainActivity extends Activity {private ExpandableListView listView; private List
Group; private List
> Child; private MyAdapter adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ExpandableListView) findViewById (R. id. expandableListView);/*** initialize data */initData (); adapter = new MyAdapter (this, group, child); listView. setAdapter (adapter);} private void initData () {group = new ArrayList
(); Child = new ArrayList
> (); AddInfo ("Beijing", new String [] {"Chaoyang", "Haidian", "Dongcheng District", "Xicheng district"}); addInfo ("Hebei ", new String [] {"Handan", "Shijiazhuang", "Xingtai"}); addInfo ("Guangdong", new String [] {"Guangzhou", "Shenzhen ", "Zhuhai"});}/*** add data information * @ param g * @ param c */private void addInfo (String g, String [] c) {group. add (g); List
List = new ArrayList
(); For (int I = 0; I <c. length; I ++) {list. add (c [I]);} child. add (list );}}
MyAdapter:
Package com. listviewdemo_expandablelistview; import java. util. list; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseExpandableListAdapter; import android. widget. textView;/*** expandableListView adapter **/public class MyAdapter extends BaseExpandableListAdapter {private Context context; private List
Group; private List
> Child; public MyAdapter (Context context, List
Group, List
> Child) {this. context = context; this. group = group; this. child = child;} @ Overridepublic int getGroupCount () {return group. size () ;}@ Overridepublic int getChildrenCount (int groupPosition) {return child. size () ;}@ Overridepublic Object getGroup (int groupPosition) {return group. get (groupPosition) ;}@ Overridepublic Object getChild (int groupPosition, int childPosition) {return child. get (childPosition ). get (childPosition) ;}@ Overridepublic long getGroupId (int groupPosition) {return groupPosition ;}@ Overridepublic long getChildId (int groupPosition, int childPosition) {return childPosition ;} @ Overridepublic boolean hasStableIds () {return false;}/*** display: group */@ Overridepublic View getGroupView (int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {ViewHolder holder; if (convertView = null) {convertView = LayoutInflater. from (context ). inflate (R. layout. list_item, null); holder = new ViewHolder (); holder. textView = (TextView) convertView. findViewById (R. id. textView); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} holder. textView. setText (group. get (groupPosition); holder. textView. setTextSize (25); holder. textView. setPadding (36, 10, 0, 10); return convertView;}/*** display: child */@ Overridepublic View getChildView (int groupPosition, int childPosition, boolean isLastChild, view convertView, ViewGroup parent) {ViewHolder holder; if (convertView = null) {convertView = LayoutInflater. from (context ). inflate (R. layout. list_item, null); holder = new ViewHolder (); holder. textView = (TextView) convertView. findViewById (R. id. textView); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} holder. textView. setText (child. get (groupPosition ). get (childPosition); holder. textView. setTextSize (20); holder. textView. setPadding (72, 10, 0, 10); return convertView;} class ViewHolder {TextView textView;} @ Overridepublic boolean isChildSelectable (int groupPosition, int childPosition) {return false ;}}
Now the link added to the CSDN blog must be reviewed before it can be published, so the source code can be found in the resources I uploaded.