简体   繁体   English

导航抽屉 onNavigationItemSelected() 没有响应

[英]Navigation Drawer onNavigationItemSelected() not responding

I added a navigation drawer to my app, however, when I click on an icon in the navigation drawer it does not execute the action defined in the onNavigationItemSelected() method.我向我的应用程序添加了一个导航抽屉,但是,当我单击导航抽屉中的图标时,它不会执行 onNavigationItemSelected() 方法中定义的操作。 Instead it closes the navigation drawer.相反,它会关闭导航抽屉。 How can I find a way to make sure that when an icon is clicked it does execute the action defined in the onNavigationItemSelected()?我怎样才能找到一种方法来确保在单击图标时它确实执行 onNavigationItemSelected() 中定义的操作?

录屏GIF

Navigation_Drawer.java Navigation_Drawer.java

public class Navigation_Drawer extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation__drawer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    setNavigationViewListner();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

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

private void setNavigationViewListner(){
    NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@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();

    //noinspection SimplifiableIfStatement

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    switch (item.getItemId()){
        case R.id.Logout: {
            User user = new User();
            Toast.makeText(getApplicationContext(), "You logged out", Toast.LENGTH_SHORT).show();
            break;
        }
    }
    if (id == R.id.nav_camera) {
        Toast.makeText(getApplicationContext(), "button has been Clicked", Toast.LENGTH_SHORT).show();
    } else if (id == R.id.nav_gallery) {
        Toast.makeText(getApplicationContext(), "button has been Clicked", Toast.LENGTH_SHORT).show();

    } else if (id == R.id.nav_slideshow) {
        Toast.makeText(getApplicationContext(), "button has been Clicked", Toast.LENGTH_SHORT).show();

    } else if (id == R.id.nav_manage) {
        Toast.makeText(getApplicationContext(), "button has been Clicked", Toast.LENGTH_SHORT).show();

    } else if (id == R.id.Logout) {
        User user = new User();
        Toast.makeText(getApplicationContext(), "You logged out", Toast.LENGTH_SHORT).show(); }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}[![enter image description here][1]][1]}

Activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:context="MainActivity"
tools:openDrawer="">

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

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:popupTheme = "@style/ThemeOverlay.AppCompat.Light"/>

<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_navigation__drawer"
    app:menu="@menu/activity_navigation__drawer_drawer" />

It looks like you need to take out this line of code...看来需要把这行代码去掉了...

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

What is happening is that the code is running through your switch statement and if statements and doing the appropriate action, but at the end of your code your are telling it to close the drawer.发生的事情是代码正在运行您的 switch 语句和 if 语句并执行适当的操作,但是在您的代码结束时,您告诉它关闭抽屉。 The return true value indicates that the user is finished and has performed all their actions. return true 值表示用户已完成并执行了所有操作。

Are you sure that the ids in activity_navigation__drawer_drawer.xml are the same that are checked inside onNavigationItemSelected() ?您确定activity_navigation__drawer_drawer.xml中的 id 与在onNavigationItemSelected()中检查的相同吗?
Because there is another menu with similar name navigation__drawer that I see you inflate in onCreateOptionsMenu() .因为还有另一个类似名称navigation__drawer菜单,我看到你在onCreateOptionsMenu()膨胀。
The 3d of these lines is redundant:这些线的 3d 是多余的:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setNavigationViewListner();

it repeats the previous 2 lines by calling setNavigationViewListner() .它通过调用setNavigationViewListner()重复前两行。
Also you check for R.id.Logout twice: in switch and later in an else if statement您还检查R.id.Logout两次:在switch ,然后在else if语句中

If you have check the button from Toast message so如果您已检查 Toast 消息中的按钮,则

Replace getApplicationContext() to this in Toast在 Toast 中将getApplicationContext()替换为此

Toast.makeText(this, "button has been Clicked", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "button has been Clicked", Toast.LENGTH_SHORT).show();

I think this it would be help you.我想这会对你有所帮助。

Had the same issue, Here is my solution.有同样的问题,这是我的解决方案。

I was programmatically adding HeaderView to NavigationView Hence I already had NavigationView我以编程方式将 HeaderView 添加到 NavigationView 因此我已经有了 NavigationView

I called navigationView.bringToFront();我打电话给 navigationView.bringToFront();

Here is code snippet for context这是上下文的代码片段

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.bringToFront();

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

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