5.2 ExpandableListView的用法

ExpandableListView的用法

发表于2年前(2014-03-04 15:09)   阅读( 25915) | 评论( 2)  7人收藏此文章, 我要收藏
4

3月19日,深圳源创会火热报名中,期待您的参与>>>>>   


   ExpandableListView组件是android中一个比较常用的组件,当点击一个父item的时候可以将它的子item显示出来,像手机QQ中的好友列表就是实现的类型效果。使用ExpandableListView组件的关键就是设置它的adapter,这个adapter必须继承BaseExpandbaleListAdapter类,所以实现运用ExpandableListView的核心就是学会继承这个BaseExpanableListAdapter类。

 下面是一个小demo:

activvity_main.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
< 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/main_expandablelistview"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"  />
 
</ RelativeLayout >


layout_parent.xml:(父item运用的样式)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? 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 = "vertical"  >
 
     < TextView
         android:id = "@+id/parent_textview"
         android:layout_width = "match_parent"
         android:layout_height = "50dp"
         android:layout_gravity = "center"
         android:gravity = "center"  />
 
</ LinearLayout >


   layout_children.xml:(子item运用的样式)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? 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 = "vertical"  >
 
     < TextView
         android:id = "@+id/second_textview"
         android:layout_width = "match_parent"
         android:layout_height = "40dp"
         android:layout_gravity = "center"
         android:gravity = "center"  />
 
</ LinearLayout >

MainActivity.java:

?
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
133
134
135
136
137
138
139
140
141
142
143
144
package  com.example.android_expandablelistview;
 
import  java.util.ArrayList;
import  java.util.HashMap;
import  java.util.List;
import  java.util.Map;
 
import  android.app.Activity;
import  android.content.Context;
import  android.os.Bundle;
import  android.view.LayoutInflater;
import  android.view.View;
import  android.view.ViewGroup;
import  android.widget.BaseExpandableListAdapter;
import  android.widget.ExpandableListView;
import  android.widget.TextView;
 
public  class  MainActivity  extends  Activity {
 
     ExpandableListView mainlistview =  null ;
     List<String> parent =  null ;
     Map<String, List<String>> map =  null ;
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         mainlistview = (ExpandableListView)  this
                 .findViewById(R.id.main_expandablelistview);
 
         initData();
         mainlistview.setAdapter( new  MyAdapter());
     }
 
     // 初始化数据
     public  void  initData() {
         parent =  new  ArrayList<String>();
         parent.add( "parent1" );
         parent.add( "parent2" );
         parent.add( "parent3" );
 
         map =  new  HashMap<String, List<String>>();
 
         List<String> list1 =  new  ArrayList<String>();
         list1.add( "child1-1" );
         list1.add( "child1-2" );
         list1.add( "child1-3" );
         map.put( "parent1" , list1);
 
         List<String> list2 =  new  ArrayList<String>();
         list2.add( "child2-1" );
         list2.add( "child2-2" );
         list2.add( "child2-3" );
         map.put( "parent2" , list2);
 
         List<String> list3 =  new  ArrayList<String>();
         list3.add( "child3-1" );
         list3.add( "child3-2" );
         list3.add( "child3-3" );
         map.put( "parent3" , list3);
 
     }
 
     class  MyAdapter  extends  BaseExpandableListAdapter {
       
         //得到子item需要关联的数据
         @Override
         public  Object getChild( int  groupPosition,  int  childPosition) {
             String key = parent.get(groupPosition);
             return  (map.get(key).get(childPosition));
         }
 
         //得到子item的ID
         @Override
         public  long  getChildId( int  groupPosition,  int  childPosition) {
             return  childPosition;
         }
 
         //设置子item的组件
         @Override
         public  View getChildView( int  groupPosition,  int  childPosition,
                 boolean  isLastChild, View convertView, ViewGroup parent) {
             String key = MainActivity. this .parent.get(groupPosition);
             String info = map.get(key).get(childPosition);
             if  (convertView ==  null ) {
                 LayoutInflater inflater = (LayoutInflater) MainActivity. this
                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 convertView = inflater.inflate(R.layout.layout_children,  null );
             }
             TextView tv = (TextView) convertView
                     .findViewById(R.id.second_textview);
             tv.setText(info);
             return  tv;
         }
 
         //获取当前父item下的子item的个数
         @Override
         public  int  getChildrenCount( int  groupPosition) {
             String key = parent.get(groupPosition);
             int  size=map.get(key).size();
             return  size;
         }
       //获取当前父item的数据
         @Override
         public  Object getGroup( int  groupPosition) {
             return  parent.get(groupPosition);
         }
 
         @Override
         public  int  getGroupCount() {
             return  parent.size();
         }
 
         @Override
         public  long  getGroupId( int  groupPosition) {
             return  groupPosition;
         }
        //设置父item组件
         @Override
         public  View getGroupView( int  groupPosition,  boolean  isExpanded,
                 View convertView, ViewGroup parent) {
             if  (convertView ==  null ) {
                 LayoutInflater inflater = (LayoutInflater) MainActivity. this
                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 convertView = inflater.inflate(R.layout.layout_parent,  null );
             }
             TextView tv = (TextView) convertView
                     .findViewById(R.id.parent_textview);
             tv.setText(MainActivity. this .parent.get(groupPosition));
             return  tv;
         }
 
         @Override
         public  boolean  hasStableIds() {
             return  true ;
         }
 
         @Override
         public  boolean  isChildSelectable( int  groupPosition,  int  childPosition) {
             return  true ;
         }
 
     }
}


最后的实现结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是关于 Android ExpandableListView 的使用方法: 1. 在 layout 文件中添加 ExpandableListView 组件。例如: ```xml <ExpandableListView android:id="@+id/my_expandable_listview" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 创建一个 ExpandableListAdapter 类,继承自 BaseExpandableListAdapter,实现其中的方法。例如: ```java public class MyExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; private List<String> mGroupData; private List<List<String>> mChildData; public MyExpandableListAdapter(Context context, List<String> groupData, List<List<String>> childData) { mContext = context; mGroupData = groupData; mChildData = childData; } @Override public int getGroupCount() { return mGroupData.size(); } @Override public int getChildrenCount(int groupPosition) { return mChildData.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return mGroupData.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return mChildData.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) { if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.group_item_layout, parent, false); } TextView groupTextView = convertView.findViewById(R.id.group_textview); groupTextView.setText(mGroupData.get(groupPosition)); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.child_item_layout, parent, false); } TextView childTextView = convertView.findViewById(R.id.child_textview); childTextView.setText(mChildData.get(groupPosition).get(childPosition)); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 3. 在 Activity 或 Fragment 中,初始化 ExpandableListView,并设置适配器。例如: ```java ExpandableListView myExpandableListView = findViewById(R.id.my_expandable_listview); MyExpandableListAdapter myExpandableListAdapter = new MyExpandableListAdapter(this, groupData, childData); myExpandableListView.setAdapter(myExpandableListAdapter); ``` 4. (可选)设置 ExpandableListView 的监听器,例如监听组展开/收起事件: ```java myExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { // 当组展开时的操作 } }); myExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { // 当组收起时的操作 } }); ``` 这样就完成了 ExpandableListView 的基本使用。需要注意的是,ExpandableListView 中的组和子项都需要自己定义布局,例如在上面的适配器中,分别使用了 group_item_layout 和 child_item_layout 两个布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值