如何在android中使用Expandable RecyclerView

时间:2017-06-10 04:29:03

标签: android android-recyclerview recycler-adapter

在我的应用程序中,我想使用category和subCategory作为列表​​UI等项目,我从服务器获取此数据:

    Category1
      |
      |--SubCat1_1
         |
         |--subSubCat1_1
      |
      |--SubCat2
         |
         |--subSubCat1_2_1
         |
         |--subSubCat1_2_2
         |
         |--subSubCat1_2_3
    Category2
      |
      |--SubCat2_1
         |
         |--subSubCat2_1
      |
      |--SubCat2_2
         |
         |--subSubCat2_2_1
         |
         |--subSubCat2_2_2
         |
         |--subSubCat2_2_3

and more ...

为此我想要RecyclerView,但我不知道它怎么样?

我怎么能?

1 个答案:

答案 0 :(得分:0)

您可以使用this library

以下是它的一些用法,您可以在上面的链接中找到更多提示:

让我们说你是摇滚明星:吉他:你想要建立一个应用程序来显示你最喜欢的流派列表以及他们的顶级艺术家名单。

首先,定义您的自定义ExpandableGroup类:

public class Genre extends ExpandableGroup<Artist> {

public Genre(String title, List<Artist> items) {
    super(title, items);
  }
}

接下来,让我们创建ChildViewHolder和GroupViewHolder。这些都是普通ol&#39; RecyclerView.ViewHolders因此可以实现您可能需要的任何视图通胀和绑定方法。

public class GenreViewHolder extends GroupViewHolder

  private TextView genreTitle;

  public GenreViewHolder(View itemView) {
    super(itemView);
    genreTitle = itemView.findViewById(R.id.genre_title);
  }

  public void setGenreTitle(ExpandableGroup group) {
    genreTitle.setText(group.getTitle());
  }
}
public class ArtistViewHolder extends ChildViewHolder {

  private TextView artistName;

  public ArtistViewHolder(View itemView) {
    super(itemView);
    artistName = itemView.findViewById(R.id.artist_name);
  }

  public void onBind(Artist artist) {
    artistName.setText(artist.getTitle());
  }
}

现在我们已准备好迎接多汁的部分 - 让我们制作我们的ExpandableRecyclerViewAdapter

通过在类的定义中包含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);
  }
}

最后,您需要一个Activity或Fragment来托管您的适配器。一旦你完成并运行,所有剩下的就是如果用列表实例化你想要的新GenreAdapter

public class GenreActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    ...

    List<Genre> genres = getGenres();
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);

    //instantiate your adapter with the list of genres
    GenreAdapter adapter = new GenreAdapter(genres);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

    ...

  }
}

如果要保存适配器的展开和折叠状态,则必须显式调用调用Activity中的适配器onSaveInstanceState()onRestoreInstanceState()

public class GenreActivity extends Activity {

  ...

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    adapter.onSaveInstanceState(outState);
  }

  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    adapter.onRestoreInstanceState(savedInstanceState);
  }
}

收听展开/折叠事件

如果要在适配器外注册ExpandCollapseListener,只需在ExpandableRecyclerViewAdapter上调用setOnGroupExpandCollapseListener

adapter.setOnGroupExpandCollapseListener(new GroupExpandCollapseListener() {
    @Override
    public void onGroupExpanded(ExpandableGroup group) {

    }

    @Override
    public void onGroupCollapsed(ExpandableGroup group) {

    }
  });