Android sideslip menu control drawerlayout usage details
Drawerlayout is a control that implements the sideslip menu effect in the support library package. It can be said that drawerlayout is a product of Google's reference after the emergence of third-party controls such as menudrawer. The drawerlayout is divided into two parts: the side menu and the main content area. The side menu can be expanded and hidden according to gestures (drawerlayout's own characteristics), and the content of the main content area can change with the click of the menu (which needs to be realized by the user).
Use steps:
Create a drawerlayout
To add a navigation drawer, you need to declare a drawerlayout object in your layout interface as the root node of the layout. Add two views inside drawerlayout at the same time:
As shown in the following example: the layout uses drawerlayout, which contains two sub nodes: a FrameLayout, which contains the main content (which will be replaced by the fragment at runtime) and a listview as the navigation drawer. The icon on the titlebar is responsible for opening and closing the drawer;
The above example contains some important layout skills:
Initialize the drawer list
The first thing to do in your activity is to initialize the list items of the navigation drawer. What to do depends on the content of your app, but the navigation drawer usually contains a listview, so a matching adapter (such as arrayadapter or simplecursoradapter) is also required. The following example shows how to initialize a navigation list with the help of a string array
Handling navigation click events
When the user selects an item in the drawer list, the system calls onitemclick() on onitemclicklistener() to setonitemclicklistener(). What you do in the onitemclick() method depends on the structure of your app implementation. In the following example, selecting each item will insert a different fragment in the layout of the main content
Opening and closing drawers
Open and close drawers using ondraweropened() and ondrawerclosed():
design sketch:
Demo download
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.