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

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

Android ExpandableListViewAdapter 的构建


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






相关文章:

  • 2021-12-20
  • 2022-12-23
  • 2021-09-24
  • 2021-09-08
  • 2021-04-29
  • 2021-11-30
  • 2021-03-31
猜你喜欢
  • 2021-09-20
  • 2021-10-10
  • 2021-04-14
  • 2021-06-14
  • 2022-02-01
  • 2022-01-09
  • 2021-09-14
相关资源
相似解决方案