Expandablelistview是可展开的listview,通过点击一层title获取对应title下面的content,着重点是ExpandableListViewaAdapter里面各个方法的使用。ExpandableListViewaAdapter中包括父、子 listview的count、id、view等,因此理解好ExpandableListViewaAdapter了,就可以很好使用Expandablelistview了。
下面我把效果图以及源码贴下来供大家阅读:
首先是activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="demoone.yizooo.com.expandablelistviewdemo_02.MainActivity"> <ExpandableListView android:id="@+id/expendlist" android:layout_width="match_parent" android:layout_height="wrap_content"></ExpandableListView></RelativeLayout>其次是expendlist_group.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#ffffff"> <!--扩展ListView的头List布局文件--> <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:orientation="horizontal" android:gravity="center_vertical"> <TextView android:id="@+id/txt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="title" android:textSize="14sp" android:textColor="#333333" android:layout_marginLeft="10dp"/> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/right" android:layout_marginRight="10dp"/> </LinearLayout></LinearLayout>其次是expendlist_item<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="#eeeeee" > <!--扩展ListView的子List布局文件--> <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:orientation="horizontal" android:gravity="center_vertical"> <TextView android:id="@+id/txt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="13sp" android:textColor="#333333" android:text="Name" android:layout_marginLeft="10dp"/> <ImageView android:id="@+id/img" android:layout_width="20.0dp" android:layout_height="20.0dp" android:src="@drawable/duigou" android:layout_marginRight="10dp"/> </LinearLayout></LinearLayout>最后是MainActivity
public class MainActivity extends Activity { PRivate ExpandableListView expandableListView; private List<String> group_list;// private List<List<String>> item_list; private List<List<Integer>> item_list2; private HashMap<String, List<String>> map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //title group_list = new ArrayList<String>(); group_list.add("总经理"); group_list.add("监管"); group_list.add("财务");//// //item// item_list = new ArrayList<List<String>>();// item_list.add(group_list);// item_list.add(group_list);// item_list.add(group_list); map = new HashMap<String, List<String>>(); List<String> list1 = new ArrayList<String>(); list1.add("Name_01"); map.put("总经理", list1); List<String> list2 = new ArrayList<String>(); list2.add("Name_02"); map.put("监管", list2); List<String> list3 = new ArrayList<String>(); list3.add("Name_03"); list3.add("Name_03"); list3.add("Name_03"); map.put("财务", list3); List<Integer> tmp_list = new ArrayList<Integer>(); tmp_list.add(R.drawable.duigou); tmp_list.add(R.drawable.duigou); tmp_list.add(R.drawable.duigou); item_list2 = new ArrayList<List<Integer>>(); item_list2.add(tmp_list); item_list2.add(tmp_list); item_list2.add(tmp_list); expandableListView = (ExpandableListView) findViewById(R.id.expendlist); //设置 属性 GroupIndicator 去掉默认向下的箭头 expandableListView.setGroupIndicator(null); expandableListView.setAdapter(new MyExpandableListViewAdapter(this)); } //用过ListView的人一定很熟悉,只不过这里是BaseExpandableListAdapter class MyExpandableListViewAdapter extends BaseExpandableListAdapter { private Context context; public MyExpandableListViewAdapter(Context context) { this.context = context; } @Override public int getGroupCount() { return group_list.size(); } @Override public int getChildrenCount(int groupPosition) { //return item_list.get(groupPosition).size(); String key = group_list.get(groupPosition); int size = map.get(key).size(); return size; } @Override public Object getGroup(int groupPosition) { return group_list.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { //return item_list.get(groupPosition).get(childPosition); return childPosition; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupHolder groupHolder = null; if (convertView == null) { convertView = (View) getLayoutInflater().from(context).inflate(R.layout.expendlist_group, null); groupHolder = new GroupHolder(); groupHolder.txt = (TextView) convertView.findViewById(R.id.txt); // groupHolder.img = (ImageView) convertView // .findViewById(R.id.img); convertView.setTag(groupHolder); } else { groupHolder = (GroupHolder) convertView.getTag(); } groupHolder.txt.setText(group_list.get(groupPosition)); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ItemHolder itemHolder = null; if (convertView == null) { convertView = (View) getLayoutInflater().from(context).inflate(R.layout.expendlist_item, null); itemHolder = new ItemHolder(); itemHolder.txt = (TextView) convertView.findViewById(R.id.txt); itemHolder.img = (ImageView) convertView.findViewById(R.id.img); convertView.setTag(itemHolder); } else { itemHolder = (ItemHolder) convertView.getTag(); } //itemHolder.txt.setText(item_list.get(groupPosition).get(childPosition)); String key = MainActivity.this.group_list.get(groupPosition); itemHolder.txt.setText(map.get(key).get(childPosition)); itemHolder.img.setBackgroundResource(item_list2.get(groupPosition).get( childPosition)); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } class GroupHolder { public TextView txt; public ImageView img; } class ItemHolder { public ImageView img; public TextView txt; }}最后附上源码下载:
http://download.csdn.net/detail/laurachen93/9748157
新闻热点
疑难解答