android列表,有时需要显示两级列表,使用Listview不能达到很好的效果,可以使用ExpandInfoAdapter很好的实现二级列表,并且能够自定义相应的列表。下面是我定义二级列表的实例:
其中的列表布局和相应的图片都是自定义的,实现代码如下:
public final static String NAME = "Name:"; public final static String PHONE = "Phone:"; public final static String SEX = "Sex:"; public List<String> group; public List<List<String>> child; ExpandInfoAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.list); initialOther(); addItemByValue("WWWW","051782214","man"); addItemByValue("ZZZZ","110","mal"); addItemByValue("DDDD","132","femal"); addItemByValue("FFFFF","11111","femal"); adapter = new ExpandInfoAdapter(this); expandableListView.setAdapter(adapter); } public void initialOther(){ group = new ArrayList<String>(); child = new ArrayList<List<String>>(); } public void addItemByValue(String n,String c1,String c2){ group.add(n); List<String> item = new ArrayList<String>(); item.add(NAME+n); item.add(PHONE+c1); item.add(SEX+c2); child.add(item); } public class ExpandInfoAdapter extends BaseExpandableListAdapter { Activity activity; public ExpandInfoAdapter(Activity a){ activity = a; } @Override public Object getChild(int arg0, int arg1) { return child.get(arg0).get(arg1); } @Override public long getChildId(int arg0, int arg1) { return arg1; } @Override public int getChildrenCount(int arg0) { return child.get(arg0).size(); } @Override public View getChildView(int arg0, int arg1, boolean arg2, View arg3, ViewGroup arg4) { View view = arg3; if (view == null) { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.listview, null); LinearLayout layout = (LinearLayout) view .findViewById(R.id.layout_013); } TextView title = (TextView) view.findViewById(R.id.content_001); title.setText(child.get(arg0).get(arg1)); return view; } @Override public Object getGroup(int arg0) { return group.get(arg0); } @Override public int getGroupCount() { return group.size(); } @Override public long getGroupId(int arg0) { return arg0; } @Override public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) { View view = arg2; if (view == null) { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.listview, null); LinearLayout layout = (LinearLayout) view .findViewById(R.id.layout_013); } ImageView imageView=(ImageView)view.findViewById(R.id.tubiao); if(arg1) { imageView.setBackgroundResource(R.drawable.tubiao_button1); }else{ imageView.setBackgroundResource(R.drawable.tubiao_button); } TextView title = (TextView) view.findViewById(R.id.content_001); title.setText(getGroup(arg0).toString()); return view; } public void onGroupExpanded(int groupPosition){ } public void onGroupCollapsed(int groupPosition){ } @Override public boolean isChildSelectable(int arg0, int arg1) { return true; } @Override public boolean hasStableIds() { return false; } }
源代码:https://easymorse-android.googlecode.com/svn/trunk/ExpandableListDemoActivity