Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Staggered grid layout manager dynamic number of columns

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:

enter image description here

like image 850
Drake Avatar asked Mar 25 '16 11:03

Drake


People also ask

What is Staggered grid LayoutManager?

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.

What is the difference between a staggered grid and a normal grid?

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).

What is use of GridLayout manager?

GridLayoutManager is used for displaying the data items in grid format and we can easily define the orientation for the items.


1 Answers

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);
}
like image 137
Ravi Theja Avatar answered Oct 11 '22 18:10

Ravi Theja