Set span for items in GridLayoutManager using SpanSizeLookup

The problem was that header should have span size of 2, and regular item should have span size of 1. So correct implementations is: mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { switch(mAdapter.getItemViewType(position)){ case MyAdapter.TYPE_HEADER: return 2; case MyAdapter.TYPE_ITEM: return 1; default: return -1; } } });

RecyclerView GridLayoutManager: how to auto-detect span count?

Personaly I don’t like to subclass RecyclerView for this, because for me it seems that there is GridLayoutManager’s responsibility to detect span count. So after some android source code digging for RecyclerView and GridLayoutManager I wrote my own class extended GridLayoutManager that do the job: public class GridAutofitLayoutManager extends GridLayoutManager { private int columnWidth; private … Read more

Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView)

Short answer For those who are already familiar with setting up a RecyclerView to make a list, the good news is that making a grid is largely the same. You just use a GridLayoutManager instead of a LinearLayoutManager when you set the RecyclerView up. recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns)); If you need more help than that, then … Read more

Android Recyclerview GridLayoutManager column spacing

Following code works well, and each column has same width: public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; private int spacing; private boolean includeEdge; public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) { this.spanCount = spanCount; this.spacing = spacing; this.includeEdge = includeEdge; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { … Read more