在項目開發中,也許我們遇到過ListView中嵌套ListView,但Google建議我們最好別這樣做,因此他們寫好了一個ExpandableListView類,他繼承ListView,可以實現ListView中嵌套ListView的效果,好了,廢話不多說,先上:
主代碼:
public class ExListView extends Activity {private static final String GROUP_TEXT = "group_text";//大群組成員Map的keyprivate static final String CHILD_TEXT1 = "child_text1";//小組成員Map的第一個keyprivate static final String CHILD_TEXT2 = "child_text2";//小組成員Map的第二個keyList<Map<String, String>> groupData = new ArrayList<Map<String, String>>();//大群組成員List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();//小組成員ExAdapter adapter;ExpandableListView exList;//可擴充的ListView@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//為大小組中添加資料for (int i = 1; i < 6; i++) {Map<String, String> curGroupMap = new HashMap<String, String>();groupData.add(curGroupMap);curGroupMap.put(GROUP_TEXT, "第" + i + "大組");List<Map<String, String>> children = new ArrayList<Map<String, String>>();for (int j = 1; j < 6; j++) {Map<String, String> curChildMap = new HashMap<String, String>();children.add(curChildMap);curChildMap.put(CHILD_TEXT1, "第" + j + "小組");curChildMap.put(CHILD_TEXT2, "第" + j + "小組簽名");}childData.add(children);}adapter = new ExAdapter(ExListView.this);exList = (ExpandableListView) findViewById(R.id.list);exList.setAdapter(adapter);exList.setGroupIndicator(null);//不設定大組指標表徵圖,因為我們自訂設定了exList.setDivider(null);//設定圖片可展開的}//關鍵代碼是這個可擴充的listView適配器class ExAdapter extends BaseExpandableListAdapter {Context context;public ExAdapter(Context context) {super();this.context = context;}//得到大群組成員的viewpublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {View view = convertView;if (view == null) {LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);view = inflater.inflate(R.layout.member_listview, null);}TextView title = (TextView) view.findViewById(R.id.content_001);title.setText(getGroup(groupPosition).toString());//設定大群組成員名稱ImageView image = (ImageView) view.findViewById(R.id.tubiao);//是否展開大組的箭頭表徵圖if (isExpanded)//大組展開時image.setBackgroundResource(R.drawable.btn_browser2);else//大組合并時image.setBackgroundResource(R.drawable.btn_browser);return view;}//得到大群組成員的idpublic long getGroupId(int groupPosition) {return groupPosition;}//得到大群組成員名稱public Object getGroup(int groupPosition) {return groupData.get(groupPosition).get(GROUP_TEXT).toString();}//得到大群組成員總數public int getGroupCount() {return groupData.size();}// 得到小組成員的viewpublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {View view = convertView;if (view == null) {LayoutInflater inflater = LayoutInflater.from(context);view = inflater.inflate(R.layout.member_childitem, null);}final TextView title = (TextView) view.findViewById(R.id.child_text);title.setText(childData.get(groupPosition).get(childPosition).get(CHILD_TEXT1).toString());//大標題final TextView title2 = (TextView) view.findViewById(R.id.child_text2);title2.setText(childData.get(groupPosition).get(childPosition).get(CHILD_TEXT2).toString());//小標題return view;}//得到小組成員idpublic long getChildId(int groupPosition, int childPosition) {return childPosition;}//得到小組成員的名稱public Object getChild(int groupPosition, int childPosition) {return childData.get(groupPosition).get(childPosition).get(CHILD_TEXT1).toString();}//得到小組成員的數量public int getChildrenCount(int groupPosition) {return childData.get(groupPosition).size();}/** * Indicates whether the child and group IDs are stable across changes to the * underlying data. * 表明大組和小組id是否穩定的更改底層資料。 * @return whether or not the same ID always refers to the same object * @see Adapter#hasStableIds() */public boolean hasStableIds() {return true;}//得到小組成員是否被選擇public boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}}