学习Android实现可折叠ListView(ExpandableListView)
2017-09-06 12:42
465 查看
首先要说明的是,本文是学习,并非原作者,源地址[Android常用控件(能折叠的ListView)–ExpandableListView的使用模仿QQ好友列表(http://blog.csdn.net/dl10210950/article/details/52525492)]
控件:
Adapter:
Main代码:
代码我已上传到Github:https://github.com/fengwenyi/ExpendableListviewDemo
控件:
<ExpandableListView android:id="@+id/expendableListview" android:layout_width="match_parent" android:layout_height="wrap_content" />
Adapter:
class MyAdapter( private var mContext : Context, /** group 数据 */ private var groupList: ArrayList<String>, /** child 数据 */ private var childList : ArrayList<ArrayList<String>>) : BaseExpandableListAdapter() { override fun getGroup(groupPosition: Int): Any { return groupPosition } override fun isChildSelectable(p0: Int, p1: Int): Boolean { return true } override fun hasStableIds(): Boolean { return true } @SuppressLint("SetTextI18n") override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View { val groupView = View.inflate(mContext, R.layout.item_group, null) /** 分组名字 */ val groupName = groupView.findViewById<TextView>(R.id.groupTvName) groupName.text = groupList[groupPosition] /** 子元素个数 */ val count = groupView.findViewById<TextView>(R.id.groupTvCount) count.text = "${childList[groupPosition].size} 个" return groupView } /** * 这个返回的一定要是对应的item里面的List集合的size */ override fun getChildrenCount(groupPosition: Int): Int { return childList[groupPosition].size } override fun getChild(groupPosition: Int, childPosition: Int): Any { return childList[groupPosition][childPosition] } override fun getGroupId(groupPosition: Int): Long { return groupPosition.toLong() } override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, view: View?, viewGroup: ViewGroup?): View { val childView = View.inflate(mContext, R.layout.item_child, null) val username = childView.findViewById<TextView>(R.id.childTvUserName) username.text = childList[groupPosition][childPosition] return childView } override fun getChildId(groupPosition: Int, childPosition: Int): Long { return childPosition.toLong() } override fun getGroupCount(): Int { return groupList.size } }
Main代码:
val adapter = MyAdapter(this, groupList, childList) expendableListview.setAdapter(adapter)
代码我已上传到Github:https://github.com/fengwenyi/ExpendableListviewDemo
相关文章推荐
- Android学习第五天————ExpandableListView组件通过适配器BaseExpandableListAdapter实现两层列表项
- Android之实现QQ好友分组(ExpandableListView)
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- android中ExpandableListView点击哪一条那一条显示,而其他条隐藏的实现办法
- Android学习 —— 多种适配器方式实现ListView
- android expandablelistview--实现类似qq界面的效果
- Android之实现QQ好友分组(ExpandableListView)
- android expandablelistview--实现类似qq界面的效果
- Android之ExpandableListView下拉分组的实现
- Android实现listview、expandablelistview、scrollview等组件的下拉或上拉刷新
- Android之ExpandableListView下拉分组的实现
- android 学习笔记:自定义通用ListView/GridView,实现ListAdapter 类
- android关于scrollview嵌套ExpandableListView的实现1
- Android学习 —— 多种适配器方式实现ListView
- android学习:ExpandableListView的运用
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- android关于scrollview嵌套ExpandableListView的实现2
- Android之实现QQ好友分组(ExpandableListView)
- Android UI ExpandableListView实现扩展列表
- 在android里用ExpandableListView实现二层和三层列表源码