Kavya
Kavya

Reputation: 37

How to expand a child on clicking a particular item from parent in a list?

enter image description here

I want to expand/collapse the child View only upon clicking on particular Text view/button like shown in the image.

Upvotes: 1

Views: 361

Answers (1)

Niel_patel
Niel_patel

Reputation: 74

You can use ExpandableRecyclerView.

By including your GroupViewHolder and ChildViewHolder in the definition of the class, you'll see that the onCreateGroupViewHolder and onCreateChildViewHolder methods return the correct type :+1:

public class GenreAdapter extends ExpandableRecyclerViewAdapter<GenreViewHolder, ArtistViewHolder> {

  public GenreAdapter(List<? extends ExpandableGroup> groups) {
    super(groups);
  }

  @Override
  public GenreViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.list_item_genre, parent, false);
    return new GenreViewHolder(view);
  }

  @Override
  public ArtistViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.list_item_artist, parent, false);
    return new ArtistViewHolder(view);
  }

  @Override
  public void onBindChildViewHolder(ArtistViewHolder holder, int flatPosition, ExpandableGroup group,
      int childIndex) {
    final Artist artist = ((Artist) group).getItems().get(childIndex);
    holder.setArtistName(artist.getName());
  }

  @Override
  public void onBindGroupViewHolder(GenreViewHolder holder, int flatPosition,
      ExpandableGroup group) {
    holder.setGenreTitle(group);
  }
}

Please checkout the following GitHub repository for complete guide to how can it implement,

ExpandableRecyclerView

You get the result look like below screenshot,

enter image description here

Upvotes: 1

Related Questions