小编spe*_*e10的帖子

在RecyclerView中更改不同视图的布局管理器

我实现了一个可扩展的recyclerview,其子元素是列表的一部分.我按照这个代码.这是它的工作原理,

使用RecyclerView实现ExpandableListView简要描述如下.列表模型有一个附加参数"type",用于标识项目是标题还是子项.通过检查此参数,适配器会扩展与该类型对应的视图和视图.如果类型是HEADER,它将膨胀标题项的布局,其中包含TextView和ImageView,用于指示子树是否展开.

现在,我想要做的是使扩展的布局成为网格.我通常会通过将布局管理器设置为GridLayoutManager来实现这一点,但在这种情况下,我只使用一个recyclerview,这意味着我无法在不更改标题的情况下更改布局管理器,最终导致整个recyclerview变为网格包括标题.

我的问题是:如何在适配器内仅为几个布局更改布局管理器?

编辑:我添加了一些代码.

Recyclerview适配器:

public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

// These are constants that are used to determine if the item is a child or a header and is defined with each item from the data model
public static final int HEADER = 0;
public static final int CHILD = 1;

private List<Item> data;

public ExpandableListAdapter(List<Item> data) {
    this.data = data;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
    View view = null;

    LayoutInflater …
Run Code Online (Sandbox Code Playgroud)

android android-layout recycler-adapter android-recyclerview

7
推荐指数
1
解决办法
6274
查看次数