I have seen many types of layout managers like:
LineraLayoutManager
RecyclerView.LayoutManager
ListViewLayoutManager
What actually LayoutManager
is and why it is used and what are the different types of LayoutManager
s? Do in android all UI components like Button
, TextView
, EditText
etc has their own LayoutManagers
?
Adapters are only responsible for creating and managing views for items (called ViewHolder), these classes do not decide how these views are arranged when displaying them. Instead, they rely on a separate class called LayoutManager.
LayoutManager is a class that tells Adapters how to arrange those items . For example, you might want those items in a single row top to bottom or you may want them arranged in Grids like Gallery. Instead of writing this logic in your adapter, you write it in LayoutManager and pass that LayoutManager to View (RecyclerView).
A beginner might ask, Why does it work like that?
Answer: Flexibility . By using this pattern, whenever you want to change the arrangement of items, you don't have to modify Adapters or RecyclerView. Moreover, without this approach, you would be limited to the functionality provided by the pre-built class. You can build your own LayoutManager by extending the LayoutManager class.
There are also some commonly used LayoutManagers included for you to use, I'll list two of them
LayoutManager is a inner class of RecyclerView
. According to documentation LayoutManager
and it's subclasses are responsible for measuring and positioning item views in RecyclerView
.
Known direct subclasses
LinearLayoutManager, StaggeredGridLayoutManager
Known indirect subclasses
GridLayoutManager, WearableLinearLayoutManager
So LinearLayoutManager
allows RecyclerView
to show data as a list and GridLayoutManager
allows to organize items in scrollable tables/grids.
Answering your second question - no, other components usually do not have their own LayoutManager
's, but there may be some exceptions.
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. Several stock layout managers are provided for general use.
If the LayoutManager specifies a default constructor or one with the signature (Context, AttributeSet, int, int), RecyclerView will instantiate and set the LayoutManager when being inflated. Most used properties can be then obtained from getProperties(Context, AttributeSet, int, int). In case a LayoutManager specifies both constructors, the non-default constructor will take precedence.
its main work is to manage the layout for the large data-set provided by adapter. It positions each item views into it's appropriate position in the RecycleView. Also, It re-uses the views that are no longer visible to the user. During this, It may ask the adapter to replace the contents of that view with a different element from data-set. Recycling(or Re-using) views in this manners improves performance a lot since there is no need to create extra views and perform costly operations like findViewById() etc. in it now. Due to this feature, This widget is named as RecyclerView (Means a widget that re-uses the views).
The Layout managers enable us to control the way in which visual components are arranged in the GUI forms by determining the size and position of components within the containers.
GridLayout: It arranges all the components in a grid of equally sized cells, adding them from the left to right and top to bottom. Only one component can be placed in a cell and each region of the grid will have the same size.
LinearLayout: It arranges all the components of the layout in a single column/row.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.