Use recycler view to create expandable List View.
To complete solution of Expandable list View and Section View.
Multiple View attach in single recycler view.
Make adapter class and extends ExpandableRecyclerViewAdapter to Override getViewHolder method that returns the object of ExpandableViewHolder.
ExpandableRecyclerViewAdapter adapter = new ExpandableRecyclerViewAdapter(addChild()) {
@Override
protected ExpandableViewHolder getViewHolder(int resource, ViewGroup parent) {
switch (resource) {
case R.layout.list_item:
return new MainViewHolder(parent, resource);
case R.layout.sub_list1:
return new SubItemOneHolder(parent, resource);
case R.layout.sub_list2:
return new SubItemTwoHolder(parent, resource);
}
return null;
}
};
public class MainViewHolder extends ExpandableViewHolder {
private TextView itemName;
private TextView itemCount;
public MainViewHolder(@NonNull ViewGroup parent, int resource) {
super(parent, resource);
}
@Override
public void bindIds(View itemView) {
itemName = (TextView) itemView.findViewById(R.id.item_name);
itemCount = (TextView) itemView.findViewById(R.id.item_count);
}
@Override
public void setData(ExpandableAttribute data) {
itemName.setText(((ExpandableParent) data).getItemName());
itemCount.setText(((ExpandableParent) data).getItemCount());
}
@Override
public void onCollapse(ExpandableAttribute data) {
// do on Collapse the view
}
@Override
public void onExpand(ExpandableAttribute data) {
}
}
public class SubItemOneHolder extends ExpandableViewHolder {
private CheckBox checkBox;
public SubItemOneHolder(@NonNull ViewGroup parent, int resource) {
super(parent, resource);
}
@Override
public void bindIds(View itemView) {
checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
}
@Override
public void setData(ExpandableAttribute data) {
checkBox.setText(((ExpandableChild) data).getValue());
}
@Override
public void onCollapse(ExpandableAttribute data) {
}
@Override
public void onExpand(ExpandableAttribute data) {
}
}
public class SubItemTwoHolder extends ExpandableViewHolder {
private RadioButton radioButton;
public SubItemTwoHolder(@NonNull ViewGroup parent, int resource) {
super(parent, resource);
}
@Override
public void bindIds(View itemView) {
radioButton = (RadioButton) itemView.findViewById(R.id.radio_btn);
}
@Override
public void setData(ExpandableAttribute data) {
radioButton.setText(((ExpandableChild) data).getValue());
}
@Override
public void onCollapse(ExpandableAttribute data) {
}
@Override
public void onExpand(ExpandableAttribute data) {
}
}
Use following method to handle RecyclerView
-
setExpand(boolean isExpand)
to define for expand or collapse the child views.
if isExpand == true then its need to expand childs. -
hasChild(bolean hasChild)
to define if ExpandableAttribute has any Child. -
doNotCollapse()
to define if parent or child doesn't need to collapse on click.
public class ExpandableParent extends ExpandableAttribute {
private String itemName;
private String itemCount;
public ExpandableParent(int resourceId, String itemName, String itemCount) {
super(resourceId);
this.itemCount = itemCount;
this.itemName = itemName;
}
protected ExpandableParent(int resourceId, ArrayList<ExpandableAttribute> expandableChildren) {
super(resourceId, expandableChildren);
}
public String getItemName() {
return itemName;
}
public String getItemCount() {
return itemCount;
}
}
public class ExpandableChild extends ExpandableAttribute {
private String name;
public ExpandableChild(int resourceId) {
super(resourceId);
}
public void setValue(String name) {
this.name = name;
}
public String getValue() {
return name;
}
}