Java转身系列之安卓5:ExpandableListView制作视频播放列表
在系列文章第3篇,根据设定的MyPlayer播放器业务需求,需要实现对视频简单分类,并且设计了一个主界面原型:
在安卓UI组件当中,ExpandableListView可以实现折叠式分类列表,在这一篇中利用该组件进行主界面实现。
新建MainActivity,layout设置为LinearLayout,orientation方向设置为vertical垂直方向:
从界面拖拽或者在xml中增加ExpandableListView组件,layout_width和layout_height都设置为match_parent,表示占据完父容器的宽和高。
设计器中,ExpandableListView的呈现与layout布局无差别。该组件的api说明摘要:
ExpandableListView有一个父类是视图组ViewGroup,这个组件是所有layout布局类的父类,也就是说ExandableListView也是一个组件布局容器。
该视图将两级列表项以垂直滚动方式显示,它与ListView的差别是允许两级:组可以被单独展开显示它的子项。列表项来自于与该视图绑定的ExpandableListAdapter。
要让ExpandableListView显示数据,需要了解和使用ExpandableListAdapter这个接口。
An adapter that links a ExpandableListView with the underlying data. The implementation of this interface will provide access to the data of the children (categorized by groups), and also instantiate Views for children and groups.
适配器将ExpandableListView与底下的数据连结。接口实现将提供以组分类的子项数据访问,并为视图充实子项与分组数据。
在给ExpandableListView填充数据前,先对录像进行数据建模。
根据需求分析,录像可以分类,有名称和播放url地址。新增两个类,VideoCategory与Video,呈一对多关系:
public class VideoCategory {
private String name;
private List<Video> videos;//录像列表
public String getName() {
return name;}
public void setName(String name) {
this.name = name;}
public List<Video> getVideos() {
return videos;}
public void setVideos(List<Video> videos) {
this.videos = videos;}
}
public class Video {
private String name;
private String url;//播放地址
public String