更改RecyclerView中不同视图的布局管理器 [英] Changing layout managers for different views in RecyclerView

查看:117
本文介绍了更改RecyclerView中不同视图的布局管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个可扩展的recyclerview,其中的子元素是列表的一部分.我遵循了此代码.就是这样,

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

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

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

我添加了一些代码.

Recyclerview适配器:

 公共类ExpandableListAdapter扩展了RecyclerView.Adapter< RecyclerView.ViewHolder>.{//这些常量用于确定项目是子项还是标头,并与数据模型中的每个项目一起定义public static final int HEADER = 0;public static final int CHILD = 1;私人List< Item>数据;public ExpandableListAdapter(List< Item>数据){this.data =数据;}@Override公共RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int type){视图view = null;LayoutInflater inflater =(LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);//检查项目是标题还是子项,并填充不同的网络布局开关(类型){案例HEADER://如果项目是标题,则添加标题布局view = inflater.inflate(R.layout.list_header,parent,false);ListHeaderViewHolder标头=新的ListHeaderViewHolder(view);返回头案例CHILD://如果该项目是子项,则为其添加子项布局view = inflater.inflate(R.layout.list_child,parent,false);ListChildViewHolder child = new ListChildViewHolder(view);归还孩子;}返回null;}public void onBindViewHolder(RecyclerView.ViewHolder持有人,int位置){最终商品item = data.get(position);//根据项目是标题还是子项来绑定不同的布局开关(item.getType()){案例HEADER://...案例CHILD://...}}@Overridepublic int getItemViewType(int position){返回data.get(position).type;}@Overridepublic int getItemCount(){返回data.size();}//标题项的Viewholder私有静态类ListHeaderViewHolder扩展了RecyclerView.ViewHolder {//...}//子项的Viewholder私有静态类ListChildViewHolder扩展了RecyclerView.ViewHolder {//...} 

这是我声明布局管理器的主要活动:

  recyclerview =(RecyclerView)findViewById(R.id.recyclerview);recyclerview.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); 

解决方案

您可以将布局管理器更改为GridLayoutManager并为页眉定义"span size",例如,如果您想要包含2列的网格,则该页眉应该具有跨度大小2,子级跨度大小1:

  GridLayoutManager glm = new GridLayoutManager(getContext(),2);glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup(){@Overridepublic int getSpanSize(int position){开关(getTypeForPosition(位置)){案例HEADER:返回2;默认:返回1;}}});recyclerView.setLayoutManager(glm); 

有一个带有标题.

I implemented an expandable recyclerview with child elements that are part of the list. I followed this code. This is how it works,

The implementation of ExpandableListView using RecyclerView is briefly described as follows. The list model has an additional parameter "type" that identifies whether the item is a header or child. By checking this parameter, the adapter inflates view and viewholder corresponding to the type. If the type is HEADER, it will inflate the layout of header item, that contains a TextView and a ImageView for indicating whether the child tree is expanded or not.

Now, what I want to do is make the expanded layout a grid. I would normally do this by setting the layout manager to GridLayoutManager but in this case, I am only using one recyclerview, which means that I can't change the layout manager without changing the header which ends up causing the entire recyclerview to turn into a grid including the headers.

My question is: how would you change the layout manager for only a couple layouts inside the adapter?

Edit: I added some code.

Recyclerview adapter:

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 inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // Check whether the item is a header or child and inflate differnet layouts
    switch (type) {
        case HEADER:
            // Inflate a header layout if the item is a header
            view = inflater.inflate(R.layout.list_header, parent, false);
            ListHeaderViewHolder header = new ListHeaderViewHolder(view);
            return header;
        case CHILD:
            // Inflate a child layout if the item is a child
            view = inflater.inflate(R.layout.list_child, parent, false);
            ListChildViewHolder child = new ListChildViewHolder(view);
            return child;
    }
    return null;
}

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    final Item item = data.get(position);

    // Bind different layouts based on if the item is a header or child
    switch (item.getType()) {
        case HEADER:
            // ...
        case CHILD:
            // ...
    }
}

@Override
public int getItemViewType(int position) {
    return data.get(position).type;
}

@Override
public int getItemCount() {
    return data.size();
}

// Viewholder for the header items
private static class ListHeaderViewHolder extends RecyclerView.ViewHolder {
    // ...
}

// Viewholder for the child items
private static class ListChildViewHolder extends RecyclerView.ViewHolder {
    // ...
}

And this is the main activity where I declare the layout manager:

recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

解决方案

You can change the layout manager to GridLayoutManager and define the "span size" for the header, for example, if you want the grid with 2 columns, the header should have span size 2 and the children span size 1:

    GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
    glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch(getTypeForPosition(position)) {
                case HEADER:
                    return 2;
                default:
                    return 1;
            }
        }
    });
    recyclerView.setLayoutManager(glm);

There is a full example of expandable grid with headers here using this library.

这篇关于更改RecyclerView中不同视图的布局管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆