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.
- LinearLayoutManager.
- GridLayoutManager.
- 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);
More Posts
How to run a cron job in laravel hosted in linux server
what is a cron job? cron is a Linux utility which schedules a command or script on your server to […]
By Smruti Ranjan
Protect your website with password on nginx ubuntu 16.04
Sometimes it is necessary to run a website for internal purpose within organisation or you might be want to share […]
By Murugan D