About android drawer, navigation drawer

Source: Internet
Author: User

A side pane is needed for a project that I recently worked on. I noticed this stuff, but it was only released in December, with less information in China. I can't find anything. Let's look at an example from the official website. We can see from the example that we can modify the project in our own project and keep reporting errors for the project, without understanding the principle, finally, the project cannot run. I simply took a closer look at this drawer and made my own demo. The official development page is:

Http://developer.android.com/design/patterns/navigation-drawer.html

The principle of this is that a ListView is on the left, and a FrameLayout is on the right. (the main content ):

Check this file:

Activity_main.xml:

    
     <frameLayout        android:id="@+id/content_frame"        android:layout_width="match_parent"        android:layout_height="match_parent" />    
     
 


Next, configure each item in the left sidebar:

Drawer_list_item.xml:

 
 

Main Content configuration file:

Fragment_planet.xml:

 
 

If menus are used, add the menu of actionBar: (in the menu folder)

Main. xml:

     
  
 

Paste String. xml:

 
     
  
   drawerexample
      
  
   Settings
      
  
   Hello world!
   
          
   
    Mercury
           
   
    Venus
           
   
    Earth
           
   
    Mars
           
   
    Jupiter
           
   
    Saturn
           
   
    Uranus
           
   
    Neptune
       
       
  
   Open navigation drawer
       
  
   Close navigation drawer
       
  
   Web search
       
  
   Sorry, there\'s no web browser available
  
 

Finally, the main activity is:

MainActivity. java

Package com. example. drawerexample; import java. util. locale; import android. OS. bundle; import android. annotation. suppressLint; import android. app. activity; import android. app. fragment; import android. app. fragmentManager; import android. app. searchManager; import android. content. intent; import android. content. res. configuration; import android. support. v4.app. actionBarDrawerToggle; import android. support. v4.view. gravityCompat; import android. support. v4.widget. drawerLayout; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. viewGroup; import android. widget. adapterView; import android. widget. arrayAdapter; import android. widget. imageView; import android. widget. listView; import android. widget. toast; @ SuppressLint ("NewApi") public class MainActivity extends Activity {private CharSequence mTitle; private CharSequence mDrawerTitle; private String [] mPlanetTitles; private ListView mDrawerList; private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mDrawerToggle; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mTitle = mDrawerTitle = getTitle (); mPlanetTitles = getResources (). getStringArray (R. array. planets_array); mDrawerLayout = (DrawerLayout) findViewById (R. id. drawer_layout); mDrawerList = (ListView) findViewById (R. id. left_drawer); // set a custom shadow that overlays the main content when the drawer opensmDrawerLayout. setDrawerShadow (R. drawable. drawer_shadow, GravityCompat. START); // set up the drawer's list view with items and click listenermDrawerList. setAdapter (new ArrayAdapter
 
  
(This, R. layout. drawer_list_item, mPlanetTitles); mDrawerList. setOnItemClickListener (new DrawerItemClickListener (); // enable ActionBar app icon to behave as action to toggle nav drawergetActionBar (). setDisplayHomeAsUpEnabled (true); getActionBar (). setHomeButtonEnabled (true); // ActionBarDrawerToggle ties together the proper interactions // between the sliding drawer and the action bar app icon m DrawerToggle = new ActionBarDrawerToggle (this,/* host Activity */mDrawerLayout,/* DrawerLayout object */R. drawable. ic_drawer,/* nav drawer image to replace 'up' caret */R. string. drawer_open,/* "open drawer" description for accessibility */R. string. drawer_close) {public void onDrawerClosed (View view) {getActionBar (). setTitle (mTitle); invalidateOptionsMenu (); // creates call to onPrepareOpt IonsMenu ()} public void onDrawerOpened (View drawerView) {getActionBar (). setTitle (mDrawerTitle); invalidateOptionsMenu (); // creates call to onPrepareOptionsMenu () }}; mDrawerLayout. setDrawerListener (mDrawerToggle); if (savedInstanceState = null) {selectItem (0) ;}// action bar @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return super. onCreate OptionsMenu (menu);}/* Called whenever we call invalidateOptionsMenu () */@ Overridepublic boolean onPrepareOptionsMenu (Menu menu) {// if the nav drawer is open, hide action items related to the content viewboolean draweropen = mDrawerLayout. isDrawerOpen (mDrawerList); menu. findItem (R. id. action_websearch ). setVisible (! Draweropen); return super. onPrepareOptionsMenu (menu) ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {if (mDrawerToggle. onOptionsItemSelected (item) {return true;} // Handle action buttonsswitch (item. getItemId () {case R. id. action_websearch: Intent intent = new Intent (Intent. ACTION_WEB_SEARCH); intent. putExtra (SearchManager. QUERY, getActionBar (). getTitle (); if (intent. resolveActivity (getPac KageManager ())! = Null) {startActivity (intent);} else {Toast. makeText (this, R. string. app_not_available, Toast. LENGTH_LONG ). show ();} return true; default: return super. onOptionsItemSelected (item) ;}// The click listner for ListView in the navigation drawe private class DrawerItemClickListener implements ListView. onItemClickListener {@ Overridepublic void onItemClick (AdapterView
  Parent, View view, int position, long id) {selectItem (position) ;}} private void selectItem (int position) {// update the main content by replacing fragmentsFragment fragment = new PlanetFragment (); Bundle args = new Bundle (); args. putInt (PlanetFragment. ARG_PLANET_NUMBER, position); fragment. setArguments (args); FragmentManager fragmentManager = getFragmentManager (); fragmentManager. beginTransaction (). replace (R. id. content_frame, fragment ). commit (); // update selected item and title, then close the drawermDrawerList. setItemChecked (position, true); setTitle (mPlanetTitles [position]); mDrawerLayout. closeDrawer (mDrawerList);} @ Overridepublic void setTitle (CharSequence title) {mTitle = title; getActionBar (). setTitle (mTitle);}/*** When using the ActionBarDrawerToggle, you must call it during * onPostCreate () and onConfigurationChanged ()... * // @ Override protected void onPostCreate (Bundle savedInstanceState) {super. onPostCreate (savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle. syncState () ;}@ Override public void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); // Pass any configuration change to the drawer toggls mDrawerToggle. onConfigurationChanged (newConfig);}/*** FragMent appears in content_frame */public static class PlanetFragment extends Fragment {public static final String ARG_PLANET_NUMBER = "planet_number"; public PlanetFragment () {// Empty constructor required for fragment subclasses} @ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View rootView = inflater. inflate (R. layout. fragment_planet, container, false); int I = getArguments (). getInt (ARG_PLANET_NUMBER); String planet = getResources (). getStringArray (R. array. planets_array) [I]; int imageId = getResources (). getIdentifier (planet. toLowerCase (Locale. getDefault (), "drawable", getActivity (). getPackageName (); (ImageView) rootView. findViewById (R. id. image )). setImageResource (imageId); getActivity (). setTitle (planet); return rootView ;}}}
 



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.