Android数据适配-ExpandableListView

简介:

Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableListView展示一种两层的效果,ExpandableListView是android中可以实现下拉list的一个控件类似于QQ那种我好友之后就是一排自己的好友,就是两层效果,实现的话使用SimpleExpandableListAdapter即可。

布局文件

先看下效果

main中xml代码:

1
2
3
4
5
6
7
8
9
10
11
<Button
       android:onClick= "test"
       android:layout_width= "fill_parent"
       android:layout_height= "wrap_content"
       android:text= "FlyElephant"  />
 
   <ExpandableListView
       android:id= "@id/android:list"
       android:layout_width= "fill_parent"
       android:layout_height= "fill_parent"
       android:drawSelectorOnTop= "false"  />

 定义一个省份的province.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= "match_parent"
     android:layout_height= "match_parent"
     android:orientation= "vertical"  >
 
     <TextView
         android:id= "@+id/list_provinceText"
         android:layout_width= "fill_parent"
         android:layout_height= "fill_parent"
         android:paddingBottom= "8px"
         android:paddingLeft= "30px"
         android:paddingRight= "5px"
         android:paddingTop= "8px"
         android:textSize= "20sp"  />
 
</LinearLayout>

定义了一个地区的child.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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/child_text"
         android:layout_width= "fill_parent"
         android:layout_height= "fill_parent"
         android:paddingBottom= "8px"
         android:paddingLeft= "30px"
         android:paddingRight= "5px"
         android:paddingTop= "8px"
         android:textSize= "20sp"  />
     
</LinearLayout>

 Demo实现

主要实现代码,代码中都已经注释,其中最主要的SimpleExpandableListAdapter中的参数,这个参数太多,很容易弄错,可以看下注释或者API文档:

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
// 创建一级条目
  List<Map<String, String>> provinces =  new  ArrayList<Map<String, String>>();
  //创建两个省份一级条目
  Map<String, String> firstProvince=  new  HashMap<String, String>();
  firstProvince.put( "province" "河南" );
  Map<String, String> secondProvince=  new  HashMap<String, String>();
  secondProvince.put( "province" "北京" );
  provinces.add(firstProvince);
  provinces.add(secondProvince);
  // 创建一级条目下的的二级地区条目
  List<Map<String, String>> childList1=  new  ArrayList<Map<String, String>>();
  //同样是在一级条目目录下创建两个对应的二级条目目录
  Map<String, String> child1=  new  HashMap<String, String>();
  child1.put( "child" "郑州" );
  Map<String, String> child2 =  new  HashMap<String, String>();
  child2.put( "child" "开封" );
  childList1.add(child1);
  childList1.add(child2);
  //同上
  List<Map<String, String>> childList2 =  new  ArrayList<Map<String, String>>();
  Map<String, String> child3 =  new  HashMap<String, String>();
  child3.put( "child" "海淀" );
  Map<String, String> child4 =  new  HashMap<String, String>();
  child4.put( "child" "昌平" );
  childList2.add(child3);
  childList2.add(child4);
  // 将二级条目放在一个集合里,供显示时使用
  List<List<Map<String, String>>> childs =  new  ArrayList<List<Map<String, String>>>();
  childs.add(childList1);
  childs.add(childList2);
  /**
   * 使用SimpleExpandableListAdapter显示ExpandableListView
   * 参数1.上下文对象Context
   * 参数2.一级条目目录集合
   * 参数3.一级条目对应的布局文件
   * 参数4.fromto,就是map中的key,指定要显示的对象
   * 参数5.与参数4对应,指定要显示在groups中的id
   * 参数6.二级条目目录集合
   * 参数7.二级条目对应的布局文件
   * 参数8.fromto,就是map中的key,指定要显示的对象
   * 参数9.与参数8对应,指定要显示在childs中的id
   */
  SimpleExpandableListAdapter adapter =  new  SimpleExpandableListAdapter(
          this , provinces, R.layout.list_group,  new  String[] {  "province"  },
          new  int [] { R.id.list_groupText }, childs, R.layout.child,
          new  String[] {  "child"  },  new  int [] { R.id.child_text });
  setListAdapter(adapter);

这个mainActivity需要继承ExpandableListActivity,当然你可以设置其中的点击事件,只要重写一下方法即可:

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
/**
  * 设置哪个二级目录被默认选中
  */
@Override
public  boolean  setSelectedChild( int  groupPosition,  int  childPosition,
         boolean  shouldExpandGroup) {
         //do something
     return  super .setSelectedChild(groupPosition, childPosition,
             shouldExpandGroup);
}
/**
  * 设置哪个一级目录被默认选中
  */
@Override
public  void  setSelectedGroup( int  groupPosition) {
     //do something
     super .setSelectedGroup(groupPosition);
}
/**
  * 当二级条目被点击时响应
  */
@Override
public  boolean  onChildClick(ExpandableListView parent, View v,
         int  groupPosition,  int  childPosition,  long  id) {
         //do something
     return  super .onChildClick(parent, v, groupPosition, childPosition, id);
}

 效果如下:

 

上面这个例子写的有点单调,其实第二个你子的布局直接是空的也行,例如定义一个images.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:orientation= "horizontal"  >
 
     <ImageView
         android:src= "@drawable/open"
         android:layout_width= "20dp"
         android:layout_height= "20dp"  />
 
     <TextView
         android:id= "@+id/txtName"
        android:paddingLeft= "10dp"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"  />
 
</LinearLayout>

然后定义一个items.xml

1
2
3
4
5
6
7
8
<?xml version= "1.0"  encoding= "utf-8" ?>
<TextView xmlns:android= "http://schemas.android.com/apk/res/android"
     android:id= "@+id/items"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"  >
     
 
</TextView>

 代码调用:

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
public  class  MyExpandleActivity  extends  Activity {
 
     /**
      * 实现可扩展展开列ExpandableListView的三种方式
      * 一是使用SimpleExpandableListAdpater将两个List集合包装成ExpandableListView 二是
      * 扩展BaseExpandableListAdpter
      * 三是使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter
      */
     private  String[] names = {  "腾讯" "百度" "阿里巴巴"  };
 
     private  String[][] childnames = { {  "马化腾" "张小龙" , "社交" },
             "李彦宏" "马东敏" , "搜索"  }, {  "马云" "陆兆禧" , "电商"  } };
     private  ExpandableListView ep;
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_my_expandle);
 
         // 定义父列表项List数据集合
         List<Map<String, String>> group =  new  ArrayList<Map<String, String>>();
         // 定义子列表项List数据集合
         List<List<Map<String, String>>> ss =  new  ArrayList<List<Map<String, String>>>();
         for  ( int  i =  0 ; i < names.length; i++) {
             // 提供父列表的数据
             Map<String, String> maps =  new  HashMap<String, String>();
             maps.put( "names" , names[i]);
             group.add(maps);
             // 提供当前父列的子列数据
             List<Map<String, String>> child =  new  ArrayList<Map<String, String>>();
             for  ( int  j =  0 ; j < names.length; j++) {
                 Map<String, String> mapsj =  new  HashMap<String, String>();
                 mapsj.put( "map" , childnames[i][j]);
                 child.add(mapsj);
             }
             ss.add(child);
         }
         /**
          * 第一个参数 应用程序接口 this 第二个父列List<?extends Map<String,Object>>集合 为父列提供数据
          * 第三个参数 父列显示的组件资源文件 第四个参数 键值列表 父列Map字典的key 第五个要显示的父列组件id 第六个 子列的显示资源文件
          * 第七个参数 键值列表的子列Map字典的key 第八个要显示子列的组件id
          */
         SimpleExpandableListAdapter expand =  new  SimpleExpandableListAdapter(
                 this , group, R.layout.images,  new  String[] {  "names"  },
                 new  int [] { R.id.txtName }, ss, R.layout.items,
                 new  String[] {  "map"  },  new  int [] { R.id.items });
         ep = (ExpandableListView) findViewById(R.id.expanable_mylist);
         ep.setAdapter(expand);
 
     }
 
}

  效果跟上面相同:

 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4107356.html,如需转载请自行联系原作者

相关文章
|
8天前
|
消息中间件 网络协议 Java
Android 开发中实现数据传递:广播和Handler
Android 开发中实现数据传递:广播和Handler
13 1
|
4月前
|
安全 API Android开发
Android网络和数据交互: 解释Retrofit库的作用。
Android网络和数据交互: 解释Retrofit库的作用。
39 0
|
9天前
|
Android开发
Android RIL 动态切换 4G 模块适配
Android RIL 动态切换 4G 模块适配
14 0
|
15天前
|
Android开发 开发者
Android网络和数据交互: 请解释Android中的AsyncTask的作用。
Android&#39;s AsyncTask simplifies asynchronous tasks for brief background work, bridging UI and worker threads. It involves execute() for starting tasks, doInBackground() for background execution, publishProgress() for progress updates, and onPostExecute() for returning results to the main thread.
11 0
|
15天前
|
网络协议 安全 API
Android网络和数据交互: 什么是HTTP和HTTPS?在Android中如何进行网络请求?
HTTP和HTTPS是网络数据传输协议,HTTP基于TCP/IP,简单快速,HTTPS则是加密的HTTP,确保数据安全。在Android中,过去常用HttpURLConnection和HttpClient,但HttpClient自Android 6.0起被移除。现在推荐使用支持TLS、流式上传下载、超时配置等特性的HttpsURLConnection进行网络请求。
11 0
|
18天前
|
编解码 人工智能 测试技术
安卓适配性策略:确保应用在不同设备上的兼容性
【4月更文挑战第13天】本文探讨了提升安卓应用兼容性的策略,包括理解平台碎片化、设计响应式UI(使用dp单位,考虑横竖屏)、利用Android SDK的兼容工具(支持库、资源限定符)、编写兼容性代码(运行时权限、设备特性检查)以及优化性能以适应低端设备。适配性是安卓开发的关键,通过这些方法可确保应用在多样化设备上提供一致体验。未来,自动化测试和AI将助力应对设备碎片化挑战。
|
23天前
|
JSON 安全 Java
Android网络部分-----网络数据请求、解析
Android网络部分-----网络数据请求、解析
Android网络部分-----网络数据请求、解析
|
29天前
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
22 1
|
29天前
|
Android开发
Android Uri转File方法(适配android 10以上版本及android 10以下版本)
Android Uri转File方法(适配android 10以上版本及android 10以下版本)
21 0
|
29天前
|
XML Java Android开发
Android每点击一次按钮就添加一条数据
Android每点击一次按钮就添加一条数据
24 1