I\'m trying to implement a recyclerview that behaves like my sketch below:
The idea is that there is a parent list, when an list item in the parent list is tapp
You can check my library in here
And create something like below code:
public class PurchaseItemRecyclerViewAdapter extends ExpandableRecyclerView.Adapter
{
List itemModels;
public PurchaseItemRecyclerViewAdapter() {
this.itemModels = new ArrayList<>();
itemModels.add(new ItemModel("group 0",3,"subitem :"));
itemModels.add(new ItemModel("group 1",3,"subitem :"));
itemModels.add(new ItemModel("group 2",2,"subitem :"));
itemModels.add(new ItemModel("group 3",1,"subitem :"));
itemModels.add(new ItemModel("group 4",3,"subitem :"));
itemModels.add(new ItemModel("group 5",5,"subitem :"));
}
@Override
public int getGroupItemCount() {
return itemModels.size();
}
@Override
public int getChildItemCount(int i) {
return itemModels.get(i).getSubItemCount();
}
@Override
public String getGroupItem(int i) {
return itemModels.get(i).getParentName();
}
@Override
public String getChildItem(int group, int child) {
return itemModels.get(group).getSubItemPrefix() + child;
}
@Override
protected ExpandableRecyclerView.SimpleGroupViewHolder onCreateGroupViewHolder(ViewGroup parent)
{
return new ExpandableRecyclerView.SimpleGroupViewHolder(parent.getContext());
}
@Override
protected ChildViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType)
{
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.purchase_list_content,parent,false);
return new ChildViewHolder(rootView);
}
@Override
public void onBindGroupViewHolder(ExpandableRecyclerView.SimpleGroupViewHolder holder, int group) {
super.onBindGroupViewHolder(holder, group);
holder.setText(getGroupItem(group));
}
@Override
public void onBindChildViewHolder(ChildViewHolder holder, final int group, int position)
{
super.onBindChildViewHolder(holder, group, position);
holder.name.setText(getChildItem(group, position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemModels.get(group).setParentName("edited Parent");
notifyItemChanged(group);
}
});
}
@Override
public int getChildItemViewType(int i, int i1) {
return 1;
}
public class ChildViewHolder extends RecyclerView.ViewHolder
{
private TextView name;
public ChildViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.item_name);
}
}
}
and this ItemModel class:
public class ItemModel {
String parentName;
int subItemCount;
String subItemPrefix;
public ItemModel(String parentName, int subItemCount, String subItemPrefix) {
this.parentName = parentName;
this.subItemCount = subItemCount;
this.subItemPrefix = subItemPrefix;
}
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
public int getSubItemCount() {
return subItemCount;
}
public void setSubItemCount(int subItemCount) {
this.subItemCount = subItemCount;
}
public String getSubItemPrefix() {
return subItemPrefix;
}
public void setSubItemPrefix(String subItemPrefix) {
this.subItemPrefix = subItemPrefix;
}
}