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
?
A layout manager is an object that implements the LayoutManager interface* and determines the size and position of the components within a container. Although components can provide size and alignment hints, a container's layout manager has the final say on the size and position of the components within the container.
A layout defines the structure for a user interface in your app, such as in an activity. All elements in the layout are built using a hierarchy of View and ViewGroup objects. A View usually draws something the user can see and interact with.
Use FrameLayout, RelativeLayout or a custom layout instead. Those layouts will adapt to different screen sizes, whereas AbsoluteLayout will not. Definitely right. I recommend RelativeLayout since it keeps the view hierachy flat.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With