android ExpandableListView详解(转载)

ExpandableListView是android中可以实现下拉list的一个控件,是一个垂直滚动的心事两个级别列表项手风琴试图,列表项是来自ExpandableListViewaAdapter,组可以单独展开。

重要方法:


1

2

3

4

5

6

7

8

9

10

11

12

expandGroup (int groupPos) ;//在分组列表视图中 展开一组,

setSelectedGroup (int groupPosition) ;//设置选择指定的组。

setSelectedChild (int groupPosition, int childPosition, boolean shouldExpandGroup);//设置选择指定的子项。

getPackedPositionGroup (long packedPosition);//返回所选择的组

getPackedPositionForChild (int groupPosition, int childPosition) ;//返回所选择的子项

getPackedPositionType (long packedPosition);//返回所选择项的类型(Child,Group)

isGroupExpanded (int groupPosition);//判断此组是否展开

expandableListView.setDivider();这个是设定每个Group之间的分割线。

expandableListView.setGroupIndicator();这个是设定每个Group之前的那个图标。

expandableListView.collapseGroup(int group); 将第group组收起

ExpandableListAdapter

一个接口,将基础数据链接到一个ExpandableListView。 此接口的实施将提供访问Child的数据(由组分类),并实例化的Child和Group。

1.重要方法

    getChildId (int groupPosition, int childPosition) 获取与在给定组给予孩子相关的数据。

    getChildrenCount (int groupPosition) 返回在指定Group的Child数目。

案例:

首先定义个一个布局文件expandablelistview.xml

?


1

2

3

4

5

6

7

8

9

10

11

12

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

     <ExpandableListView

        android:id ="@+id/expandableListView"

        android:layout_width ="fill_parent"

        android:layout_height ="wrap_content"

        >

     </ExpandableListView>

</LinearLayout>

?


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

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

package com.test;

import java.util.ArrayList;

import java.util.List;

import javax.security.auth.PrivateCredentialPermission;

import android.app.Activity;

import android.os.Bundle;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup;

import android.view.Window;

import android.widget.AbsListView;

import android.widget.BaseExpandableListAdapter;

import android.widget.ExpandableListView;

import android.widget.TextView;

public class ExpandableListViewDemo extends Activity {

    /** Called when the activity is first created. */

    

    //定义两个List用来控制Group和Child中的String;

    

    private  List<String>  groupArray;//组列表

    private  List<List<String>> childArray;//子列表

    private  ExpandableListView  expandableListView_one;

    

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

//        requestWindowFeature(Window.FEATURE_NO_TITLE);  //设置为无标题 

        setContentView(R.layout.expandablelistview);

        expandableListView_one =(ExpandableListView)findViewById(R.id.expandableListView);

        groupArray =new ArrayList<String>();

        childArray = new ArrayList<List<String>>();

       

        /*-第一季-*/

        initdate();

        expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this));

        

        /*-第二季-*/

//        groupArray.add("移动开发");

//        List<String> arrayList = new ArrayList<String>();

//        arrayList.add("Android");

//        arrayList.add("IOS");

//        arrayList.add("Windows Phone");

//        //组循环

//        for(int index=0;index<groupArray.size();++index)

//        {

//          childArray.add(arrayList);

//        }

//        expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this));

        

    }

    class ExpandableListViewaAdapter extends BaseExpandableListAdapter {

        Activity activity;

         public  ExpandableListViewaAdapter(Activity a) 

            

                activity = a; 

            

       /*-----------------Child */

        @Override

        public Object getChild(int groupPosition, int childPosition) {

            // TODO Auto-generated method stub

            return childArray.get(groupPosition).get(childPosition);

        }

        @Override

        public long getChildId(int groupPosition, int childPosition) {

            // TODO Auto-generated method stub

            return childPosition;

        }

        @Override

        public View getChildView(int groupPosition, int childPosition,

                boolean isLastChild, View convertView, ViewGroup parent) {

            

            String string =childArray.get(groupPosition).get(childPosition);

            

            return getGenericView(string);

        }

        @Override

        public int getChildrenCount(int groupPosition) {

            // TODO Auto-generated method stub

            return childArray.get(groupPosition).size();

        }

       /* ----------------------------Group */

        @Override

        public Object getGroup(int groupPosition) {

            // TODO Auto-generated method stub

            return getGroup(groupPosition);

        }

        @Override

        public int getGroupCount() {

            // TODO Auto-generated method stub

            return groupArray.size();

        }

        @Override

        public long getGroupId(int groupPosition) {

            // TODO Auto-generated method stub

            return groupPosition;

        }

        @Override

        public View getGroupView(int groupPosition, boolean isExpanded,

                View convertView, ViewGroup parent) {

            

           String   string=groupArray.get(groupPosition);

           return getGenericView(string);

        }

        @Override

        public boolean hasStableIds() {

            // TODO Auto-generated method stub

            return false;

        }

        @Override

        public boolean isChildSelectable(int groupPosition, int childPosition)

        {

            // TODO Auto-generated method stub

            return true;

        }

        

        private TextView  getGenericView(String string )

        {

              AbsListView.LayoutParams  layoutParams =new AbsListView.LayoutParams(

                    ViewGroup.LayoutParams.MATCH_PARENT,

                    ViewGroup.LayoutParams.WRAP_CONTENT);

              

              TextView  textView =new TextView(activity);

              textView.setLayoutParams(layoutParams);

              

              textView.setGravity(Gravity.CENTER_VERTICAL |Gravity.LEFT);

              

              textView.setPadding(40, 0, 0, 0);

              textView.setText(string);

              return textView;

         }

    }

    

    private void initdate()

    {

        addInfo("语言", new String[]{"Oracle","Java","Linux","Jquery"});

        addInfo("男人的需求", new String[]{"金钱","事业","权力","女人","房子","车","球"});

    }

    private void addInfo(String group,String []child) {

        

        groupArray.add(group);

        

        List<String>  childItem =new ArrayList<String>();

        

        for(int index=0;index<child.length;index++)

        {

            childItem.add(child[index]);

        }

         childArray.add(childItem);

    }

}

运行效果:

注释修改如下代码:

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

/*-第一季-*/

//        initdate();

//        expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this));

        

        /*-第二季-*/

        groupArray.add("移动开发");

        List<String> arrayList = new ArrayList<String>();

        arrayList.add("Android");

        arrayList.add("IOS");

        arrayList.add("Windows Phone");

        //组循环

        for(int index=0;index<groupArray.size();++index)

        {

            childArray.add(arrayList);

        }

        expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this));

运行效果:

★★★★★★★★★★★★★★★★★★★★

案例二:

1.定义一个主界面expandablelistview.xml

?


1

2

3

4

5

6

7

8

9

10

11

12

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

     <ExpandableListView

        android:id ="@+id/expandableListView"

        android:layout_width ="fill_parent"

        android:layout_height ="wrap_content"

        >

     </ExpandableListView>

</LinearLayout>

2.在res/drawable目录下创建样式文件expandablelistview_groups.xml该界面是组界面:

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

     <TextView

        android:id="@+id/textGroup"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:paddingLeft="40px"

        android:paddingTop="6px"

        android:paddingBottom="6px"

        android:textSize="15sp"

        android:text="No data"

    >  

    </TextView>

</LinearLayout>

3.在res/drawable目录下创建样式文件expandablelistview_child.xml;是子控件,直接显示列表内容

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

     <TextView

        android:id="@+id/textChild"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:paddingLeft="60px"

        android:paddingTop="10px"

        android:paddingBottom="10px"

        android:textSize="20sp"

        android:text="No Data" /> 

</LinearLayout>

定义java文件:ExpandableListViewDemo_two.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

package com.test;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.security.auth.PrivateCredentialPermission;

import com.test.R;

import com.test.ExpandableListViewDemo.ExpandableListViewaAdapter;

import com.test.R.id;

import com.test.R.layout;

import android.app.Activity;

import android.os.Bundle;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup;

import android.view.Window;

import android.widget.AbsListView;

import android.widget.BaseExpandableListAdapter;

import android.widget.ExpandableListView;

import android.widget.SimpleExpandableListAdapter;

import android.widget.TextView;

public class ExpandableListViewDemo_two extends Activity {

    /** Called when the activity is first created. */

    private  ExpandableListView  expandableListView_one;

    @Override

    public void onCreate(Bundle savedInstanceState)  

    {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.expandablelistview);  

        expandableListView_one =(ExpandableListView)findViewById(R.id.expandableListView);  

        //创建二个一级条目标题   

        Map<String, String> title_1 = new HashMap<String, String>();  

        Map<String, String> title_2 = new HashMap<String, String>();  

           

        title_1.put("group", "移动开发");  

        title_2.put("group", "男人的需求");  

           

        //创建一级条目容器   

        List<Map<String, String>> gruops = new ArrayList<Map<String,String>>();  

           

        gruops.add(title_1);  

        gruops.add(title_2);  

           

        //创建二级条目内容   

           

        //内容一   

        Map<String, String> content_1 = new HashMap<String, String>();  

        Map<String, String> content_2 = new HashMap<String, String>();  

           

        content_1.put("child", "ANDROID");  

        content_2.put("child", "IOS");  

           

        List<Map<String, String>> childs_1 = new ArrayList<Map<String,String>>();  

        childs_1.add(content_1);  

        childs_1.add(content_2);  

           

        //内容二   

        Map<String, String> content_3 = new HashMap<String, String>();  

        Map<String, String> content_4 = new HashMap<String, String>(); 

        Map<String, String> content_5 = new HashMap<String, String>();

           

        content_3.put("child", "金钱");  

        content_4.put("child", "权力");  

        content_5.put("child", "女人");

        List<Map<String, String>> childs_2 = new ArrayList<Map<String,String>>();  

        childs_2.add(content_3);  

        childs_2.add(content_4); 

        childs_2.add(content_5);

           

        //存放两个内容, 以便显示在列表中   

        List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();  

        childs.add(childs_1);  

        childs.add(childs_2);  

           

        //创建ExpandableList的Adapter容器   

/** 

* 使用SimpleExpandableListAdapter显示ExpandableListView 

* 参数1.上下文对象Context 

* 参数2.一级条目目录集合 

* 参数3.一级条目对应的布局文件 (expandablelistview_groups.xml文件

* 参数4.fromto,就是map中的key,指定要显示的对象 

* 参数5.与参数4对应,指定要显示在groups中的id 

* 参数6.二级条目目录集合 

* 参数7.二级条目对应的布局文件 

* 参数9.与参数8对应,指定要显示在childs中的id 

/           SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(  

                this, gruops, R.drawable.expandablelistview_groups, new String[]{"group"}, new int[]{R.id.textGroup},   

                childs, R.drawable.expandablelistview_child, new String[]{"child"}, new int[]{R.id.textChild}  

                );  

           

        //加入列表   

        expandableListView_one.setAdapter(adapter);

     expandableListView_one.setOnChildClickListener(listener);

    }  

    private OnChildClickListener  listener =new OnChildClickListener() {

   @Override

  public boolean onChildClick(ExpandableListView parent, View v,

    int groupPosition, int childPosition, long id) {

   // TODO Auto-generated method stub

   toast("点击了");

   return false;

  }

 };

 private void toast(String str) {

 Toast.makeText(this, str, Toast.LENGTH_LONG).show(); 

 }

}

上面的样式也可以使用系统的自带的样式如下:

android.R.layout.simple_expandable_list_item_1,//层显示样式 ,系统自定义

android.R.layout.simple_expandable_list_item_2,

运行效果:

案例三:如果group中有个ImageVIew将会是什么情况呢?

在SimpleExpandableListAdapter中有如下方法:

?


1

2

3

4

5

6

7

8

9

10

private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {

        int len = to.length;

        for (int i = 0; i < len; i++) {

            TextView v = (TextView)view.findViewById(to[i]);

            if (v != null) {

                v.setText((String)data.get(from[i]));

            }

        }

    }

从上面的方法中可以看出 SimpleExpandableListAdapter把所以的View都当成TextView来处理了,而不像SimpleAdapter可以自动判断View的类型,自动绑定,解决版本就是重写bingview回调一下试试:

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

public class MyExpandableListAdapter extends BaseExpandableListAdapter{

        private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {

                int len = to.length;

                boolean isBound = false;

                for (int i = 0; i < len; i++) {

                   final View v = view.findViewById(to[i]);

                 if (v!=null) {

                final Object _data = data.get(from[i]);

                String text = _data == null ? "" : data.toString();

                if (text == null) {

                    text = "";

                }

                          if (mViewBinder != null) {//如果Binder不为空,使用Binder进行处理

                                        isBound = mViewBinder.setViewValue(v, data.get(from[i]), text);

                                }

                                if (!isBound) {//如果Binder跳过,使用原来的方法进行处理

                                        TextView _v = (TextView)v;

                                        _v.setText((String)data.get(from[i]));

                                }                              

                        }

                }

        }

}

时间: 2024-08-30 01:25:33

android ExpandableListView详解(转载)的相关文章

android ExpandableListView详解

ExpandableListView是android中可以实现下拉list的一个控件,是一个垂直滚动的心事两个级别列表项手风琴试图,列表项是来自ExpandableListViewaAdapter,组可以单独展开. 重要方法: 1 expandGroup (int groupPos) ;//在分组列表视图中 展开一组, 2 setSelectedGroup (int groupPosition) ;//设置选择指定的组. 3 4 setSelectedChild (int groupPositi

Android ProgressBar详解以及自定义

版本:1.0 日期:2014.5.16 版权:© 2014 kince 转载注明出处 这一次主要说一下Android下的进度条,为什么是它呢,因为近期被其各种美轮美奂的设计所倾倒,计划逐渐去实现.另外一个因素也是它也是为数不多的直接继承于View类的控件,从中可以学习到一些自定义控件的知识.下面列举了一些个人觉得还算漂亮的进度条,仅供参考. 是不是很漂亮,其实就像上面图形展示的那样,进度条大体上无非就是这几种形式.这样一来肯定是需要自定义了,所以方向有两个:要么继承于系统的ProgressBar

Android编译详解之lunch命令 【转】

本文转载自: Android编译详解之lunch命令 (2012-10-08 10:27:55) 转载▼ 标签: it 分类: android内核剖析 Android的优势就在于其开源,手机和平板生产商可以根据自己的硬件进行个性定制自己的手机产品,如小米,LePhone,M9等,因此,在我们在对Android的源码进行定制的时候,很有必要了解下,Android的编译过程. 如果你从来没有做过Android代码的编译,那么最官方的编译过程就是查看Android的官方网站:http://source

android WebView详解,常见漏洞详解和安全源码

这篇博客主要来介绍 WebView 的相关使用方法,常见的几个漏洞,开发中可能遇到的坑和最后解决相应漏洞的源码,以及针对该源码的解析. 由于博客内容长度,这次将分为上下两篇,上篇详解 WebView 的使用,下篇讲述 WebView 的漏洞和坑,以及修复源码的解析. 下篇:android WebView详解,常见漏洞详解和安全源码(下) 转载请注明出处:http://blog.csdn.net/self_study/article/details/54928371. 对技术感兴趣的同鞋加群 54

Python安装、配置图文详解(转载)

Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境(IDE) 1. 在Eclipse中安装PyDev插件 2. 配置Python Interpreters 四. 创建Python Project 五. 编写HelloWorld 六. 小结 一. Python简介: Python在Linux.windows.Mac os等操作系统下都有相应的版本,不管在

android WebView详解,常见漏洞详解和安全源码(下)

上篇博客主要分析了 WebView 的详细使用,这篇来分析 WebView 的常见漏洞和使用的坑. 上篇:android WebView详解,常见漏洞详解和安全源码(上) 转载请注明出处:http://blog.csdn.net/self_study/article/details/55046348 对技术感兴趣的同鞋加群 544645972 一起交流. WebView 常见漏洞 WebView 的漏洞也是不少,列举一些常见的漏洞,实时更新,如果有其他的常见漏洞,知会一下我-- WebView

(转) Android ListView 详解

ListView的使用方法 ListView是Android软件开发中非常重要组件之一,基本上是个软件基本都会使用ListView ,今天我通过一个demo来教大家怎么样使用ListView组件 绘制出漂亮的列表,说道ListView就不得不说Adapter适配器,因为只有通过Adapter才可以把列表中的数据映射到ListView中. 在android的开发中最Adapter 一共可以分为ArrayAdapter<T>,BaseAdapter, CursorAdapter,HeaderVie

Android 动画详解之属性动画(Property Animation)

转载请注明http://blog.csdn.net/u014163726/article/details/41210951 前文也提到过Android 3.0以后引入了属性动画,属性动画可以轻而易举的办到许多View动画做不到的事,今天我们就来学习一下属性动画. 前文提到过View动画只是改变了View的绘制效果,而属性动画则是真正的改变一个属性,效果如下图. 对比Android 动画详解之View动画我们可以看到明显的区别,那么属性动画究竟是怎么用的呢,莫慌,接下来代码奉上. 1,Object

Android slidingmenu详解 优化侧滑

Android slidingmenu 详解 性能优化 转载请注明:   http://blog.csdn.net/aaawqqq 简介 SlidingMenu 是github 上Android开源项目  能用于快速集成 Android 侧滑菜单效果 Slidingmenu 里面可以包含多种组件 fragment  viewpager  listview  等 下载地址: github    : https://github.com/jfeinstein10/SlidingMenu action