Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StaggeredGridLayoutManager change spanCount dynamically RecyclerView

How can we change the spanCount of RecyclerView dynamically?

My code create 3 column of cards in RecyclerView:

 StaggeredGridLayoutManager mFavouratesLayoutManager;
 mFavouratesLayoutManager = new StaggeredGridLayoutManager(3,
                StaggeredGridLayoutManager.VERTICAL);

I'm trying to make the adapter like:

  • one element at top row
  • 2 elements in second row
  • 3 items below that
like image 401
Bora Avatar asked Dec 26 '14 12:12

Bora


1 Answers

StaggeredGridLayoutManager does not support that. You can change the span count but it will effect every row. Your only option is to have full rows w/ SGLM.

If you use GridLayoutManager, you can achieve that by setting span count to 6, then providing a SpanSizeLookup that returns

  • 6 for first item
  • 3 for items 1 and 2
  • 2 for items 3 4 5

Etc.

like image 181
yigit Avatar answered Oct 20 '22 14:10

yigit