I have a RecyclerView using StaggeredGridLayoutManager, 2 columns for example, I want to set different backgrounds for the first column and second column, how to do this, thanks in advance.
Asked
Active
Viewed 311 times
1
-
Could you include the adapter code? – Hai Hack Dec 18 '17 at 09:30
2 Answers
1
Base on this answer, I guess you can try:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
StaggeredGridLayoutManager.LayoutParams lp =
(StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
switch (lp.getSpanIndex()) {
case 0:
holder.itemView.setBackgroundColor(Color.GREEN);
break;
case 1:
holder.itemView.setBackgroundColor(Color.RED);
break;
}
}

Hai Hack
- 948
- 13
- 24
-
This is not ok, the result is always StaggeredGridLayoutManager.LayoutParams#INVALID_SPAN_ID. – twlkyao Dec 21 '17 at 09:53
0
I solve this by set a custom RecyclerView.ItemDecoration
, override the method getItemOffsets
and use StaggeredGridLayoutManager.LayoutParams#getSpanIndex
to set different backgrounds.

twlkyao
- 14,302
- 7
- 27
- 44