27
2017
09

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

前述

一般我们在使用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 mContext;

    public MyMaterialExpandableAdapter(Context context, List<String> groupMapList, List<List<MaterialEntity>> childMapList) {
        mContext = context;
        this.groupMapList = groupMapList;
        this.childMapList = childMapList;
    }

    @Override
    public int getGroupCount() {
        return groupMapList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childMapList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupMapList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childMapList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = convertView;
        GroupHolder holder = null;
        if (view == null) {
            holder = new GroupHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_material_group_expand, null);
            holder.groupName = view.findViewById(R.id.item_group_left_text);
            holder.groupRightText = view.findViewById(R.id.item_group_right_text);
            view.setTag(holder);
        } else {
            holder = (GroupHolder) view.getTag();
        }
        //判断是否已经打开列表
        if (isExpanded) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.icon_arr_up);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            holder.groupRightText.setCompoundDrawables(null, null, drawable, null);
        } else {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.icon_arr_down);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            holder.groupRightText.setCompoundDrawables(null, null, drawable, null);
        }
        String _title = groupMapList.get(groupPosition);
        if (!TextUtils.isEmpty(_title)) {
            holder.groupName.setText(_title);
        } else {
            holder.groupName.setText("");
        }
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = convertView;
        ChildHolder holder = null;
        if (view == null) {
            holder = new ChildHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_material_child_expand, null);
            holder.mScope = view.findViewById(R.id.item_material_scope);
            holder.mName = view.findViewById(R.id.item_material_name);
            holder.mPhone = view.findViewById(R.id.item_material_phone);
            holder.mLocaiton = view.findViewById(R.id.item_material_location);
            holder.mMaterial = view.findViewById(R.id.item_material_material);
            view.setTag(holder);
        } else {
            holder = (ChildHolder) view.getTag();
        }
        String _scope = childMapList.get(groupPosition).get(childPosition).getScope();
        if (!TextUtils.isEmpty(_scope)) {
            holder.mScope.setText(_scope);
        } else {
            holder.mScope.setText("");
        }
        String _name = childMapList.get(groupPosition).get(childPosition).getTeamName();
        if (!TextUtils.isEmpty(_name)) {
            holder.mName.setText(_name);
        } else {
            holder.mName.setText("");
        }
        String _phone = childMapList.get(groupPosition).get(childPosition).getTeamPhone();
        if (!TextUtils.isEmpty(_phone)) {
            holder.mPhone.setText(_phone);
        } else {
            holder.mPhone.setText("");
        }
        String _location = childMapList.get(groupPosition).get(childPosition).getStorageLocation();
        if (!TextUtils.isEmpty(_location)) {
            holder.mLocaiton.setText(_location);
        } else {
            holder.mLocaiton.setText("");
        }
        String _material = childMapList.get(groupPosition).get(childPosition).getMaterialName();
        if (!TextUtils.isEmpty(_material)) {
            holder.mMaterial.setText(_material);
        } else {
            holder.mLocaiton.setText("");
        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupHolder {
        public TextView groupName;
        public TextView groupRightText;
    }

    class ChildHolder {
        public TextView mScope;
        public TextView mName;
        public TextView mPhone;
        public TextView mLocaiton;
        public TextView mMaterial;
    }
}

适配器Group和Child的布局很简单。

  1. 数据源自己上哦

重点到了

1.mHistoryExpandMaterialListView.setOnGroupClickListener(this);
监听ExpandListView的Group的点击事件。

@Override
    public boolean onGroupClick(ExpandableListView _expandableListView, View _view, int _i, long _l) {
        //如果分组被打开 直接关闭
        if (mHistoryExpandMaterialListView.isGroupExpanded(_i)) {
            mHistoryExpandMaterialListView.collapseGroup(_i);
        } else {
            String _groupString = (String) mMyMaterialExpandableAdapter.getGroup(_i);
            if (!TextUtils.isEmpty(_groupString)) {
                //记录点击的第几个
                mClickIndex = _i;
                //Engine中请求数据
                mHistoryDetailEngine.getCailiaoInfo(_groupString);
            }
        }
        return true;
    }

2.加载完成数据后,需要打开Group,显示加载的数据

@Override
    public void getCailiaoOnSuccess(List<MaterialEntity> _listEntities) {
        mLists.get(mClickIndex).clear();
        mLists.get(mClickIndex).addAll(_listEntities);
        //打开记录的第几个Group进行显示
        mHistoryExpandMaterialListView.expandGroup(mClickIndex, true);
    }
上一篇:Android 集成 EaseUi 下一篇:Vue 之Element ui 中upload的那些事