How i can make dynamic number of columns in RecyclerView
using StaggeredGridLayoutManager
. I already have working RecyclerView
. Here is screen what i need to do:
androidx.recyclerview.widget.StaggeredGridLayoutManager. A LayoutManager that lays out children in a staggered grid formation. It supports horizontal &vertical layout as well as an ability to layout children in reverse. Staggered grids are likely to have gaps at the edges of the layout.
Grid View : It is is a ViewGroup that displays items in a two-dimensional, scrollable grid. In this each Grid is of same size (Height and width). Grid View shows symmetric items in view. Staggered Grid View : It is basically an extension to Grid View but in this each Grid is of varying size(Height and width).
GridLayoutManager is used for displaying the data items in grid format and we can easily define the orientation for the items.
if max columns are two that would be quite simple.For the view that occupy the full width,setFullSpan(true) like in your BindView method of Recyclerview Adapter
//rlm is RecyclerView.LayoutManager passed in constructor or setter in adapter
if (rlm instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
/*
* to make View to occupy full width of the parent
*/
layoutParams.setFullSpan(true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With