RecyclerView LayoutManager Types

Kamal Vaid
2 min readDec 29, 2018

--

RecyclerView?

As we know RecyclerView is more advanced and efficient version of ListView. Using RecyclerView, we can display and maintained large number of data using limited number of views.

LayoutManagers?

We can manage/create RecyclerView behaviours using LayoutMangers. RecyclerView uses pre built LayoutMangers and Implements different behaviours such as :

  1. Vertical/Horizontal List(Using LinearLayoutManger)
  2. Uniform Grid List (Using GridLayoutManager)
  3. Staggered Grid List (Using StaggeredGridLayoutManager)

LayoutManager is For?

LayoutManger Helps us in :

  1. Orientation of the List (Vertical orHorizontal).
  2. Manages positions of the items.
  3. Defines type of list i.e List, Gird or staggered.
  4. Calculates position and size of items of individual items in the RecyclerView.

LinearLayoutManager

In order to show list in a vertical or horizontal fashion we make use of LinearLayoutManager.

Below is the code snippet :

  1. Horizontal LinearLayoutManager
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.HORIZONTAL

recyclerview.layoutManager=layoutManager

2. Vertical LinearLayoutManager

val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.VERTICAL

recyclerview.layoutManager=layoutManager
RecyclerView with Vertical and Horizontal behaviours. (Image references from https://enoent.fr/images/articles/recyclerview-basics/recyclerview-linear-horizontal-reversed.png)

GridLayoutManager

To display list items in the form of grid similar to Photos app inside our phone we use the LayoutManager.

GridLayoutManager accepts two parameters in below code snippet i.e Context and SpanCount.

Below is the code snippet :

val layoutManager =GridLayoutManager(this,2)

recyclerview.layoutManager=layoutManager
RecyclerView with Grid (Image reference from https://enoent.fr/images/articles/recyclerview-basics/recyclerview-grid-3-vertical-reversed.png)

StaggeredLayoutManager

This LayoutManager comes into play when we want to display items in random and zig zag way. In short individual items may varies in terms of dimensions (width or height).

StaggeredGridLayoutManager accepts two parameters in below code snippet i.e SpanCount and Orientation (Vertical or Horizontal).

Below is the code snippet :

val layoutManager = StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)recyclerview.layoutManager=layoutManager
RecyclerView with Staggered Grid(Image reference from https://enoent.fr/images/articles/recyclerview-basics/recyclerview-staggered-grid.png)

That’s what all we need to know if we want handle RecyclerView with different behaviours. Above mentioned code snippets are in Kotlin and will be posting the whole tutorial with Github link soon.

Happy Coding!!

Cheers!!

--

--

Kamal Vaid

GDE For Google Assistant,Technology Lead at Infosys Ltd, Organiser Google Developer Group Chandigarh, Lead Facebook Developer Circle Chandigarh