<span style="font-size:24px;"> 活动界面 public class MainActivity extends Activity { private ExpandableListView el; // 一级列表的列表 private List<String> grouplist; // 二级标题的列表 private List<List<String>> itemlist; public void getdata() { grouplist = new ArrayList<String>(); grouplist.add("我的好友"); grouplist.add("我的家人"); grouplist.add("我的设备"); grouplist.add("同学"); grouplist.add("八维同事"); grouplist.add("八维市场部"); List<String> item = new ArrayList<String>(); item.add("好友1"); item.add("好友2"); List<String> item1 = new ArrayList<String>(); item1.add("家人1"); item1.add("家人2"); List<String> item2 = new ArrayList<String>(); item2.add("设备1"); item2.add("设备2"); List<String> item3 = new ArrayList<String>(); item3.add("同学1"); item3.add("同学2"); List<String> item4 = new ArrayList<String>(); item4.add("同事1"); item4.add("同事2"); List<String> item5 = new ArrayList<String>(); item5.add("晋中市场部"); item5.add("晋南市场部"); itemlist = new ArrayList<List<String>>(); itemlist.add(item); itemlist.add(item1); itemlist.add(item2); itemlist.add(item3); itemlist.add(item4); itemlist.add(item5); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getdata(); el = (ExpandableListView) findViewById(R.id.el); el.setAdapter(new Myad()); // 一级标题的点击事件 el.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { Toast.makeText(MainActivity.this, "第" + groupPosition + "组被点击", 0).show(); return false; } }); // 二级标题的点击事件 el.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity.this, itemlist.get(groupPosition).get(childPosition), 0) .show(); return false; } }); } class Myad implements ExpandableListAdapter { @Override public void registerDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public void unregisterDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public int getGroupCount() { // 返回一级标题的个数 return grouplist.size(); } @Override public int getChildrenCount(int groupPosition) { // 返回一级标题对应的二级标题的个数 return itemlist.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return null; } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return null; } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return 0; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return 0; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } // 得到一级标题的view @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.group_layout, null); TextView tv_group = (TextView) view.findViewById(R.id.tv_group); tv_group.setText(grouplist.get(groupPosition)); return view; } // 得到二级标题的view @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View vv = View .inflate(MainActivity.this, R.layout.group_item, null); TextView tv_item = (TextView) vv.findViewById(R.id.tv_item); tv_item.setText(itemlist.get(groupPosition).get(childPosition)); return vv; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } @Override public boolean areAllItemsEnabled() { // TODO Auto-generated method stub return false; } @Override public boolean isEmpty() { // TODO Auto-generated method stub return false; } @Override public void onGroupExpanded(int groupPosition) { // TODO Auto-generated method stub } @Override public void onGroupCollapsed(int groupPosition) { // TODO Auto-generated method stub } @Override public long getCombinedChildId(long groupId, long childId) { // TODO Auto-generated method stub return 0; } @Override public long getCombinedGroupId(long groupId) { // TODO Auto-generated method stub return 0; } } }</span>
主函数布局
<span style="font-size:24px;"><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <include android:id="@+id/include1" android:layout_width="wrap_content" android:layout_height="20dp" layout="@layout/xiaoyun" /> <ExpandableListView android:id="@+id/el" android:layout_width="match_parent" android:layout_height="400dp" android:layout_marginTop="50dp" > </ExpandableListView> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/include1" android:layout_alignTop="@+id/include1" android:layout_marginTop="22dp" android:text="好友分组" /> </RelativeLayout> </span><pre name="code" class="java"><span style="font-size:24px;">一级列表布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="4/17" /> <TextView android:id="@+id/tv_group" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="45dp" android:text="textview" /> </RelativeLayout> </span>二级列表布局
<span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imageView1" android:text="TextView" /> </RelativeLayout></span>