ExpandableListView组件分组用法
2016-03-08 22:56
393 查看
ExpandableListView组件分组用法
ExpandableListView组件是android中一个比较常用的组件,当点击一个父item的时候可以将它的子item显示出来,像QQ中的好友列表就是实现的类型效果。使用ExpandableListView组件的关键就是设置它的adapter,这个adapter必须继承BaseExpandbaleListAdapter类,所以实现运用ExpandableListView的核心就是学会继承这个BaseExpanableListAdapter类
首先建立3个xml文件
1.activity_main.xml
2.layout_children.xml
3.layout_parent.xml
最后就是java代码了MainActivity.java,详情见注释
运行结果,样式设置太简单,有点丑,可以使用官方提供的android.R.layout.simple_expandable_list_item_1
和android.R.layout.simple_expandable_list+item_2,个人建议去参照我的第一篇博文,加一张图片和两行文本
推荐使用相对布局.你要相信只要你想写就能成功.
ExpandableListView组件是android中一个比较常用的组件,当点击一个父item的时候可以将它的子item显示出来,像QQ中的好友列表就是实现的类型效果。使用ExpandableListView组件的关键就是设置它的adapter,这个adapter必须继承BaseExpandbaleListAdapter类,所以实现运用ExpandableListView的核心就是学会继承这个BaseExpanableListAdapter类
首先建立3个xml文件
1.activity_main.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ExpandableListView android:id="@+id/myExpandListView" android:layout_width="match_parent" android:layout_height="wrap_content"/> </RelativeLayout>
2.layout_children.xml
<?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/tv_children" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
3.layout_parent.xml
<?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/tv_parent" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
最后就是java代码了MainActivity.java,详情见注释
public class MainActivity extends Activity { private ExpandableListView expandableListView; private List<String> parentList =new ArrayList<String>(); private Map<String,List<String>> map =new HashMap<String,List<String>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); expandableListView = (ExpandableListView) findViewById(R.id.myExpandListView); initData(); expandableListView.setAdapter(new MyAdapter()); } private void initData() { parentList.clear(); map.clear(); parentList.add("friend"); parentList.add("high"); parentList.add("classM"); List<String> list1 =new ArrayList<String>(); { list1.clear(); list1.add("曹老师"); list1.add("高敏"); list1.add("宋慧乔"); map.put("friend",list1); } List<String> list2 =new ArrayList<String>(); { list2.clear(); list2.add("袁妙"); list2.add("茅岗"); list2.add("阿全"); map.put("high",list2); } List<String> list3 =new ArrayList<String>(); { list3.clear(); list3.add("老潘"); list3.add("郑大神"); list3.add("娄大神"); map.put("classM",list3); } } class MyAdapter extends BaseExpandableListAdapter{ @Override public int getGroupCount() { return parentList.size(); } //获取当前父item下的子item的歌手 @Override public int getChildrenCount(int groupPosition) { String key =parentList.get(groupPosition); return map.get(key).size(); } @Override public Object getGroup(int groupPosition) { return parentList.get(groupPosition); } //获取子item对象 @Override public Object getChild(int groupPosition, int childPosition) { String key=parentList.get(groupPosition); return map.get(key).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){ 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.tv_parent); tv.setText(MainActivity.this.parentList.get(groupPosition)); return tv; } //子组件和数据绑定 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String key = parentList.get(groupPosition); String text =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.tv_children); tv.setText(text); return tv; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }
运行结果,样式设置太简单,有点丑,可以使用官方提供的android.R.layout.simple_expandable_list_item_1
和android.R.layout.simple_expandable_list+item_2,个人建议去参照我的第一篇博文,加一张图片和两行文本
推荐使用相对布局.你要相信只要你想写就能成功.
相关文章推荐
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- flex 控件的重要属性
- 交换机升级排障实例
- Delphi控件ListView的属性及使用方法详解
- upload上传单张图片
- 图片引发的溢出危机(图)
- web下载的ActiveX控件自动更新
- C#实现把彩色图片灰度化代码分享
- C#将图片和字节流互相转换并显示到页面上
- WinForm实现按名称递归查找控件的方法
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- C#中父窗口和子窗口之间控件互操作实例
- 如何使用C#从word文档中提取图片
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- C#图片添加水印的实现代码
- 随鼠标移动的图片或文字特效代码
- CSS 图片横向排列实现代码
- C#实现将Email地址转成图片显示的方法