简体   繁体   English

Android - 什么是布局管理器?

[英]Android - What is Layout Manager?

I have seen many types of layout managers like:我见过许多类型的布局管理器,例如:

  1. LineraLayoutManager
  2. RecyclerView.LayoutManager
  3. ListViewLayoutManager
  4. etc等等

What actually LayoutManager is and why it is used and what are the different types of LayoutManager s?究竟LayoutManager ,以及为什么使用它,什么是不同类型的LayoutManager S' Do in android all UI components like Button , TextView , EditText etc has their own LayoutManagers ?在 android 中,像ButtonTextViewEditText等所有 UI 组件都有自己的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.适配器只负责创建管理项目的视图(称为 ViewHolder),这些类不决定这些视图在显示时如何排列。 Instead, they rely on a separate class called LayoutManager.相反,它们依赖于一个名为 LayoutManager 的单独类。

LayoutManager is a class that tells Adapters how to arrange those items . LayoutManager 是一个类,它告诉适配器如何安排这些项目 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).不是在您的适配器中编写此逻辑,而是在 LayoutManager 中编写它并将该 LayoutManager 传递给 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.通过使用这种模式,无论何时你想改变items的排列,你都不必修改Adapters或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.您可以通过扩展 LayoutManager 类来构建自己的 LayoutManager。

There are also some commonly used LayoutManagers included for you to use, I'll list two of them还有一些常用的LayoutManagers供大家使用,我列举两个

  • LinearLayoutManager Arranges items in 1 column or 1 row based on orientation. LinearLayoutManager根据方向将项目排列在 1 列或 1 行中。 For example, a horizontal LinearLayoutManager will place items from left to right in 1 row.例如,水平 LinearLayoutManager 会将项目从左到右放置在 1 行中。
  • GridLayoutManager Arranges items in a provided number of rows and columns, much like images in the gallery. GridLayoutManager按照提供的行数和列数排列项目,就像画廊中的图像一样。

LayoutManager is a inner class of RecyclerView . LayoutManagerRecyclerView的内部类。 According to documentation LayoutManager and it's subclasses are responsible for measuring and positioning item views in RecyclerView .根据文档LayoutManager及其子类负责测量和定位RecyclerView项目视图

Known direct subclasses已知的直接子类

LinearLayoutManager, StaggeredGridLayoutManager线性布局管理器、交错网格布局管理器

Known indirect subclasses已知的间接子类

GridLayoutManager, WearableLinearLayoutManager GridLayoutManager、WearableLinearLayoutManager

So LinearLayoutManager allows RecyclerView to show data as a list and GridLayoutManager allows to organize items in scrollable tables/grids.所以LinearLayoutManager允许RecyclerView将数据显示为列表,而GridLayoutManager允许在可滚动表/网格中组织项目。

Answering your second question - no, other components usually do not have their own LayoutManager 's, but there may be some exceptions.回答您的第二个问题 - 不,其他组件通常没有自己的LayoutManager ,但可能有一些例外。

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. LayoutManager负责在RecyclerView中测量和放置项目视图,并确定何时回收用户不再可见的项目视图的策略。 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. 通过更改LayoutManager,可以使用RecyclerView来实现标准的垂直滚动列表,统一网格,交错网格,水平滚动集合等等。 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. 如果LayoutManager指定默认的构造函数或带有签名的默认构造函数(Context,AttributeSet,int,int),则RecyclerView将在实例化时实例化并设置LayoutManager。 Most used properties can be then obtained from getProperties(Context, AttributeSet, int, int). 然后可以从getProperties(Context,AttributeSet,int,int)获得最常用的属性。 In case a LayoutManager specifies both constructors, the non-default constructor will take precedence. 如果LayoutManager同时指定了两个构造函数,则非默认构造函数将优先。

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. 它将每个项目视图放置在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. 以这种方式回收(或重用)视图可以极大地提高性能,因为现在无需在其中创建额外的视图并执行诸如findViewById()等昂贵的操作。 Due to this feature, This widget is named as RecyclerView (Means a widget that re-uses the views). 由于此功能,此窗口小部件被命名为RecyclerView(表示一个可重复使用视图的窗口小部件)。

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.布局管理器使我们能够通过确定容器内组件的大小和位置来控制可视组件在 GUI 表单中的排列方式。

GridLayout: It arranges all the components in a grid of equally sized cells, adding them from the left to right and top to bottom. GridLayout:它将所有组件排列在相同大小的单元格中,从左到右,从上到下添加它们。 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. LinearLayout:它将布局的所有组件排列在一个列/行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM