9

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

Community
  • 1
  • 1
nafsaka
  • 940
  • 2
  • 12
  • 17
  • Did you check this https://github.com/karumi/dividers ? You can customize your layoutmanager also. – Piyush Oct 29 '15 at 09:18
  • @Piyush Gupta. I'll check that – nafsaka Oct 29 '15 at 09:19
  • @Piyush Gupta Looks like it's about customize RecyclerView separator. Sadly, not this. I don't want to change the separator. I want the upper part is some information ( in one column) and the content (in staggered grid 2 column). – nafsaka Oct 29 '15 at 09:29

2 Answers2

7

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.

denis_lor
  • 6,212
  • 4
  • 31
  • 55
  • 2
    If i use this, it will still in StaggeredGridLayoutManager, which means, the view will be staggered. I want it to be in something like LinearLayoutManager (one column). – nafsaka Oct 29 '15 at 09:31
  • 1
    Thank you for the hint, i'll try it. – nafsaka Oct 29 '15 at 09:32
  • Can't change LayoutManger inside Adapter. I put the code to change layout manager into getItemViewType. Error: `Attempt to invoke virtual method 'int android.view.View.getLayoutDirection()' on a null object reference` – nafsaka Oct 29 '15 at 10:25
  • Glad you figured out! :D – denis_lor Oct 29 '15 at 14:32
5

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;
                }
            });
marmor
  • 27,641
  • 11
  • 107
  • 150