Android ExpandableListView简单例子

jopen 8年前

   android中常常要用到ListView,有时也要用到ExpandableListView,如在手机设置中,对于分类有很好的效果,会用ListView的人一定会用ExpandableListView,因为
 ExpandableListView extends ListView的,下面来看个简单的例子
 
 运行效果图:
 

Android ExpandableListView简单例子

<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"      tools:context=".MainActivity" >        <ExpandableListView          android:id="@+id/expendlist"          android:layout_width="match_parent"          android:layout_height="wrap_content" >      </ExpandableListView>     </RelativeLayout>

 //扩展ListView的头List布局文件   <?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" >      <TextView     android:id="@+id/txt"     android:layout_width="wrap_content"     android:layout_height="wrap_content" />      <ImageView     android:id="@+id/img"     android:layout_width="wrap_content"     android:layout_height="wrap_content" />   </LinearLayout>

 //扩展ListView的子List布局文件   <?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:padding="10.0dp" >      <ImageView     android:id="@+id/img"     android:layout_width="20.0dp"     android:layout_height="20.0dp" />      <TextView     android:id="@+id/txt"     android:layout_width="wrap_content"     android:layout_height="wrap_content" />     </LinearLayout>

package com.example.test;     import java.util.ArrayList;   import java.util.List;     import android.app.Activity;   import android.content.Context;   import android.os.Bundle;   import android.view.View;   import android.view.ViewGroup;   import android.widget.BaseExpandableListAdapter;   import android.widget.ExpandableListView;   import android.widget.ImageView;   import android.widget.TextView;     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;     @Override   protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);      //随便一堆测试数据    group_list = new ArrayList<String>();    group_list.add("A");    group_list.add("B");    group_list.add("C");      item_list = new ArrayList<List<String>>();    item_list.add(group_list);    item_list.add(group_list);    item_list.add(group_list);      List<Integer> tmp_list = new ArrayList<Integer>();    tmp_list.add(R.drawable.ic_launcher);    tmp_list.add(R.drawable.ic_launcher);    tmp_list.add(R.drawable.ic_launcher);      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);    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();    }      @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);    }      @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));     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;   }  }