ExpandableListview with single as well as multiple selection

1.4k views Asked by At

enter image description here

[Adapter class]

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<FilterList>> expandableListDetail;
private boolean checked;
private int lastClickedPosition;

    private List<Boolean> setValueForSeletedFilter;
private String[] keysOfHashmap;

public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                   HashMap<String, List<FilterList>> expandableListDetail, List<Boolean> setValueForSeletedFilter) {
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
    this.setValueForSeletedFilter = setValueForSeletedFilter;

}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .get(expandedListPosition).getSearchFilterValue();
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public View getChildView(final int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String expandedListText = (String) getChild(listPosition, expandedListPosition);
    if (convertView == null) {
        int i = listPosition;
        int j = expandedListPosition;

        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_item, null);



        TextView expandedListTextView = (TextView) convertView
                .findViewById(R.id.expandedListItem);
        expandedListTextView.setText(expandedListText);
        String s = expandableListTitle.get(listPosition);
        final ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_select);

        if (setValueForSeletedFilter.get(expandedListPosition) == true) {
            imageView.setVisibility(View.VISIBLE);
        } else {
            imageView.setVisibility(View.GONE);
        }


    }
    return convertView;
}

private void toggleSelection(int i, View v) {
    int j = i;
    ImageView imageView = (ImageView) v.findViewById(R.id.iv_select);
    imageView.setImageResource(R.drawable.ic_review);
}

@Override
public int getChildrenCount(int listPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .size();
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

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

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

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String listTitle = (String) getGroup(listPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_group, null);
    }
    TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.listTitle);
    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(listTitle);
    return convertView;
}

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

public void setData(){
    notifyDataSetChanged();
}

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
    return true;
}

}

[setting values for adapter class and notifying adapter on changing values]

final HashMap> setValueForSeletedFilter = new HashMap<>(); String header = null; final List booleanList = new ArrayList<>(); final HashMap> stringListHashMap = new HashMap<>(); final List expandableListTitle; for (int i = 0; i < filtersInfos.size(); i++) { stringListHashMap.put(filtersInfos.get(i).getFilterHeaderName(), filtersInfos.get(i).getFilterLists());

        if (filtersInfos.get(i).getFilterHeaderName().equalsIgnoreCase("Distance")) {
            header = filtersInfos.get(i).getFilterHeaderName();
            for (int j = 0; j < filtersInfos.get(i).getFilterLists().size(); j++)
                booleanList.add(false);
        }
    }
    setValueForSeletedFilter.put(header, booleanList);


    expandableListTitle = new ArrayList<String>(stringListHashMap.keySet());
    final CustomExpandableListAdapter adapter = new CustomExpandableListAdapter(this, expandableListTitle, stringListHashMap, booleanList);
    expandableListView.setAdapter(adapter);
    filterDialog.show();

    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {

            Toast.makeText(getApplicationContext(),
                    expandableListTitle.get(groupPosition) + " List Expanded.",
                    Toast.LENGTH_SHORT).show();
        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    expandableListTitle.get(groupPosition) + " List Collapsed.",
                    Toast.LENGTH_SHORT).show();
        }
    });

    final boolean[] checked = {false};
    final int[] lastClickedPosition = {0};
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {

            if (expandableListTitle.get(groupPosition).equalsIgnoreCase("Distance")) {
                booleanList.add(childPosition, true);
                setValueForSeletedFilter.put(expandableListTitle.get(groupPosition), booleanList);
                expandableListView.setAdapter(adapter);
                adapter.notifyDataSetChanged();

            }

            return false;
        }
    });

    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            filterDialog.dismiss();
        }
    });
    done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

on the basis of header make list single or multi selection android.

1

There are 1 answers

0
Ahad Khan On

getChildView() example for my case "Single selection"

@Override
public View getChildView(final int i, final int i1, boolean b, View view,    ViewGroup viewGroup) {
    View v = LayoutInflater.from(context).inflate(R.layout.expandablelv_child, null);

    final CheckBox checkBox = v.findViewById(R.id.expandable_lv_child_cb);
    RelativeLayout quantityContainer = v.findViewById(R.id.expandable_lv_child_quantity_cont);
    final TextView textView = v.findViewById(R.id.expandable_lv_child_quantity_tv);

    checkBox.setText(children.get(i).get(i1).getItemName());

    double d=Double.parseDouble(children.get(i).get(i1).getMinQty());

    int k=(int)d;

    textView.setText(String.valueOf(k));


    quantityContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            QuantityDialog dialog = new QuantityDialog(context, children.get(i).get(i1).getMinQty(),
                    children.get(i).get(i1).getMaxQty(), new DialogSelectable() {
                @Override
                public void onQuantitySelected(String quantity) {
                    if (group[i].parent == 1 && group[i].child == i1)
                        textView.setText(quantity);
                        ExpandableLvAdapter.quantity[i] = quantity;
                }
            });
            dialog.show();
        }
    });

    try {
        // if (model.children.get(i).size() < children.get(i).size()) {
        try {
            model.children.get(i).add(checkBox);
        } catch (Exception e) {
            ArrayList<CheckBox> temp = new ArrayList<>();
            temp.add(checkBox);
            model.children.add(temp);
        }
        //}
    } catch (Exception e) {
        try {
            model.children.get(i).add(checkBox);
        } catch (Exception ex) {
            ArrayList<CheckBox> temp = new ArrayList<>();
            temp.add(checkBox);
            model.children.add(temp);
        }
    }

    if (group[i].parent == 1) {
        model.children.get(i).get(group[i].child).setEnabled(true);
        model.children.get(i).get(group[i].child).setChecked(true);
        model.disable(i, children.get(i).get(group[i].child).getItemName());
    } else {
        model.enable(i);
    }


    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if (b) {
                group[i].parent = 1;
                group[i].child = i1;
                model.disable(i, children.get(i).get(i1).getItemName());
                selected.add(children.get(i).get(i1));
                quantity[i] = children.get(i).get(i1).getMaxQty();
            } else {
                group[i].parent = 0;
                model.enable(i);
            }
        }
    });

    return v;
}