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);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章