Android Study Notes: Implementation Sidebar of DrawerLayout,

Source: Internet
Author: User

Android Study Notes: Implementation Sidebar of DrawerLayout,

Layout file required by DrawerLayout

First, use android. support. v4.widget. DrawerLayout as the tag.

In addition, you need to set the android: layout_gravity = "start" start of the sidebar layout to the left layout and end to the right layout.

 

 

Code in the activity:

Private DrawerLayout mDrawerLayout;
Private ActionBarDrawerToggle mActionBarDrawerToggle;

// The android. support. v7.app. ActionBarDrawerToggle package should be used in ActionBarDrawerToggle. If not, please inport V7 Database support/v7/appcompat

// Obtain the sidebar id.

MDrawerLayout = (DrawerLayout) findViewById (R. id. f1 );

MActionBarDrawerToggle = new ActionBarDrawerToggle (this, mDrawerLayout,
R. string. open, R. string. close );

// Listen to mDrawerLayout.

MDrawerLayout. setDrawerListener (new DrawerListener (){

@ Override
Public void onDrawerStateChanged (int arg0 ){
MActionBarDrawerToggle. onDrawerStateChanged (arg0 );
}

@ Override
Public void onDrawerSlide (View arg0, float arg1 ){
MActionBarDrawerToggle. onDrawerSlide (arg0, arg1 );
}

@ Override
Public void onDrawerOpened (View arg0 ){
MActionBarDrawerToggle. onDrawerOpened (arg0 );
}

@ Override
Public void onDrawerClosed (View arg0 ){
MActionBarDrawerToggle. onDrawerClosed (arg0 );
}
});

Note: Another simple listening method

MDrawerLayout. setDrawerListener (mActionBarDrawerToggle );

@ Override
Protected void onPostCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onPostCreate (savedInstanceState );
MActionBarDrawerToggle. syncState ();
}

Public boolean onOptionsItemSelected (MenuItem item ){

Return mActionBarDrawerToggle. onOptionsItemSelected (item)
| Super. onOptionsItemSelected (item );
}

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.