
在需要实现一个List的item需要包含列表的时候,我们就可以选择ExpandableListVIEw. 其实这个VIEw的原始设计还是ListVIEw的那套.就是增加2层的ListVIEw而已.所以在写它的适配器与ListVIEw的适配器挺相似的,所以会有一个通病就是没有Item的VIEw的复用机制请一定要注意这点,在实现使用的时候需要写Item的VIEw的复用,减少内存与增加性能.
一个简单的Demo老规矩,先来一个最简单的demo来了解下最基本的使用方法.注意!这个demo是没有在Adapter写任何VIEw复用机制的请不要用到实际项目中. demo只是帮助你快速认识了解ExpandableListVIEw
效果图 Activity的Xml<?xml version="1.0" enCoding="utf-8"?><linearLayout 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:orIEntation="vertical" tools:context=".MainActivity"> <ExpandableListVIEw androID:ID="@+ID/expandableListvIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> </ExpandableListVIEw></linearLayout>一级Item和二级Item用的xml布局
偷懒,我让一级和二级都使用一个布局
<?xml version="1.0" enCoding="utf-8"?><TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/text1" androID:layout_wIDth="match_parent" androID:layout_height="45dp" androID:text="内容" androID:textSize="15sp" androID:textcolor="@color/FontBlack3" androID:gravity="center" androID:background="@color/colorWhite"></TextVIEw>编写适配器
import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.Baseexpandablelistadapter;import androID.Widget.TextVIEw;import java.util.List;public class DemoAdapter extends Baseexpandablelistadapter { List<String> mGroupList;//一级List List<List<String>> mChildList;//二级List 注意!这里是List里面套了一个List<String>,实际项目你可以写一个pojo类来管理2层数据 public DemoAdapter(List<String> groupList,List<List<String>> childList){ mGroupList = groupList; mChildList = childList; } @OverrIDe public int getGroupCount() {//返回第一级List长度 return mGroupList.size(); } @OverrIDe public int getChildrenCount(int groupposition) {//返回指定groupposition的第二级List长度 return mChildList.get(groupposition).size(); } @OverrIDe public Object getGroup(int groupposition) {//返回一级List里的内容 return mGroupList.get(groupposition); } @OverrIDe public Object getChild(int groupposition,int childposition) {//返回二级List的内容 return mChildList.get(groupposition).get(childposition); } @OverrIDe public long getGroupID(int groupposition) {//返回一级VIEw的ID 保证ID唯一 return groupposition; } @OverrIDe public long getChildID(int groupposition,int childposition) {//返回二级VIEw的ID 保证ID唯一 return groupposition + childposition; } /** * 指示在对基础数据进行更改时子ID和组ID是否稳定 * @H_500_301@@return */ @OverrIDe public boolean hasStableIDs() { return true; } /** * 返回一级父VIEw */ @OverrIDe public VIEw getGroupVIEw(int groupposition,boolean isExpanded,VIEw convertVIEw,VIEwGroup parent) { convertVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_item,parent,false); ((TextVIEw)convertVIEw).setText((String)getGroup(groupposition)); return convertVIEw; } /** * 返回二级子VIEw */ @OverrIDe public VIEw getChildVIEw(int groupposition,int childposition,boolean isLastChild,false); ((TextVIEw)convertVIEw).setText((String)getChild(groupposition,childposition)); return convertVIEw; } /** * 指定位置的子项是否可选 */ @OverrIDe public boolean isChildSelectable(int groupposition,int childposition) { return true; }}Activity里的代码
mExpandableListVIEw = findVIEwByID(R.ID.expandableListvIEw); List<String> groupList = new ArrayList<>(); groupList.add("一"); groupList.add("二"); groupList.add("三"); List<List<String>> childList = new ArrayList<>(); List<String> childList1 = new ArrayList<>(); childList1.add("1"); childList1.add("1"); childList1.add("1"); List<String> childList2 = new ArrayList<>(); childList2.add("2"); childList2.add("2"); childList2.add("2"); List<String> childList3 = new ArrayList<>(); childList3.add("3"); childList3.add("3"); childList3.add("3"); childList.add(childList1); childList.add(childList2); childList.add(childList3); DemoAdapter demoAdapter = new DemoAdapter(groupList,childList); mExpandableListVIEw.setAdapter(demoAdapter); mExpandableListVIEw.setonGroupClickListener(new ExpandableListVIEw.OnGroupClickListener() {//一级点击监听 @OverrIDe public boolean onGroupClick(ExpandableListVIEw parent,VIEw v,int groupposition,long ID) { //如果你处理了并且消费了点击返回true,这是一个基本的防止ontouch事件向下或者向上传递的返回机制 return false; } }); mExpandableListVIEw.setonChildClickListener(new ExpandableListVIEw.OnChildClickListener() {//二级点击监听 @OverrIDe public boolean onChildClick(ExpandableListVIEw parent,long ID) { //如果你处理了并且消费了点击返回true return false; } });其他Xml属性
androID:divIDerHeight="20dp" 设置边距高度,注意设置这个边距包括了一级和二级
androID:divIDer="@color/colorRed1" 设置一级边距的背景色
end
总结以上是内存溢出为你收集整理的Android开发 ExpandableListView 列表内容下拉View详解全部内容,希望文章能够帮你解决Android开发 ExpandableListView 列表内容下拉View详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)