-1

我有一个简单的android.support.v7.widget.Toolbar,我要做的就是通过按左上角的“汉堡包”图标打开NavigationDrawer。“汉堡包”按钮是可见的,当我开始从左侧拉动时,我看到了按钮上的动画,但按下按钮并没有像我预期的那样打开/关闭 NavigationDrawer。代码如下: -

MainActivity.java

    public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    //    private String drawerTitle;
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;

    Button sign_in;

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

        setToolbar(); 

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        sign_in = (Button) findViewById(R.id.sign_in);
        mNavigationView = (NavigationView) findViewById(R.id.nav_view);

        if (mNavigationView != null) {
            setupDrawerContent(mNavigationView);
        }

        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.main_content, new HomeFragment()).commit();

//        drawerTitle = getResources().getString(R.string.home_item);
//        if (savedInstanceState == null) {
//            selectItem(drawerTitle);
//        }

        sign_in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent signin = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(signin);
            }
        });

    }

    private void setToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final ActionBar ab = getSupportActionBar();
        if (ab != null) {
            ab.setHomeAsUpIndicator(R.drawable.ic_menu);
            ab.setDisplayHomeAsUpEnabled(true);
        }

    }



    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {

                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        menuItem.setChecked(true);
                        if (menuItem.getItemId() == R.id.nav_home) {
                            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                            fragmentTransaction.replace(R.id.main_content, new HomeFragment()).commit();
                            mDrawerLayout.closeDrawers();

                        }

                        if (menuItem.getItemId() == R.id.nav_training) {
                            FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                            xfragmentTransaction.replace(R.id.main_content, new TrainingFragment()).commit();
                            mDrawerLayout.closeDrawers();
                        }

                        if (menuItem.getItemId() == R.id.nav_product) {
                            FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                            xfragmentTransaction.replace(R.id.main_content, new ProductFragment()).commit();
                            mDrawerLayout.closeDrawers();
                        }

//                        if (menuItem.getItemId() == R.id.nav_product_details) {
//                            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
//                            fragmentTransaction.replace(R.id.main_content, new ProductDetailsFragment()).commit();
//                            mDrawerLayout.closeDrawers();
//
//                        }

                        if (menuItem.getItemId() == R.id.nav_my_cart) {
                            FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                            xfragmentTransaction.replace(R.id.main_content, new MyCartFragment()).commit();
                            mDrawerLayout.closeDrawers();
                        }

                        if (menuItem.getItemId() == R.id.nav_my_account) {
                            FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                            xfragmentTransaction.replace(R.id.main_content, new MyAccountFragment()).commit();
                            mDrawerLayout.closeDrawers();
                        }

                        if (menuItem.getItemId() == R.id.nav_log_out) {
//                            FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
//                            xfragmentTransaction.replace(R.id.main_content, new HomeFragment()).commit();
//                            mDrawerLayout.closeDrawers();
                            finish();
                        }

//                        if (menuItem.getItemId() == R.id.nav_log_in) {
//                            FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
//                            xfragmentTransaction.replace(R.id.main_content, new LoginFragment()).commit();
//                            mDrawerLayout.closeDrawers();
//                        }
                        return false;
                    }
                }
        );
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_about_us:
                Intent intent = new Intent(MainActivity.this, AboutUs.class);
                startActivity(intent);
                return true;

            case R.id.action_privacy_policy:
                Intent intent_two = new Intent(MainActivity.this, PrivacyPolicy.class);
                startActivity(intent_two);
                return true;

            case R.id.action_contact_us:
                Intent intent_three = new Intent(MainActivity.this, ContactUs.class);
                startActivity(intent_three);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void selectItem(String title) {
        Bundle args = new Bundle();
        args.putString(PlaceholderFragment.ARG_SECTION_TITLE, title);

        Fragment fragment = PlaceholderFragment.newInstance(title);
        fragment.setArguments(args);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.main_content, fragment).commit();

        mDrawerLayout.closeDrawers(); 

        setTitle(title); 

    }


}

xml文件如下:-

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <!-- Contenido Principal -->
    <include layout="@layout/main_content" />

    <!-- Menú Deslizante -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu" />

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

1 回答 1

1

将此添加到 onCreate 方法的末尾

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){

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

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
于 2016-04-27T21:40:02.263 回答