如何在列表中从父项单击特定项目时扩展子项?

时间:2017-12-27 06:52:27

标签: java android android-layout

enter image description here

我想在点击图片中显示的特定文字视图/按钮时展开/折叠子视图。

1 个答案:

答案 0 :(得分:1)

您可以使用 ExpandableRecyclerView

通过在类的定义中包含GroupViewHolder和ChildViewHolder,您将看到onCreateGroupViewHolder和onCreateChildViewHolder方法返回正确的类型:+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);
  }
}

请查看以下GitHub存储库,以获取有关如何实施的完整指南,

ExpandableRecyclerView

您的结果如下图所示,

enter image description here