Android using LayoutManagers with RecyclerView

LayoutManager behaviour in Android by using  Recyclerview.The RecyclerView provides built in LayoutManagers and implement vertical scrolling list, horizontal scrolling list , a uniform grid, staggered grid collections.

RecyclerView : The RecyclerView  is more advanced and flexible version of ListView. We can display large number of data sets and that can be maintained  with  limited number of views.

The LayoutManager : The LayoutManager is used for
1. To positioning the items of RecyclerView.
2. Type of RecyclerView like list or grid.
3. The orientations,the item display in Horizontal or Vertical.
4. To calculate size and position of each item in RecyclerView.

RecyclerView provides bulit-in LayoutManagers.

  1. LinearLayoutManager.
  2. GridLayoutManager.
  3. StraggeredGridLayoutManager.

1.LinearLayoutManager is used to display  items in horizontal and vertical scroll.

//initialize recyclerview
  recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

//fixed size of recyclerview layout
  recyclerView.setHasFixedSize(true);

//intialize linear layout manager vertically
  LinearLayoutManager linearVertical = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);


//attach linear layout to recyclerview
  recyclerView.setLayoutManager(linearVertical);


//initialize linear layout manager horizontally
  LinearLayoutManager linearHorizontal = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);

//attach linear layout to recyclerview
  recyclerView.setLayoutManager(linearHorizontal);

//initialize adapter
  notificationAdapter = new NotificationAdapter(MainActivity.this,recyclerView,dat);

//attach adapter to recyclerview
  recyclerView.setAdapter(notificationAdapter)

 

2.GridLayoutManager is used to display items in a grid.

//here the second parameter is for number of rows.
//third parameter is for horizontal scroll.
//fourth paramter is boolean,when it set to false,layout from start to end
  GridLayoutManager gridHorizontal = new GridLayoutManager(this,2,GridLayoutManager.HORIZONTAL,false);

//attach layout manger to recyclerview
  recyclerView.setLayoutManager(gridLayoutManager);

 //for vertical scroll
  //second parameter is for number of columns  
  GridLayoutManager gridVertical = new GridLayoutManager(this,3,GridLayoutManager.VERTICAL,false);

  recyclerView.setLayoutManager(gridVertical);

 

3.StaggeredGridLayoutManager is used to display items in staggered grid.

//here first param represents number of columns
//second param represents vertical scroll
  StaggeredGridLayoutManager staggeredGridVertical=new     
  StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
  recyclerView.setLayoutManager(staggeredGridVertical);

Conclusion: for complete code refer,

https://github.com/DesignString/RecyclerView

 

Leave a Reply

Your email address will not be published. Required fields are marked *