使用导航视图,但汉堡菜单不起作用 [英] Using navigationview, but hamburger menu does not work

查看:73
本文介绍了使用导航视图,但汉堡菜单不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家!我目前正在尝试使用导航视图创建导航抽屉,下面是代码

everyone! I am currently trying to use navigation view to create a navigation drawer,below is the code

AndroidMainfest

AndroidMainfest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
    <activity
        android:name=".C"
        android:label="@string/title_activity_c"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme" >

    </activity>
    <activity
        android:name=".New_Request"
        android:label="@string/title_activity_new__request" >
    </activity>
</application>

C.java

public class C extends AppCompatActivity {
DrawerLayout drawerLayout;

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

    setupDrawerLayout();

    // my_child_toolbar is defined in the layout file
    Toolbar myChildToolbar = (Toolbar) findViewById(R.id.my_childtoolbar);
    setSupportActionBar(myChildToolbar);

    // Get a support ActionBar corresponding to this toolbar
    ActionBar ab = getSupportActionBar();

    // Enable the Up button
    if(ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeAsUpIndicator(R.drawable.rsz_ic_list_white_48dp);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_c, menu);

    MenuItem searchitem = menu.findItem(R.id.action_search1);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchitem);

    // Define the listener
    MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Do something when action item collapses
            return true;  // Return true to collapse action view
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // Do something when expanded
            return true;  // Return true to expand action view
        }
    };

    // Get the MenuItem for the action item
    MenuItem actionMenuItem = menu.findItem(R.id.action_sharre);

    MenuItemCompat.setOnActionExpandListener(actionMenuItem, expandListener);

    // Assign the listener to that action item
    return super.onCreateOptionsMenu(menu);
}

private void setupDrawerLayout(){
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_c);

    NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
    view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            Snackbar.make(getWindow().getDecorView(), menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
            menuItem.setChecked(true);
            drawerLayout.closeDrawers();
            return true;
        }
    });

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    switch (id){
        case R.id.home:
            drawerLayout.openDrawer(GravityCompat.START);
            Log.d("kk","reallt");

            return true;
        case R.id.action_settings:
            return true;

    }


    //noinspection SimplifiableIfStatement


    return super.onOptionsItemSelected(item);
}

}

activity_c.xml

activity_c.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_c"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">


<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_linear"
tools:context="com.example.ji.myapplication19.C">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_childtoolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="#2196F3"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_head"
    app:menu="@menu/navigation_view_menu"/>

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

主要问题是:单击汉堡菜单时,它没有出现在导航抽屉中. 有人帮我吗?

Thee main problem is:when click the hamburger menu, it does not appear the navigation drawer. Any one help me?

推荐答案

在活动类中onCreate()方法之前

In your activity class before onCreate() method

ActionBarDrawerToggle mDrawerToggle;

setupDrawerLayout()中添加此内容:

mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, myChildToolbar, "Open", "Close") {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
drawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
});
drawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();

在此之后,您还必须对onOptionsItemSelected进行如下更改:

After this you also have to do some changes to your onOptionsItemSelected as below:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

这篇关于使用导航视图,但汉堡菜单不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆