StaggeredGridLayoutManager
doesn't seem to allow customising a cell width or span multiple columns (except full span) for vertical orientation.
What is a preferred LayoutManager
for organising cells as shown above?
P.S. I just want to know how to customise cell width and not height with StaggeredGridLayoutManager
. I know height can be customised as implemented in this sample.
public class VerticalStaggeredGridFragment extends RecyclerFragment {
public static VerticalStaggeredGridFragment newInstance() {
VerticalStaggeredGridFragment fragment = new VerticalStaggeredGridFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
protected RecyclerView.LayoutManager getLayoutManager() {
return new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
}
@Override
protected RecyclerView.ItemDecoration getItemDecoration() {
return new InsetDecoration(getActivity());
}
@Override
protected int getDefaultItemCount() {
return 100;
}
@Override
protected SimpleAdapter getAdapter() {
return new SimpleStaggeredAdapter();
}
}
Adapter
public class SimpleStaggeredAdapter extends SimpleAdapter {
@Override
public void onBindViewHolder(VerticalItemHolder itemHolder, int position) {
super.onBindViewHolder(itemHolder, position);
final View itemView = itemHolder.itemView;
if (position % 4 == 0) {
int height = itemView.getContext().getResources()
.getDimensionPixelSize(R.dimen.card_staggered_height);
itemView.setMinimumHeight(height);
} else {
itemView.setMinimumHeight(0);
}
}
}
itemView.setMinimumWidth(customWidth)
doesn't affect cell width.
To simplify, I update my grid layout to
How to increase cell 0 width as compared to cell 1 in a StaggeredGridLayoutManager
or any other layout manager?
For everyone else, looking for a solution without simplifying your layout, you can have a look to my answer here, or you can read more.
Big thanks to Nick Butcher! His layout manager called
SpannableGridLayoutManager
is capable of doing this.Download SpannableGridLayoutManager from here
For some reason I had to change this line:
to
to got the manager working.
Set SpannableGridLayoutManger to your RecyclerView
In your case:
Which will give you exactly what is in the first picture of the question.
You can use
StaggeredGridLayoutManager.LayoutParams
to change width and height of cells, What you expect to design follows a simple pattern, If you assign every cell an index (in the order thatStaggeredGridLayoutManager
arranges them by default) you will have this:first declare some constants in adapter to define span types:
Then override
getItemViewType
method in your adapter like this:All you need to do is change layoutparams considering
viewType
of theholder
:My adapter always returns 50 for items count(just a test) and I used a simple layout file for items, It contains a
LinearLayout
and aTextView
to show the position of holder, Remember you should pass 2 forspanCount
(int theStaggeredGridLayoutManager
constructor) because of your design.PS: For lazy people like me maybe it's a simpler way rather than extending my custom
LayoutManager
, But I'm sure there are better ways to achieve this.Update: Updated part of your question is more simpler, You can use
GridLayoutManager
:In this way you set 3 for default span size, Then considering the position of your span, Each item occupies 1, 2 or 3 spans, Something like this: