简体   繁体   English

Android:所有活动的导航抽屉

[英]Android: Navigation-Drawer on all activities

I want to add navigation drawer on all actvities of my Android project. 我想在Android项目的所有活动中添加导航抽屉。 This is the code of the MainActivity: 这是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? 有人能帮我吗?

Okay Guys will share my way I do it (in case you are developing project after somebody and there is already tons of activities). 好吧,伙计们会分享我这样做的方式(如果你正在开发一个人之后的项目并且已经有很多活动)。

Finally I inflate navigation drawer with one line of code (in onCreate ): 最后,我用一行代码(在onCreate )为导航抽屉充气:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this; //context
        //setContentView(R.layout.activity_favorites); // Was so
        Utils.inflatedNavigationDrawerView(mContext, R.layout.activity_favorites); // Now is
        //...
    }

next what is Utils ? 那么什么是Utils :

public class Utils{

public static void inflatedNavigationDrawerView(Context context, int layoutId){
        // Here get view of clear DrawerLayout
        final View drawerLayout = View.inflate(context,R.layout.navigation_drawer_inflator, null);
        // Next inflate to it passed layout for activity
        final View mainLayout = View.inflate(context,layoutId, (ViewGroup) drawerLayout);
        // And finally inflate to it fragment for NavigationDraver
        final View fragmentLayout = View.inflate(context,R.layout.navigation_drawer_inflator_fragment, (ViewGroup) drawerLayout);

        // Next we should try to get our drawer (should check with casting to each activity you want to insert to)
        NavigationDrawerFragment drawerFragment = null;

        // this block should be repeated for each activity you want to use in.    
        if (context instanceof FavoritesActivity){
            // Set our pack of inflates to contentview
            ((FavoritesActivity) context).setContentView(mainLayout);
            // And now we get NavigationDrawerFragment
            drawerFragment = (NavigationDrawerFragment)
                    ((FavoritesActivity) context).getSupportFragmentManager().findFragmentById(R.id.navigation_drawer_fragment);
        }

        if(drawerFragment != null){ // if null then we missed some castings for activity used in.
            // Finally setup our drawer
            drawerFragment.setUpEasy(R.id.navigation_drawer_fragment, (DrawerLayout)drawerLayout.findViewById(R.id.drawer_layout), (Toolbar) mainLayout.findViewById(R.id.app_bar));
        }
    }
}

If you would like to avoid creation of hamburger in Toolbar then you shoud in setUpEasy place: mDrawerToggle.setDrawerIndicatorEnabled(false); 如果您想避免在工具栏中创建汉堡包,那么您应该在setUpEasy中放置: mDrawerToggle.setDrawerIndicatorEnabled(false);

Here is my sample of setup: 这是我的设置示例:

public void setUpEasy(int fragmentId, DrawerLayout drawerLayout, Toolbar toolBar) {
        mContainerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;


        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolBar, R.string.open, R.string.close){

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if(!mUserLearnedDrawer){
                    mUserLearnedDrawer = true;
                    saveToPreferences(getActivity(), Constants.PREFERENCES_LEARNDRAWER_KEY, mUserLearnedDrawer+"");
                }
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }


        };
        mDrawerToggle.setDrawerIndicatorEnabled(false); // If false then no hamburger menu.
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });
    }

and views used: 和使用的观点:

// navigation_drawer_inflator.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dragAndDrop="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.widget.DrawerLayout>

// navigation_drawer_inflator_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_drawer_fragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginTop="56dp"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

Finally if you will implement this - You will be able to insert navigation drawer to any activity on run. 最后,如果您将实现此功能 - 您将能够在运行时将导航抽屉插入任何活动。 Cheers :) 干杯:)

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 创建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 你的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 现在,要在所有活动中包含相同的导航抽屉并再考虑一件事,您的所有活动都必须扩展 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
   }
}

Here's how I did it. 这就是我做到的。 Create the helper class. 创建帮助程序类。 I also added some optional code for the ability to finish() the class. 我还添加了一些可选代码来完成()类的完成。

public class NavDrawerHelper extends ContextWrapper{

public NavDrawerHelper(Context context){
    super(context);
}

    public void initNav(final DrawerLayout drawerLayout, NavigationView navigationView, Toolbar toolbar, final boolean isFinish){

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            int id = menuItem.getItemId();
            switch (id){
                case R.id.nav_home:
                    startActivity(new Intent(getBaseContext(), MainActivity.class));
                    if (isFinish) ((Activity)getBaseContext()).finish();
                    drawerLayout.closeDrawers();
                    break;
                case R.id.nav_settings:
                    startActivity(new Intent(getBaseContext(), SettingsActivity.class));
                    if (isFinish) ((Activity)getBaseContext()).finish();
                    drawerLayout.closeDrawers();
                    break;
            }
            return true;
        }
    });

    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(((Activity)getBaseContext()),drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){
        @Override
        public void onDrawerClosed(View v){
            super.onDrawerClosed(v);
        }
        @Override
        public void onDrawerOpened(View v) {
            super.onDrawerOpened(v);
        }
    };
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
}

} }

then add this code to all activities. 然后将此代码添加到所有活动中。 This replaces your existing initNavigationDrawer() method. 这将替换您现有的initNavigationDrawer()方法。

    public void initNavigationDrawer() {
    //views
    NavigationView navigationView = findViewById(R.id.navigation_view);
    DrawerLayout drawerLayout = findViewById(R.id.drawer);

    NavDrawerHelper navDrawerHelper = new NavDrawerHelper(this);
    navDrawerHelper.initNav(drawerLayout, navigationView, toolbar, false);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM