Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

java - Android: Navigation-Drawer on all activities

I want to add navigation drawer on all actvities of my Android project. This is the code of the MainActivity:

public class MainActivity extends Activity {


        private String[] drawerListViewItems;
        private DrawerLayout drawerLayout;
        private ListView drawerListView;
        private ActionBarDrawerToggle actionBarDrawerToggle;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // get list items from strings.xml
            drawerListViewItems = getResources().getStringArray(R.array.items);


            // get ListView defined in activity_main.xml
            drawerListView = (ListView) findViewById(R.id.left_drawer);

            // Set the adapter for the list view
            drawerListView.setAdapter(new ArrayAdapter<String>(this,
                    R.layout.drawer_listview_item, drawerListViewItems));

            // App Icon 
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            //drawerLayout = (DrawerLayout) findViewById(R.drawable.ic_drawer_2);

            actionBarDrawerToggle = new ActionBarDrawerToggle(
                    this,                  /* host Activity */
                    drawerLayout,         /* DrawerLayout object */
                    R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                    R.string.drawer_open,  /* "open drawer" description */
                    R.string.drawer_close  /* "close drawer" description */
                    );

            // Set actionBarDrawerToggle as the DrawerListener
            drawerLayout.setDrawerListener(actionBarDrawerToggle);

            getActionBar().setDisplayHomeAsUpEnabled(true); 

            // just styling option add shadow the right edge of the drawer
        drawerLayout.setDrawerShadow(R.drawable.ic_drawer, GravityCompat.START);

        drawerListView.setOnItemClickListener(new DrawerItemClickListener());
    }

    @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            actionBarDrawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            actionBarDrawerToggle.onConfigurationChanged(newConfig);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

             // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
            // then it has handled the app icon touch event
            if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        private class DrawerItemClickListener implements ListView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {

                    displayView(position);

                drawerLayout.closeDrawer(drawerListView);

            }

            private void displayView(int position) 
            {
                switch (position) 
                {
                case 0:
                    secondactivity();
                    break;


                case 1:
                    Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();
                    break;

                case 2:
                    Toast.makeText(MainActivity.this, "3", Toast.LENGTH_LONG).show();

                default:
                    break;
                }

            }
        }

        public void secondactivity (){

            Intent cambioActivity;

            cambioActivity = new Intent (this, SecondActivity.class);

            startActivity(cambioActivity);
        }
}

In this code I create the navigation drawer, I want the navigation drawer on all activities, so my Second Activity's code is this:

public class SecondActivity extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondactivity);

    }

The navigation drawer is on the first activity but there isn't on other activities, why? Can someone help me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The easy way is that you should create fragments. If you are ready to for little hard thing then this is for you. It will let you have same navigation drawer in all activities.

Create drawer_n_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/drawer_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<YourDrawer
    android:id="@+id/drawer_drawer"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

</YourDrawer>

</RelativeLayout>

Your DrawerActivity.class

public class DrawerActivity extends Activity {

    public RelativeLayout fullLayout;
    public FrameLayout frameLayout;

    @Override
    public void setContentView(int layoutResID) {

        fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.drawer_n_activity, null);
        frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);

        getLayoutInflater().inflate(layoutResID, frameLayout, true);

        super.setContentView(fullLayout);

        //Your drawer content...

    }
}

Now, to include same Navigation Drawer in all your activities and mind one more thing, all your activities must extend DrawerActivity

public class MainActivity extends DrawerActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //layout for 1st activity
   }
}

public class SecondActivity extends DrawerActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity); //layout for 2nd activity
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...