Android ExpandableListViewAdapter 的构建

来源:互联网 发布:远程桌面连接换端口 编辑:程序博客网 时间:2024/05/18 03:10

控件PullToRefreshExpandableListView  和  控件ExpandableListView  用的是同一个适配器BaseExpandableListAdapter

那么就好办了,自定义一个类 去继承系统的 BaseExpandableListAdapter 适配器,去重写里面的方法:



public class OrderAdapter extends BaseExpandableListAdapter {            private ArrayList<OrderListGroup> orderListGroups;        public OrderAdapter(ArrayList<OrderListGroup> orderListGroups) {        this.orderListGroups = orderListGroups}    @Override    public int getGroupCount() {        //父布局长度        return orderListGroups.size();    }    @Override    public int getChildrenCount(int groupPosition) {       //每个父布局当中子布局的长度       return orderListGroups.get(groupPosition).getOrderListChilds().size();    }    @Override    public Object getGroup(int groupPosition) {       //获取每一条父布局       return orderListGroups.get(groupPosition);    }    @Override    public Object getChild(int groupPosition, int childPosition) {       //从每一条父布局中获取每一条子布局       return orderListGroups.get(groupPosition).getOrderListChilds().get(childPosition);    }    @Override    public long getGroupId(int groupPosition) {       //父布局当前ID       return groupPosition;    }    @Override    public long getChildId(int groupPosition, int childPosition) {
       //父布局当前ID
  return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { //展示父布局
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_order_group,null);
return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
       //展示子布局
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_order_child,null);
return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; }

其中要注意hasStableIds() 和isChildSelectable(int groupPosition, int childPosition)这两个方法。hasStableIds() 主要是用来判断ExpandableListView内容id是否有效的(返回true or false),系统会跟据id来确定当前显示哪条内容,也就是firstVisibleChild的位置。而isChildSelectable(int groupPosition, int childPosition)用来判断某Group某个child是否可可选。我们可以添加条件控制某Group某个child可点或不可点击。当不加任何条件直接返回false,所有的组的child均不可点击。


在这之前,首先就要准备好数据。所以建了个构造方法用来接收数据

private ArrayList<OrderListGroup> orderListGroups;public OrderAdapter(ArrayList<OrderListGroup> orderListGroups) {    this.orderListGroups = orderListGroups;}

Apadter 适配器优化和 ListView 优化一样。

适配器建好之后,和控件PullToRefreshExpandableListView  或  控件ExpandableListView  相结合使用

控件PullToRefreshExpandableListView 的使用说明:http://blog.csdn.net/shiyangkai/article/details/72470098