Creating drawerlayout navigation for Android components

Overview this blog is a simple translation of the training course on developer.android.com /. If you find it difficult to understand the translation, please click the link below to view the original text! Training on drawerlayout: http://developer.android.com/training/implementing-navigation/nav-drawer.html About the drawerlayout API: http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

Create drawer layout

To create a drawer layout, you must use drawerlayout as the root node of the XML file. Remember that drawerlayout refers to android.support.v4.drawerlayout, and then add a content view area and one or two drawer view areas inside the layout. Here, it is understood that the drawer view is the menu view described above. For example, as shown in the following section of layout, a FrameLayout is added to the layout as the content area (usually used to render the fragment), and a listview is defined below to render the drawer menu view:

This layout file demonstrates some important layout features

Initialize drawer list

In your activity, the first thing is to initialize the elements in the navigation drawer list. What you do depends on the content of your application. However, a navigation drawer usually includes a listview, so the list should be filled with an adapter (such as arrayadapter or simplecursoradapter). For example, here is an example of how to initialize a navigation list with string array

This code calls setonitemclicklistener() to receive the click event of the navigation drawer list. The next section will show how to implement this interface to change the content view when the user selects an item

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

Listen for open and close events

Listen for drawer opening and closing events, call your drawerlayout setdrawerlistener() and pass it to the implementation of drawerlayout. Drawerlistener. This interface provides callback drawer events, such as ondraweropened() and ondrawerclosed(). However, compared with the implementation of drawerlayout.drawerlistener, if your activity includes a toolbar, you can instead inherit the actionbardrawertoggle class. Actionbardrawertoggle implements drawerlayout.drawerlistener. So you can still override these callbacks, but it also helps to correctly interact between the toolbar icon and the navigation drawer (discussed further in the next section). Just like in the navigation drawer design guide, when the drawer is visible, you should modify the contents of the toolbar, such as changing the title and deleting the action item. The following code uses an instance of actionbardrawertoggle class to show how to override the callback method of drawerlayout.drawerlistener:

The next section describes the actionbardrawertoggle constructor parameter and other steps required to set it to handle toolbar icons

Open close app Icon

Users can open and close the navigation drawer by sliding their fingers from the left edge of the screen, but if you use the toolbar, you should also allow users to open and close it by touching the application icon. The application icon can also display a special icon about the state of the navigation drawer. You can achieve all these behaviors by using actionbardrawertoggle, As shown in the previous section. Let actionbardrawertoggle work, create an instance of it and use its construction method, which requires the following parameters:

Then, whether you have created a subclass of actionbardrawertoggle as the listener of your drawer or not, you need to call your actionbardrawertoggle at several places in the activity life cycle:

Official Demo: http://xiazai.jb51.net/201701/yuanma/NavigationDrawer (jb51.net).rar

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.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>