前述
一般我们在使用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);
}
}
- MyMaterialExpandableAdapter
public class MyMaterialExpandableAdapter extends BaseExpandableListAdapter {
private List<String> groupMapList;
private List<List<MaterialEntity>> childMapList;
private Context