微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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.

现在,我想要做的是使扩展的布局成为网格.我通常会通过将布局管理器设置为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 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,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 {
    // ...
}

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

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

解决方法

您可以将布局管理器更改为GridLayoutManager并定义标题的“跨度大小”,例如,如果您希望网格具有2列,则标题应具有跨度大小2且子跨度大小为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);

使用此library一个可扩展网格的完整示例,标题here.

原文地址:https://www.jb51.cc/android/314086.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐