Navigationview of Android Support Design Library

Source: Internet
Author: User

The second view that needs to be explained in this Android support library is Navigationview, I believe that we often use the drawer in the development, then Google also provides this function for everyone, do not need to go to GitHub to download the use of open source software. The Navigationview basically meets all the requirements of the daily development drawer and is also highly efficient. We'll look at the details of how to use it later.



A match between 1.NavigationView and Drawerlayout


Navigationview Full package name + class name as follows: Android.support.design.widget.NavigationView


and Drawerlayout Full package name + class name such as: Android.support.v4.widget.DrawerLayout


The XML layout files that match them are as follows:


<android.support.v4.widget.drawerlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:app= "Http://schemas.android.com/apk/res-auto"
android:orientation= "Vertical"
Android:id= "@+id/drawerlayout"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:fitssystemwindows= "true" >

<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

        < Android.support.design.widget.AppBarLayout
             android:layout_width= "Match_parent"
             android:layout_height= "Wrap_content"
             Android:theme= "@style/themeoverlay.appcompat.dark.actionbar"
             android:fitssystemwindows= "true";

<android.support.v7.widget.toolbar
Android:id= "@+id/toolbar"
Android:layout_width= "Match_parent"
android:layout_height= "? attr/actionbarsize"/>

</android.support.design.widget.AppBarLayout>

<linearlayout
Android:id= "@+id/linearlayout"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"/>

</LinearLayout>


<android.support.design.widget.navigationview
Android:id= "@+id/navigation"
Android:layout_width= "240DP"
android:layout_height= "Match_parent"
Android:layout_gravity= "Start"
app:headerlayout= "@layout/drawer_header"
app:menu= "@menu/drawer_menu"/>



</android.support.v4.widget.DrawerLayout>


The properties are explained as follows:


①android:fitssystemwindows= "true": This property is set in the title bar and drawer, ensuring that the system status bar is integrated with drawers and titles, because my test phone is a system of Xiaomi Is,andorid 4.1, The function is more than 5.0 of the mobile phone can show its effect, so the test chart has no effect.


②app:headerlayout= "@layout/drawer_header" is the area:



Here in the General app is put in the avatar, we just do the introduction, as long as you want to drawer_header write your arbitrary layout, then this area will display the interface you need.


③app:menu= "@menu/drawer_menu": This is the area shown:



The various menu options in the drawer.


2. Implement the menu options in the drawer


Create a new Drawer_menu.xml file under the Res/menu file and write the following code:


<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<group android:checkablebehavior= "single" >
<item
Android:id= "@+id/lyj_menu_datou"
android:icon= "@drawable/blog_tag_parent_expert"
Android:checked= "true"
Android:title= "I am the Big head"/>
<item
Android:id= "@+id/lyj_menu_xiaotou"
android:icon= "@drawable/blog_tag_parent_honorary_expert"
Android:title= "I'm a small head"/>
<item
Android:id= "@+id/lyj_menu_chilun"
android:icon= "@drawable/blog_tag_parent_system_maintenance"
Android:title= "I'm a cog"/>
</group>
<item android:title= "Other" >
<menu>
<item
android:icon= "@drawable/blog_tag_parent_cloud_computing"
Android:title= "I am a cloud disk"/>
<item
android:icon= "@drawable/blog_tag_parent_comprehensive"
Android:title= "I am the label"/>
</menu>
</item>
</menu>


The properties are explained as follows:


①android:checkablebehavior= "single": for all menus on behalf of group, select only one at a time.


②android:checked= "true": The menu item is selected by default, here is "I am the Big head"


③<item android:title= "Other";: In the form of a submenu, you can display split lines and sub-headings


3. Associated Drawer and title bar


In the activity that inherits from Appcompatactivity, the title bar setting is displayed as follows:


Toolbar toolbar= (Toolbar) Findviewbyid (R.id.toolbar);
Toolbar.settitle ("I am the Big Head");
Setsupportactionbar (toolbar);


Then use the Actionbardrawertoggle to associate the drawer with the title bar. Its construction method has five parameters, sequentially as follows:


㈠ context

㈡drawerlayout

㈢toolbar

㈣ Open Identity

㈤ Close Identification


Four and five do not show, do only the identification with. The associated drawer and title bar code are as follows:


Drawerlayout drawerlayout= (drawerlayout) Findviewbyid (r.id.drawerlayout);
Actionbardrawertoggle mdrawertoggle = new Actionbardrawertoggle (this, Drawerlayout, toolbar,
R.string.drawer_open, R.string.drawer_close);
Mdrawertoggle.syncstate ();//Initialize State
Drawerlayout.setdrawerlistener (Mdrawertoggle);


Use the Setdrawerlistener mode to set the listening switch on and off. This enables the drawer to open when the left button of the menu bar is pressed.


4. Set the listener event for the Navigationview menu option


This view provides us with a menu listener callback method:


Setnavigationitemselectedlistener the interfaces it needs to implement are as follows:


Public interface Onnavigationitemselectedlistener {
Boolean onnavigationitemselected (MenuItem var1);
}


The interface method is one, and the parameter is the menu. Below I will implement its monitoring:


Navigationview.setnavigationitemselectedlistener (New Navigationview.onnavigationitemselectedlistener () {
@Override
public boolean onnavigationitemselected (MenuItem MenuItem) {
Switch (Menuitem.getitemid ()) {//Get menu Itemid
Case R.id.lyj_menu_datou:
Getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.linearlayout,new drawerfragment ("I am the Big Head"). commit ();
Toolbar.settitle ("I am the Big Head");
Break
Case R.id.lyj_menu_xiaotou:
Getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.linearlayout,new drawerfragment ("I am a small head"). commit ();
Toolbar.settitle ("I am a small head");
Break
Case R.id.lyj_menu_chilun:
Getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.linearlayout,new drawerfragment ("I am a cog")). commit ();
Toolbar.settitle ("I am a cog");
Break
Default
Break
}
Menuitem.setchecked (TRUE);//Settings menu selected
Drawerlayout.closedrawers ()///The drawer will be closed after the menu is selected
return false;
}
});


Set the fragment interface based on the menu ID. Finally, this is the picture of the beginning of the blog.


5. Import the Support library


Yesterday the explanation forgot, need to configure the following information, can use the control:


dependencies {
Compile Filetree (dir: ' Libs ', include: [' *.jar '])
Compile ' com.android.support:appcompat-v7:23.0.1 '
compile ' com.android.support:design:23.0.1 '
compile ' com.android.support:recyclerview-v7:23.0.1 '
compile ' com.android.support:cardview-v7:23.0.1 '
}


Red is labeled as the imported code.

??

Android Support Design Library Navigationview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.