Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple LayoutManager in RecyclerView

Tags:

android

I made RecyclerView with StaggeredGridLayoutManager like this:

staggered

I need to add some TextView (one colum like in LinearLayoutManager) in above it. It's like LinearLayoutManager in above and StaggeredGridLayoutManager in bottom. Something like this:

enter image description here

How can i achieve that? Really confused in here, please help.

UPDATE 29-OCT-2015:

I solve it in conjunction with the answer by denis_lor and mato in here: Span multiple columns with RecyclerView

like image 476
nafsaka Avatar asked Oct 29 '15 09:10

nafsaka


2 Answers

The key is in your adapter, you basically rely on the viewType and then decide what kind of viewholder and layout to inflate. Here an example:

private static final int TYPE_FIRST_ITEM = 0;
private static final int TYPE_ITEM = 1;

...

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   if (viewType == TYPE_FIRST_ITEM) {
      // inflate your view holder for the first item
   } else {
      // here inflate your view holder for all the other items
   }

...

@Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_FIRST_ITEM;
        } else {
            return TYPE_ITEM;
        }
    }

So basically you have 1 adapter, 2 (or more) viewHolder and thats all. If your adapter should have 2 kind of layout you use 2 viewHolder and layouts, otherwise you use more if you need also a footer layout.

like image 156
denis_lor Avatar answered Sep 28 '22 22:09

denis_lor


What do you want to happen when scrolling?

If the top view should remain visible, you can wrap the RecyclerView with a LinearLayout with 2 children: TextView and RecyclerView.

If you want it to scroll out, you can use LayoutManager.setSpanSizeLookup, something like this:

            layoutManager.setSpanSizeLookup(new SpanSizeLookup() {

                @Override
                public int getSpanIndex(int position, int spanCount) {
                    return position % spanCount;
                }

                @Override
                public int getSpanSize(int position) {
                    if (position != 0) {
                        return layoutManager.getSpanCount();
                    }
                    return 1;
                }
            });
like image 31
marmor Avatar answered Sep 28 '22 21:09

marmor