To show list item by using RecyclerView we need LayoutManager in android have 3 type layout manager
Type of Layout Manager :
1. LinearLayoutManager: It is used for displaying Vertical or Horizontal List.
2. GridLayoutManager: It is used for displaying the items in the form of Grids.
3. StaggeredGridLayoutManager: It is used to show the items in staggered Grid.
RecyclerView recyclerView =findViewById(R.id.recyclerView); // XML ID
// set a LinearLayoutManager with default horizontal orientation and false value for reverseLayout to show the items from start to end
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(yourAdapterObject);
2. GridLayoutManager (Context context, int spanCount): It is used to create a Vertical
RecyclerView recyclerView =findViewById(R.id.recyclerView); // XML ID
// set a GridLayoutManager with default vertical orientation and 3 number of columns
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(yourAdapterObject);
3 GridLayoutManager (Context context, int spanCount, int orientation, boolean reverseLayout):
RecyclerView recyclerView = findViewById(R.id.recyclerView); // XML ID
// set a GridLayoutManager with 3 number of columns , horizontal gravity and false value for reverseLayout to show the items from start to end
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3,LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(yourAdapterObject);
for more details you can visit https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager
https://developer.android.com/reference/androidx/recyclerview/widget/LinearLayoutManager
Type of Layout Manager :
1. LinearLayoutManager: It is used for displaying Vertical or Horizontal List.
2. GridLayoutManager: It is used for displaying the items in the form of Grids.
3. StaggeredGridLayoutManager: It is used to show the items in staggered Grid.
Also we can set orientation of list (HORIZONTAL,VERTICAL)
1. A. Example of LinearLayoutManager :
RecyclerView recyclerView = findViewById(R.id.recyclerView); // XML id
// set a LinearLayoutManager with default orientation
LinearLayoutManager linearLayoutManager = new inearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(yourAdapterObject);
1 .B . Example of LinearLayoutManager (with Orientation ) :
RecyclerView recyclerView =findViewById(R.id.recyclerView); // XML ID
// set a LinearLayoutManager with default horizontal orientation and false value for reverseLayout to show the items from start to end
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(yourAdapterObject);
2. GridLayoutManager (Context context, int spanCount): It is used to create a Vertical
RecyclerView recyclerView =findViewById(R.id.recyclerView); // XML ID
// set a GridLayoutManager with default vertical orientation and 3 number of columns
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(yourAdapterObject);
3 GridLayoutManager (Context context, int spanCount, int orientation, boolean reverseLayout):
RecyclerView recyclerView = findViewById(R.id.recyclerView); // XML ID
// set a GridLayoutManager with 3 number of columns , horizontal gravity and false value for reverseLayout to show the items from start to end
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3,LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(yourAdapterObject);
for more details you can visit https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager
https://developer.android.com/reference/androidx/recyclerview/widget/LinearLayoutManager
No comments:
Post a Comment