Android navigation drawer-Navigation drawer (drawerlayout) used

Source: Internet
Author: User
1. Create a drawer Layout

UseDrawerLayoutAs the interface root control. In drawerlayout, the first view is the main content of the current interface, and the second and third views are the drawer menu content. If the current interface only needs one drawer menu, the third view can be omitted.

In the following example, drawerlayout contains two views. The first framelayout is the display area of the main content of the current interface, and the second listview is the drawer menu content.

<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=".MainActivity"><FrameLayout        android:id="@+id/frame_layout"        android:layout_width="match_parent"        android:layout_height="match_parent"        ></FrameLayout>    <ListView            android:id="@+id/listview"            android:layout_width="240dp"            android:layout_height="match_parent"            android:background="#FFFFFF"            android:choiceMode="singleChoice"            android:layout_gravity="start"            /></android.support.v4.widget.DrawerLayout>

Note the following points in the above Code:

  • View of the main content of the interface (
    FrameLayout)
    It must be the first child view of drawerlayout,The reason is that the view order in the XML layout file is the Z-ordering order in the Android system, and the drawer must appear on the content.
  • The view width and height of the displayed interface content are the same as that of the parent view, because when the drawer menu is invisible, the interface content represents the UI of the entire interface.
  • Drawer menu (above
    ListView)
    You must use the Android: layout_gravity attribute to set the horizontal gravity value.. If you want to support right-to-left (RTL, read from right to left ),
    "start"Replace"left"(When "end" is used, the menu bar is on the right. But on the right side, I always slide back automatically and never understood it. If you want to know it, tell me ).
  • The width of the drawer menu is
    dpThe height is the same as that of the parent view. The width of the drawer menu should not exceed 320dp, so that you can see part of the content interface when the menu is opened.

  • 2. initialize the drawer menu

    Initialize the drawer menu content in your activity. The drawer menu content may not be listview according to your application requirements. In the official example, the process of loading fixed data in normal listview is not much said. It is the same as normal time and there is no dry goods.

3. Enable and disable the event in the listener menu.

You must call
Of the drawerlayout class

setDrawerListener()Function. The parameter is
Implementation of the drawerlayout. drawerlistener interface.This interface provides callback functions for events such as opening and closing menus. For example
onDrawerOpened()AndonDrawerClosed().

If your activity uses
Action bar, you can useActionBarDrawerToggleClass, which implements
Drawerlayout. drawerlistener interface, and you can rewrite related functions as needed. This class implements menu and action bar related operations.

According to
The introduction in the navigation drawer Design Guide. When the menu is displayed, you should hide the functional menu on the actionbar as needed and modify the title of the actionbar.

Key code:

package com.tao.drawerlayout;import android.os.Bundle;import android.support.v4.app.ActionBarDrawerToggle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTransaction;import android.support.v4.widget.DrawerLayout;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import java.util.ArrayList;import java.util.List;public class MainActivity extends FragmentActivity {    ListView myListview;    DrawerLayout drawerLayout;    List<String> list;    CharSequence title;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        myListview = (ListView)findViewById(R.id.listview);        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);        list = new ArrayList<String>();        for (int i = 0;i<10;i++){            list.add("item"+(i+1));        }        ListAdapter listAdapter = new ListAdapter(this,list);        myListview.setAdapter(listAdapter);        drawerLayout.setDrawerListener(new ActionBarDrawerToggle(this,drawerLayout,R.drawable.ic_launcher,R.string.open,R.string.close){            @Override            public void onDrawerOpened(View drawerView) {                super.onDrawerOpened(drawerView);                invalidateOptionsMenu();            }            @Override            public void onDrawerClosed(View drawerView) {                super.onDrawerClosed(drawerView);                getActionBar().setTitle(title);                invalidateOptionsMenu();            }        });        myListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                MyFragment myFragment = new MyFragment();                Bundle bundle = new Bundle();                bundle.putString("content",list.get(i));                myFragment.setArguments(bundle);                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();                transaction.replace(R.id.frame_layout,myFragment).commit();                title = list.get(i);                myListview.setItemChecked(i,true);                drawerLayout.closeDrawer(myListview);            }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    }

This article references the article on cloud qiofeng. Leave me a message if you need code.


Related Article

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.