ExpandableListView: 可展开的ListView
ListView 对于Android开发者来说是再熟悉不过的了,不过ListView的功能也有限。当需要展示第二层级数据时,使用ExpandableListView会更方便,下面是两个实例:
(一).
public class MainActivity extends ExpandableListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 这里不用setContentView()去加载布局,因为继承了类ExpandableListActivity。 如果需要加载自定义布局,则不需继承类ExpandableListActivity, 而在自定义布局文件里添加ExpandableListView等控件 */ //创建主List,用于存放第一层级数据 List<Map<String, String>> groupData = new ArrayList<Map<String, String>>(); //创建子List,用于存放第二层级数据 List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>(); //向主List、子List中添加数据 for (int i = 0; i < 5; i++) { //向主List中添加数据 Map<String, String> groupMap = new HashMap<String, String>(); groupMap.put("group", "group " + i); groupData.add(groupMap); //向子List中添加数据 List<Map<String, String>> children = new ArrayList<Map<String, String>>(); for (int j = 0; j < 3; j++) { Map<String, String> childMap = new HashMap<String, String>(); childMap.put("child", "child " + j); children.add(childMap); } childData.add(children); } //利用SimpleExpandableListAdapter类构建设配器,可类比SimpleAdapter. ExpandableListAdapter mAdapter = new SimpleExpandableListAdapter(this, groupData, //第一层级数据 android.R.layout.simple_expandable_list_item_1, //第一层级子布局,可自定义 new String[]{"group"}, //键值 new int[]{android.R.id.text1}, //第一层级子布局中的显示内容的控件Id childData, //第二层级数据 android.R.layout.simple_expandable_list_item_2, //第二层级子布局,可自定义 new String[]{"child"}, //键值 new int[]{android.R.id.text1} //第二层级子布局中的显示内容的控件Id ); setListAdapter(mAdapter); } }效果图如下:
*以上只是简单地显示字符串,当然也可以自定义更复杂的布局,具体可参考SimpleAdapter
(二).
public class MainActivity1 extends Activity { ExpandableListView expandableListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //与上一个例子不同的是这里加载了自定义布局 setContentView(R.layout.activity_main); expandableListView = (ExpandableListView) findViewById(R.id.expandableListView); MyAdapter madapter = new MyAdapter(); expandableListView.setAdapter(madapter); } //自定义一个继承BaseExpandableListAdapter的设配器类,可类比BaseAdapter public class MyAdapter extends BaseExpandableListAdapter{ TextView textView; //groupData为第一层级的数据,childData为第二层级的数据 public String [] groupData = {"name 1","name 2","name 3","name 4"}; public String [][] childData = { {"number 1_1","number 1_2"}, {"number 2_1"}, {"number 3_1","number 3_2","number 3_3"}, {"number 4_1","number 4_2"} }; @Override public int getGroupCount() { return groupData.length; } @Override public int getChildrenCount(int groupPosition) { return childData[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return groupData[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return childData[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { return 0; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item,null); textView = (TextView) view.findViewById(R.id.textView); textView.setText(groupData[groupPosition]); return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item,null); textView = (TextView) view.findViewById(R.id.textView); textView.setText(childData[groupPosition][childPosition]); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } }效果图如下:
* 以上只是简单地显示字符串,当然也可以自定义更复杂的布局,具体可参考BaseAdapter
**第一次写博客,记录自己所学,若有不足之处,望指出。