Android ActionBarDrawerToggle, DrawerLayout, ActionBar, and androidactionbar

Source: Internet
Author: User

Android ActionBarDrawerToggle, DrawerLayout, ActionBar, and androidactionbar

ActionBarDrawerToggle is a switch used to open/close the DrawerLayout drawer.


ActionBarDrawerToggle provides a convenient way to work with DrawerLayout and ActionBar to implement the recommended drawer function.

Click the home button of the ActionBar to bring up the DrawerLayout drawer.


Use it in two callback functions in Activity:

OnConfigurationChanged
OnOptionsItemSelected

Call ActionBarDrawerToggle. syncState () in onPostCreate () of the Activity. It indicates that ActionBarDrawerToggle synchronizes the status of DrawerLayout and sets the drawer icon in ActionBarDrawerToggle to the icon of Home-Button of ActionBar.


About DrawerLayout not much said, please move here http://blog.csdn.net/jjwwmlp456/article/details/38682637

In this official example, the content described in this article has been implemented.


The overall effect, need to support ActionBar, compat support-v7 package has ActionBarActivity can inherit, 3.0 can also be used directly in the form of subject. Not much.


Several major function calls

ActionBarDrawerToggle mDrawerToggle; DrawerLayout mDrawerLayout; void init () {mDrawerLayout. setDrawerListener (new MyDrawerListener (); // set the drawer switch to listen to mDrawerToggle = new ActionBarDrawerToggle (activity, mDrawerLayout, R. drawable. ic_drawer, R. string. drawer_open, R. string. drawer_close);}/** after the activity is created */@ Overrideprotected void onPostCreate (Bundle savedInstanceState) {super. onPostCreate (savedInstanceState); mDrawerToggle. syncState (); // This method is automatically associated with the actionBar, and the picture of the switch is displayed on the action. If it is not set, it can also have the drawer effect, but it is the default icon}/** event processing for menu key click */@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {return mDrawerToggle. onOptionsItemSelected (item) | super. onOptionsItemSelected (item);}/** when the device Configuration changes */@ Overridepublic void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); mDrawerToggle. onConfigurationChanged (newConfig);}/** drawer listener */private class MyDrawerListener implements DrawerLayout. drawerListener {@ Overridepublic void onDrawerOpened (View drawerView) {// open drawermDrawerToggle. onDrawerOpened (drawerView); // switch status to opened} @ Overridepublic void onDrawerClosed (View drawerView) {// close drawermDrawerToggle. onDrawerClosed (drawerView); // switch status to closed} @ Overridepublic void onDrawerSlide (View drawerView, float slideOffset) {// callback mDrawerToggle for drawer Sliding. onDrawerSlide (drawerView, slideOffset) ;}@ Overridepublic void onDrawerStateChanged (int newState) {// callback mDrawerToggle when the drawer status changes. onDrawerStateChanged (newState );}}


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.