RecyclerView LayoutManagers

Nandish swarup
4 min readMar 13, 2021

RecyclerView listing can be defined in more than one way using different LayoutManager i.e items can be arranged in different ways depending upon what kind LayoutManager one might be using.

RecyclerView

RecyclerView is a more advanced, flexible and optimized version of ListView.
It is used when you want to display a list of things. This widget is used to display a large number of data sets that can be scrolled efficiently

The RecyclerView model does a lot of optimization work so you don't have to:

  • When the list is first populated, it creates and binds some view holders on either side of the list. For example, if the view is displaying list positions 0 through 9, the RecyclerView creates and binds those view holders, and might also create and bind the view holder for position 10. That way, if the user scrolls the list, the next element is ready to display.
  • As the user scrolls the list, the RecyclerView creates new view holders as necessary. It also saves the view holders which have scrolled off-screen, so they can be reused. If the user switches the direction they were scrolling, the view holders which were scrolled off the screen can be brought right back. On the other hand, if the user keeps scrolling in the same direction, the view holders which have been off-screen the longest can be re-bound to new data. The view holder does not need to be created or have its view inflated; instead, the app just updates the view's contents to match the new item it was bound to.
  • When the displayed items change, you can notify the adapter by calling an appropriate RecyclerView.Adapter.notify…() method. The adapter's built-in code then rebinds just the affected items.

LayoutManager

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user. By changing the LayoutManager a RecyclerView can be used to implement a standard vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections and more.

Vertical List LayoutManager

This is perhaps one of the simplest and widely used lists that one might have already seen in various applications where items are arranged one below the other.

Code Snippet:

val linearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
rvList.layoutManager=linearLayoutManager;

Horizontal List LayoutManager

Items are arranged horizontally and the user can scroll from left to right or right to left

Code Snippet:

val linearLayoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
rvList.layoutManager=linearLayoutManager

GridLayout LayoutManager

If you want to arrange items like items are arranged in the gallery i.e in columns then you’ll have to use Gridlayout. To define the number of columns, you need to set the value of spanCount in the constructor as shown in the code snippet.

Code Snippet:

var spanCount=2
val gridLayout= GridLayoutManager(this,spanCount,GridLayoutManager.VERTICAL,false)
rvList.layoutManager=gridLayout

StaggeredLayout LayoutManager

StaggeredLayout is like GridLayout but items are arranged according to content in grids i.e each grid is of varying size and shows asymmetric items in view

Code Snippet:

var spanCount=2
val gridLayout= StaggeredGridLayoutManager(spanCount,StaggeredGridLayoutManager.VERTICAL)
binding.rvList.layoutManager=gridLayout

Other parameters of various Layoutmanager constructor
spanCount

The number of columns or rows in the grid
orientation
Layout orientation. Should be HORIZONTAL or VERTICAL.
reverseLayout
When set to true, layout is rendered from end to start

That’s it for now!!
Thanks for reading and don’t forget to share with your fellow developers :)
This post was originally posted on
CodeTheraphy.com.

For more articles follow me on Medium and CodeTheraphy.com.
You can also connect me on
LinkedIn.!!

--

--