As a common interactive navigation control, the side menu is usually used when there is no official android control at first. It has never been used to analyze the implementation mechanism of the side menu, this article will analyze android. support. v4.widget. use and Implementation of DrawerLayout.
Official introduction
DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from the edge of the window.
Drawer positioning and layout is controlled usingandroid:layout_gravity
Attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction .)
To use a DrawerLayout, position your primary content view as the first child with a width and heightmatch_parent
. Add drawers as child views after the main content view and setlayout_gravity
Appropriately. Drawers commonly usematch_parent
For height with a fixed width.
DrawerLayout.DrawerListener
Can be used to monitor the state and motion of drawer views. Avoid scaling Ming expensive operations such as layout during animation as it can cause stuttering; try to perform expensive operations duringSTATE_IDLE
State.DrawerLayout.SimpleDrawerListener
Offers default/no-op implementations of each callback method.
As per the Android Design guide, any drawers positioned to the left/start shoshould always contain content for navigating the application, whereas any drawers positioned to the right/end shoshould always contain actions to take on the current content. this preserves the same navigation left, actions right structure present in the Action Bar and elsewhere
DrawerLayout refers to the layout of a drawer. As a top-level container in a window, it allows you to pull the outer edge of the View window to the screen through a drawer-type push-pull operation, as shown in the right figure:
The placement and layout of Drawer menus areandroid:layout_gravity
Attribute. Optional values include left, right, start, and end. To use xml for layout, you need to use DrawerLayout as the parent container, the group interface layout as its first child node, and the drawer layout followed by it as the second child node, in this way, the content display area and the drawer menu area are independent. You only need to set the content in the other two areas. Android provides some useful listeners. You can reload the related callback methods to write logical services during the menu interaction process. The following is a demo layout:
<! -- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<Android. support. v4.widget. DrawerLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/drawer_layout"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Tools: context = "com. aven. weather. app. MainActivity">
<! -- As the main content view, the view below consumes the entire
Space available using match_parent in both dimensions. -->
<FrameLayout
Android: id = "@ + id/container"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"/>
<! -- Android: layout_gravity = "start" tells DrawerLayout to treat
This as a sliding drawer on the left side for left-to-right
Ages and on the right side for right-to-left ages.
If you're not building against API 17 or higher, use
Android: layout_gravity = "left" instead. -->
<! -- The drawer is given a fixed width in dp and extends the full height
The container. -->
<FragmentAndroid: id = "@ + id/navigation_drawer"
Android: layout_width = "@ dimen/navigation_drawer_width"
Android: layout_height = "match_parent"
Android: layout_gravity = "start"
Android: name = "com. aven. weather. app. NavigationDrawerFragment"/>
</Android. support. v4.widget. DrawerLayout>
Therefore, the use of DrawerLayout is very simple. Like many container layout types, it also inherits from ViewGroup, but the first subnode is used as the content area by default in internal implementation, the second is used as the drawer menu, so the layout must be kept in mind after writing. Fortunately, IDE is very intelligent now. When you create Drawerlayout through guidance, the Activity and xml layout will be automatically generated, for example, it is very convenient to use AndroidStudio.
Source:
Git clone https://github.com/avenwu/DrawerDemo.git