Android开发实践:多级列表的封装与应用

简介:

   Android中多级列表可以使用ExpandableListView和SimpleExpandableListAdapter配合来实现,但是,SimpleExpandableListAdapter用起来挺麻烦的,不易理解,而且扩展性也不好,因此,自定义BaseExpandableListAdapter类的子类以及封装相关的操作,用起来会更加直观和方便,我把我设计的封装贴出来供新手参考吧。


   首先上效果图,如图所示:


224351468.png


1. 首先设计多级列表的标题类


就像文件和文件夹可以统一地用File类来抽象一样,多级列表的一级标题和二级标题其实也可以用同一个基类来抽象,因此,我设计了一个基类和两个子类,GroupList,GroupListChild 和 GroupListParent,其实现如下所示:


(1) GroupList 多级列表标题的抽象基类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public  abstract  class  GroupList {
     private  final  String mTitle;
                                                                                                                                                                                                                                                                            
     public  GroupList(String title) {
         mTitle = title;
     }
                                                                                                                                                                                                                                                                            
     public  String getTitle() {
         return  mTitle;
     }
                                                                                                                                                                                                                                                                            
     public  abstract  List<GroupList> getChild();
                                                                                                                                                                                                                                                                            
     public  abstract  int  getResource();     
     public  abstract  void  buildView(View v);
}


(2) GroupListChild 多级列表二级标题子类


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  GroupListChild  extends  GroupList {
                                                                                                                                                                                                                                  
     public  GroupListChild(String title) {
         super (title);      
     }
                                                                                                                                                                                                                               
     @Override
     public  int  getResource() {
         return  R.layout.grouplist_child;
     }
                                                                                                                                                                                                                               
     @Override
     public  List<GroupList> getChild() {     
         return  null ;
     }
                                                                                                                                                                                                                               
     @Override
     public  void  buildView(View v) {
                                                                                                                                                                                                                                      
         TextView textView  = (TextView)v.findViewById(R.id.GroupListChild);                   
         textView.setText(getTitle());
    
}


(3) GroupListParent 多级列表一级标题子类


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
public  class  GroupListParent  extends  GroupList {
                                                                                                                                                                                                                   
     private  List<GroupList> mPopListChilds;
                                                                                                                                                                                                                       
     public  GroupListParent(String title,List<GroupList> childs) {
         super (title);      
         mPopListChilds = childs;
     }
                                                                                                                                                                                                                   
     @Override
     public  int  getResource() {
         return  R.layout.grouplist_parent;
     }
                                                                                                                                                                                                                   
     @Override
     public  List<GroupList> getChild() {
         return  mPopListChilds;
     }
                                                                                                                                                                                                                       
     @Override
     public  void  buildView(View v) {
                                                                                                                                                                                                                                  
         TextView textView = (TextView)v.findViewById(R.id.GroupListParent);              
         textView.setText(getTitle());
     }
}


2.  设计BaseExpandableListAdapter的子类


我设计的子类是一种通用的Adapter子类,类的实现中并不包含具体的Layout实现,所有的Layout都由GroupList的getResource和buildView来负责,因此,可以非常灵活地修改Layout的具体实现,而不用修改Adapter的代码。


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
public  class  GroupListAdapter  extends  BaseExpandableListAdapter {
                                                                                                                                                                              
     private  Context mContext;
     private  List<GroupList> mGroups;
                                                                                                                                                                                  
     public  GroupListAdapter(Context context, List<GroupList> groups) {
         this .mContext = context;
         this .mGroups  = groups;
     }
                                                                                                                                                                                  
     @Override
     public  Object getChild( int  groupPosition,  int  childPosition) {
                                                                                                                                                                                     
         List<GroupList> chList = mGroups.get(groupPosition).getChild();
         if ( chList ==  null ) {
             return  null ;
         }
                                                                                                                                                                                      
         return  chList.get(childPosition);
     }
                                                                                                                                                                              
     @Override
     public  long  getChildId( int  groupPosition,  int  childPosition) {
                                                                                                                                                                               
         return  childPosition;
     }
                                                                                                                                                                                
     @Override
     public  View getChildView( int  groupPosition,  int  childPosition,  boolean  isLastChild, View view,ViewGroup parent) {
                                                                                                                                                                                                                     
         GroupList child = (GroupList)getChild(groupPosition, childPosition);
         if ( child ==  null ) {
             return  null ;
         }
                                                                                                                                                                                          
         if  (view ==  null ) {   
             LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
             view = (RelativeLayout)inflater.inflate(child.getResource(),  null );
         }
                                                                                                                                                                                           
         child.buildView(view);
                                                                                                                                                                                     
         return  view;
     }
                                                                                                                                                                                
     @Override
     public  int  getChildrenCount( int  groupPosition) {
                                                                                                                                                                                     
         List<GroupList> chList = mGroups.get(groupPosition).getChild();
         if ( chList ==  null ) {
             return  0 ;
         }
                                                                                                                                                                                     
         return  chList.size();
     }
                                                                                                                                                                                
     @Override
     public  Object getGroup( int  groupPosition) {
         return  mGroups.get(groupPosition);
     }
                                                                                                                                                                                
     @Override
     public  int  getGroupCount() {
         return  mGroups.size();
     }
                                                                                                                                                                                
     @Override
     public  long  getGroupId( int  groupPosition) {
         return  groupPosition;
     }
                                                                                                                                                                                
     @Override
     public  View getGroupView( int  groupPosition,  boolean  isLastChild, View view,ViewGroup parent) {
                                                                                                                                                                               
         GroupList group = (GroupList)getGroup(groupPosition);
                                                                                                                                                                                       
         if  (view ==  null ) {
                                                                                                                                                                                
             LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
             view = (RelativeLayout)inflater.inflate(group.getResource(),  null );  
         }
                                                                                                                                                                                
         group.buildView(view);
                                                                                                                                                                                     
         return  view;
     }
                                                                                                                                                                                
     @Override
     public  boolean  hasStableIds() {
         return  true ;
     }
                                                                                                                                                                                
     @Override
     public  boolean  isChildSelectable( int  arg0,  int  arg1) {
         return  true ;
     }
}


3.  应用代码


为了简化,我就直接在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
public  class  MainActivity  extends  Activity  implements  OnGroupClickListener,OnChildClickListener {
                                                                                                                                              
     private  ExpandableListView mlistView; 
     private  GroupListAdapter mAdapter;
                                                                                                                                                  
     private  static  final  String[] mParentMenu = {
         "Book" "Video" "Audio"
     };
                                                                                                                                                  
     private  static  final  String[][] mChildMenu = {
         "book1" "book2" "book3" "book4"  },
         "video1" "video2"  },
         "audio1" "audio2" "audio3" , "audio4" }
     };
                                                                                                                                                  
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
                                                                                                                                                            
         mlistView =  new  ExpandableListView( this );                      
         mlistView.setOnGroupClickListener( this );      
         mlistView.setOnChildClickListener( this );
                                                                                                                                                       
         List<GroupList> groups =  new  ArrayList<GroupList>();
         for int  i= 0 ; i<mParentMenu.length; i++) {         
             List<GroupList> childs =  new  ArrayList<GroupList>();
             for int  j= 0 ; j<mChildMenu[i].length; j++ ) {
                 childs.add( new  GroupListChild(mChildMenu[i][j]));
             }
             groups.add( new  GroupListParent(mParentMenu[i],childs));
         }
                                                                                                                                              
         mAdapter =  new  GroupListAdapter( this ,groups);
         mlistView.setAdapter(mAdapter);
                                                                                                                                                      
         setContentView(mlistView);    
     }
                                                                                                                                              
     @Override
     public  boolean  onChildClick(ExpandableListView parent, View v, int  groupPosition,  int  childPosition,  long  id) {
                                                                                                                                                      
         return  false ;
     }
                                                                                                                                              
     @Override
     public  boolean  onGroupClick(ExpandableListView parent, View v, int  groupPosition,  long  id) {
                                                                                                                                                    
         return  false ;
     }
}


4.  相关的xml文件


(1) grouplist_child.xml 文件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<? xml  version = "1.0"  encoding = "utf-8" ?>
< RelativeLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     android:orientation = "horizontal" >
                                                                                                                                      
     < TextView
         android:id = "@+id/GroupListChild"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:textColor = "#FFFF0000"
         android:layout_margin = "10dp"
         android:layout_centerInParent = "true" />
                                                                                                                                      
</ RelativeLayout >


(2) grouplist_parent.xml 文件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<? xml  version = "1.0"  encoding = "utf-8" ?>
< RelativeLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     android:orientation = "horizontal" >
                                                                                                           
     < TextView
         android:id = "@+id/GroupListParent"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:textColor = "@android:color/black"
         android:textStyle = "bold"
         android:layout_margin = "10dp"
         android:layout_centerInParent = "true" />
                                                                                                           
</ RelativeLayout >




本文转自 Jhuster 51CTO博客,原文链接:http://blog.51cto.com/ticktick/1289642,如需转载请自行联系原作者

相关文章
|
21天前
|
前端开发 数据可视化 Java
Android用Canvas画一个折线图,并加以简单封装
本文介绍了如何用Java绘制动态折线图,从固定折线图的实现到封装成可复用的组件。首先通过绘制XY坐标轴、添加坐标标签和绘制折线及数据点完成基础折线图。接着,将静态数据替换为动态输入,支持自定义X轴、Y轴和折线数据。代码中包含关键方法如`drawDaxes`(绘制坐标轴)、`drawAxispoint`(绘制坐标点)和`drawbrokenLine`(绘制折线)。最终实现可根据传入数据动态生成折线图,适用于Android开发中的数据可视化场景。
|
15天前
|
安全 Java Android开发
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
42 0
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
|
3月前
|
JavaScript Linux 网络安全
Termux安卓终端美化与开发实战:从下载到插件优化,小白也能玩转Linux
Termux是一款安卓平台上的开源终端模拟器,支持apt包管理、SSH连接及Python/Node.js/C++开发环境搭建,被誉为“手机上的Linux系统”。其特点包括零ROOT权限、跨平台开发和强大扩展性。本文详细介绍其安装准备、基础与高级环境配置、必备插件推荐、常见问题解决方法以及延伸学习资源,帮助用户充分利用Termux进行开发与学习。适用于Android 7+设备,原创内容转载请注明来源。
558 76
|
4月前
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
118 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
|
4月前
|
Dart 前端开发 Android开发
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
106 4
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
4月前
|
安全 Android开发 iOS开发
escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
escrcpy 是一款基于 Scrcpy 的开源项目,使用 Electron 构建,提供图形化界面来显示和控制 Android 设备。它支持 USB 和 Wi-Fi 连接,帧率可达 30-120fps,延迟低至 35-70ms,启动迅速且画质清晰。escrcpy 拥有丰富的功能,包括自动化任务、多设备管理、反向网络共享、批量操作等,无需注册账号或广告干扰。适用于游戏直播、办公协作和教育演示等多种场景,是一款轻量级、高性能的 Android 控制工具。
206 1