android的ExpandableListView

activity_main.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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"
     android:paddingBottom= "@dimen/activity_vertical_margin"
     android:paddingLeft= "@dimen/activity_horizontal_margin"
     android:paddingRight= "@dimen/activity_horizontal_margin"
     android:paddingTop= "@dimen/activity_vertical_margin"
     tools:context= "com.example.expandablelistview.MainActivity"  >
 
     <ExpandableListView 
         android:id= "@+id/ExpandableListView1_1"
         android:layout_width= "match_parent"
         android:layout_height= "wrap_content"
        android:layout_alignParentLeft= "true"
        android:layout_alignParentTop= "true"
        android:groupIndicator= "@null"
         
                 >
         
     </ExpandableListView>
 
</RelativeLayout>
<!--  android:groupIndicator= "@null" 去掉自带的箭头图标 -->

group_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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"  >
     
     <ImageView
         android:id= "@+id/imageViewgroup_1" 
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:src= "@drawable/ic_launcher"
         />
     
     <TextView 
         android:id= "@+id/textViewgroup_1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
       
         />
</LinearLayout>


child_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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"  >
     
     
      <ImageView
         android:id= "@+id/imageViewchild_1" 
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:src= "@drawable/ic_launcher"
         android:padding= "10dp"
         />
     
     <TextView 
         android:id= "@+id/textViewchild_1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "sdfds"
         android:padding= "10dp"
       
         />
</LinearLayout>

MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package  com.example.expandablelistview;
 
import  android.app.Activity;
import  android.os.Bundle;
import  android.view.Menu;
import  android.view.MenuItem;
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;
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         expandableListView=(ExpandableListView) findViewById(R.id.ExpandableListView1_1);
         expandableListView.setAdapter( new  MyExpandableListAdapter());
     }
 
     @Override
     public  boolean  onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
         return  true ;
     }
 
     @Override
     public  boolean  onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int  id = item.getItemId();
         if  (id == R.id.action_settings) {
             return  true ;
         }
         return  super .onOptionsItemSelected(item);
     }
     
     class  MyExpandableListAdapter  extends  BaseExpandableListAdapter{
         private  String[] skills =  new  String[]{
                 "WORD" "EXCEL" "EMAIL" "PPT"
         };
         private  String[][] groups =  new  String[][]{
                     { "文档编辑" "文档排版" "文档处理" "文档打印" },
                     { "表格编辑" "表格排版" "表格处理" "表格打印" },
                     { "收发邮件" "管理邮箱" "登录登出" "注册绑定" },
                     { "演示编辑" "演示排版" "演示处理" "演示打印" },
             };
         @Override
         public  int  getGroupCount() {
             // TODO Auto-generated method stub
             return  skills.length;
         }
         //二级列表的数量
         @Override
         public  int  getChildrenCount( int  groupPosition) {
             // TODO Auto-generated method stub
             return  groups[groupPosition].length;
         }
         //返回每一组的对象
         @Override
         public  Object getGroup( int  groupPosition) {
             // TODO Auto-generated method stub
             return  skills[groupPosition];
         }
         //返回每组中的列表项
         @Override
         public  Object getChild( int  groupPosition,  int  childPosition) {
             // TODO Auto-generated method stub
             return  groups[groupPosition][childPosition];
         }
 
         @Override
         public  long  getGroupId( int  groupPosition) {
             // TODO Auto-generated method stub
             return  groupPosition;
         }
 
         @Override
         public  long  getChildId( int  groupPosition,  int  childPosition) {
             // TODO Auto-generated method stub
             return  childPosition;
         }
 
         @Override
         public  boolean  hasStableIds() {
             // TODO Auto-generated method stub
             return  true ;
         }
 
         @Override
         public  View getGroupView( int  groupPosition,  boolean  isExpanded,
                 View convertView, ViewGroup parent) {
             // TODO Auto-generated method stub
             if (convertView== null ){
                 convertView=getLayoutInflater().inflate(R.layout.group_item,  null );
                 
             }
             ImageView imageView=(ImageView) convertView.findViewById(R.id.imageViewgroup_1);
             TextView textView=(TextView) convertView.findViewById(R.id.textViewgroup_1);
             imageView.setImageResource(R.drawable.ic_launcher);
             textView.setText(skills[groupPosition]);
             return  convertView;
         }
 
         @Override
         public  View getChildView( int  groupPosition,  int  childPosition,
                 boolean  isLastChild, View convertView, ViewGroup parent) {
             // TODO Auto-generated method stub
             if (convertView== null ){
                 convertView=getLayoutInflater().inflate(R.layout.child_item,  null );
                 ImageView imageView=(ImageView) convertView.findViewById(R.id.imageViewchild_1);
                 TextView textView=(TextView) convertView.findViewById(R.id.textViewchild_1);
                 imageView.setImageResource(R.drawable.ic_launcher);
                 textView.setText(groups[groupPosition][childPosition]);
             }
             return  convertView;
         }
 
         @Override
         public  boolean  isChildSelectable( int  groupPosition,  int  childPosition) {
             // TODO Auto-generated method stub
             return  true ;
         }
         
     }
}

wKioL1hQ_y3CFvIhAACn1k8Opfw784.png-wh_50



 本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1882709


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值