ExpandableListView点击Group动态获取Child数据源

当ExpandableListView的Child数据量较大时,为避免资源浪费,可以改为点击Group时才获取Child数据。实现包括自定义MyExpandableListView,创建MyMaterialExpandableAdapter,并设置数据源。通过监听Group点击事件来动态加载并展示Child数据。
摘要由CSDN通过智能技术生成

前述

一般我们在使用ExpandableListView的时候,都是把Group和Child的数据都加载到适配器中。
现在有一个问题:那就是假如说Child的数据量过大的时候,全部加载下来是否造成浪费,由此,
我们想到在点击Group对应的分组的时候,再去获取数据,现需现取,比较靠谱。

实现

一些准备工作:
1. UI :MyExpandableListView (由于是嵌套在ScrollView中,所有需要自定义)
2. Adapter:MyMaterialExpandableAdapter
3. 数据源:Group的数据源,Child的数据源

贴代码
1. MyExpandableListView

/**
 * 重写ExpandableListView以解决ScrollView嵌套ExpandableListView
 * Created by llay on 2017/9/12.
 */

public class MyExpandableListView extends ExpandableListView {
   

    public MyExpandableListView(Context context) {
        super(context);
    }

    public MyExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyExpandableListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

}
  1. MyMaterialExpandableAdapter
public class MyMaterialExpandableAdapter extends BaseExpandableListAdapter {
   

    private List<String> groupMapList;
    private List<List<MaterialEntity>> childMapList;
    private Context 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值