首页 > 代码库 > ExpandableListView的使用多级列表

ExpandableListView的使用多级列表

多级列表ExpandableListView 

扩展列表能够显示一个指示在每项显示项的当前状态(状态通常是一个扩展的组,组的孩子,或倒塌,最后一个孩子)。使用setchildindicator(drawable)setgroupindicator(drawable)(或相应的XML属性)来设置这些指标,一个默认的风格多级列表提供指标,将示给意见多级列表。布局android.r.layout.simple_expandable_list_item_1和android.r.layout.simple_expandable_list_item_2(应用simplecursortreeadapter)包含位置信息的首选指标。

效果图:

技术分享

 

 1 activity_main.xml
 2 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="#ffffff"
 6     android:orientation="vertical">
 7     <ExpandableListView
 8         android:id="@+id/expandableListView"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         android:divider="#E4E4E4"
12         android:dividerHeight="5dp">
13     </ExpandableListView>
14 </LinearLayout>
15 child_item.xml
16 <?xmlversion="1.0"encoding="utf-8"?>
17 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
18     android:layout_width="match_parent"
19     android:layout_height="match_parent"
20     android:orientation="vertical">
21     <TextView
22         android:id="@+id/textViews"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"/>
25     <View
26         android:layout_width="match_parent"
27         android:layout_height="0.5dp"
28         android:background="#ff0000"/>
29 </LinearLayout>
30 groud_item.xml
31 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
32     xmlns:tools="http://schemas.android.com/tools"
33     android:layout_width="match_parent"
34     android:layout_height="match_parent"
35    android:gravity="center_vertical"
36     android:orientation="horizontal">
37     <ImageView
38         android:id="@+id/imageView1"
39         android:layout_width="65dp"
40         android:layout_height="55dp"
41         android:layout_marginLeft="10dp"
42         android:scaleType="fitXY"
43         android:src="@drawable/l1"/>
44     <TextView
45         android:id="@+id/textView"
46         android:layout_width="wrap_content"
47         android:layout_height="wrap_content"
48         android:layout_marginLeft="10dp"
49         android:text="斯蒂芬"/>
50 </LinearLayout>

 

 1 public class MainActivity extends Activity {
 2     private ExpandableListView listView;
 3     private List<String> group;
 4     private List<List<String>> child;
 5     private MyAdapter adapter;
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_main);
10         
11         listView = (ExpandableListView) findViewById(R.id.expandableListView);
12         /**
13          * 初始化数据 
14          */
15         initData();
16         adapter = new MyAdapter(this,group,child);
17         listView.setAdapter(adapter);
18     
19     }
20     
21     
22     
23 
24     private void initData() {
25         group = new ArrayList<String>();
26         child = new ArrayList<List<String>>();
27         addInfo("笑傲江湖",new String[]{"东方不败","风清扬","令狐冲","岳不群"});
28         addInfo("天龙八部", new String[]{"乔峰","虚竹","段誉"});
29         addInfo("九阴真经", new String[]{"中神通","东邪","西毒","南帝","北丐"});
30     }
31     
32     /**
33      * 添加数据信息
34      * @param g
35      * @param c
36      */
37     private void addInfo(String g,String[] c) {
38         group.add(g);
39         List<String> list = new ArrayList<String>();
40         for (int i = 0; i < c.length; i++) {
41             list.add(c[i]);
42         }
43         child.add(list);
44     }
45 
46 }

 

  1 /**
  2  * expandableListView的用法同ListView
  3  *
  4  */
  5 public class MyAdapter extends BaseExpandableListAdapter {
  6     private Context context;
  7     private List<String> group;
  8     private List<List<String>> child;
  9     private int[] img={R.drawable.l2,R.drawable.l3,R.drawable.l1};
 10     public MyAdapter(Context context, List<String> group,
 11             List<List<String>> child) {
 12         this.context = context;
 13         this.group = group;
 14         this.child = child;
 15     }
 16 
 17     @Override
 18     public int getGroupCount() {
 19         return group.size();
 20     }
 21 
 22     @Override
 23     public int getChildrenCount(int groupPosition) {
 24         return child.get(groupPosition).size();
 25     }
 26 
 27     @Override
 28     public Object getGroup(int groupPosition) {
 29         return group.get(groupPosition);
 30     }
 31 
 32     @Override
 33     public Object getChild(int groupPosition, int childPosition) {
 34         return child.get(childPosition).get(childPosition);
 35     }
 36 
 37     @Override
 38     public long getGroupId(int groupPosition) {
 39         return groupPosition;
 40     }
 41 
 42     @Override
 43     public long getChildId(int groupPosition, int childPosition) {
 44         return childPosition;
 45     }
 46 
 47     @Override
 48     public boolean hasStableIds() {
 49         return false;
 50     }
 51     
 52     /**
 53     *
 54     * 获取显示指定组的视图对象
 55     *
 56     * @param groupPosition 组位置
 57     * @param isExpanded 该组是展开状态还是伸缩状态
 58     * @param convertView 重用已有的视图对象
 59     * @param parent 返回的视图对象始终依附于的视图组
 60     * @return
 61     * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View,
 62     *      android.view.ViewGroup)
 63     */
 64     @Override
 65     public View getGroupView(int groupPosition, boolean isExpanded,
 66             View convertView, ViewGroup parent) {
 67         ViewHolder holder;
 68         if (convertView == null) {
 69             convertView = LayoutInflater.from(context).inflate(
 70                     R.layout.groud_item, null);
 71             holder = new ViewHolder();
 72             holder.textView = (TextView) convertView
 73                     .findViewById(R.id.textView);
 74             holder.mImageView=(ImageView) convertView.findViewById(R.id.imageView1);
 75             convertView.setTag(holder);
 76         } else {
 77             holder = (ViewHolder) convertView.getTag();
 78         }
 79         holder.textView.setText(group.get(groupPosition));
 80         holder.textView.setTextSize(25);
 81         holder.textView.setPadding(36, 10, 0, 10);
 82         holder.mImageView.setImageResource(img[groupPosition]);
 83         return convertView;
 84 
 85     }
 86     
 87     /**
 88     *
 89     * 获取一个视图对象,显示指定组中的指定子元素数据。
 90     *
 91     * @param groupPosition 组位置
 92     * @param childPosition 子元素位置
 93     * @param isLastChild 子元素是否处于组中的最后一个
 94     * @param convertView 重用已有的视图(View)对象
 95     * @param parent 返回的视图(View)对象始终依附于的视图组
 96     * @return
 97     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View,
 98     *      android.view.ViewGroup)
 99     */
100     @Override
101     public View getChildView(int groupPosition, int childPosition,
102             boolean isLastChild, View convertView, ViewGroup parent) {
103         ViewHolder holder;
104         if (convertView == null) {
105             convertView = LayoutInflater.from(context).inflate(
106                     R.layout.child_item, null);
107             holder = new ViewHolder();
108             holder.textView = (TextView) convertView
109                     .findViewById(R.id.textViews);
110             convertView.setTag(holder);
111         } else {
112             holder = (ViewHolder) convertView.getTag();
113         }
114         holder.textView.setText(child.get(groupPosition).get(childPosition));
115         holder.textView.setTextSize(20);
116         holder.textView.setPadding(72, 10, 0, 10);
117         return convertView;
118     }
119 
120     
121 
122      /**
123     *
124     * 是否选中指定位置上的子元素。
125     *
126     * @param groupPosition
127     * @param childPosition
128     * @return
129     * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
130     */
131     @Override
132     public boolean isChildSelectable(int groupPosition, int childPosition) {
133         return false;
134     }
135 
136     
137     class ViewHolder {
138         ImageView mImageView;
139         TextView textView;
140     }
141 }

 

源码下载:

 

Eclipse下载:http://download.csdn.net/detail/dickyqie/9630549

AndroidStudio下载:https://github.com/DickyQie/ListViewExpandableListViewDelete/tree/expandablelistview

 

ExpandableListView的使用多级列表