【问题标题】:Navigation Menu item click not working导航菜单项单击不起作用
【发布时间】:2017-11-20 14:57:30
【问题描述】:

我为我的应用程序创建了一个侧边菜单(导航菜单),并重写了 OnOptionsItemSelected(MenuItem item) 方法,以便在单击每个菜单项时添加一些自定义行为。但是,在编写了一些代码之后,当单击第一个菜单项时(nav_home

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // When a menu item is clicked, then depending on it's id start a new Activity
    switch (item.getItemId()) {
        case R.id.nav_home:
            Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}

这是我的整个 MainActivity 类:

package com.dcv.spdesigns.dokkancards;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.dcv.spdesigns.dokkancards.model.ImageAdapter;
import com.dcv.spdesigns.dokkancards.ui.CardViewActivity;

public class MainActivity extends AppCompatActivity {
GridView gridView;

// NavMenu member vars
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle; // Button for toggling the side menu

private Toolbar mToolbar;

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

    // Get Screen Size Details
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;
    final int width = displayMetrics.widthPixels;

    gridView = (GridView) findViewById(R.id.gridViewLayout);
    gridView.setAdapter(new ImageAdapter(this)); // used to set the contents of the GridView-in this case images-

    // When an item from the GridView gets clicked
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Create a new Intent...
            Toast.makeText(MainActivity.this, "Position: " + position, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this,CardViewActivity.class);
            intent.putExtra("Card Index",position);
            intent.putExtra("SCREEN_WIDTH",width);
            startActivity(intent);
        }
    });

    mToolbar = (Toolbar) findViewById(R.id.navActionBar);
    setSupportActionBar(mToolbar); // check quick doq
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    mToolbar.setTitle("");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.drawer_open,R.string.drawer_closed); // Instantiating our button

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();


}

// When an item from the Action Bar gets tapped, then...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // When a menu item is clicked, then depending on it's id start a new Activity
    switch (item.getItemId()) {
        case R.id.nav_home:
            Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}
}

主xml文件:

<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/colorPrimary"
    tools:context="com.dcv.spdesigns.dokkancards.MainActivity"
    android:id="@+id/drawerLayout">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Include the custom Action Bar we created -->

    <include layout="@layout/navigation_action_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="4"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:id="@+id/gridViewLayout"/>

    <!-- Add text & images -->
</LinearLayout>

<!-- The actual side menu Nav View -->
<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_header"
    app:menu="@menu/navigation_menu">

</android.support.design.widget.NavigationView>

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

【问题讨论】:

  • 评论条件:if(mToggle.onOptionsItemSelected(item)) {
  • 我试过了,但没用
  • a Toast will be displayed,nothing happens,没有别的了,你在期待什么
  • 我不期待更多...但就像我说的那样,这不起作用。不显示 Toasts
  • 发布完整代码

标签: java android switch-statement onclicklistener


【解决方案1】:

如果没有完整的代码,很难说,但这个问题的一个常见原因是导航不在所有其他元素之上。为了确保这一点,请将您的布局中的标题/导航引用放置到完整的底部。项目在布局 xml 中的位置越低,它的 z-index 就越高。

布局的最后几行应该是这样的:

    ...
    <include layout="@layout/header" />
    <include layout="@layout/navigation" />
</android.support.v4.widget.DrawerLayout>

另外,我没有看到你关闭 DrawerLayout 标签。

我在导航中检测点击的方式通常也有所不同。我实现了类似这样的标题和导航::

 protected void setUpHeader(int headerRequestingActivity) {
    setContentView(headerRequestingActivity);
    headerPresenter = new HeaderPresenterImpl(this);
    drawerHeaderView = findViewById(R.id.navigationheader);
    toolbar = (Toolbar) findViewById(R.id.tbHeader);
    setSupportActionBar(toolbar);

    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer);
    final NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
    navigationView.setNavigationItemSelectedListener(this);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
        @Override
        public void onDrawerStateChanged(int newState) {
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                //do something on opening the navigation
            } else {
                //optional actions when navigation menu closes
            }
        }
    };
    drawer.setDrawerListener(toggle);
    toggle.syncState();
}

并通过以下方法监听点击:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.nav_changelanguage:
            //change language logic
            break;
        case R.id.nav_logout:
            //logout user
            break;
    }
    return true;
}

希望这会有所帮助。

【讨论】:

  • 我在上面发布了我的 main.xml 文件,因为我认为我的设置有点奇怪。我只在 NavigationView 小部件中包含了我的 nav_header,然后使用 app:menu 分配了菜单……但这怎么会导致问题呢?我在许多帖子中看到您刚才所说的是实际问题,但我在 navView 之上没有看到任何其他视图。也许你能看到我看不到的东西:(
  • 我添加了一个完整示例,说明如何将我的标题集成到不同的活动中。另外,我没有看到您在布局 xml 中关闭了您的抽屉布局。
  • 是的,我忘了 TAB 最后一行 :P 。我将尝试将你的一些代码实现到我的代码中,看看我是否能在这里找到一些东西。感谢您尝试帮助我
  • 遗憾的是,这种 listenerforclicks 方法对我不起作用。令我困惑的是,我似乎无法发现这个问题的根源。可能是 ToggleButton 吗?也许它以某种方式与 navView 重叠?
  • 想通了....没有意识到我应该使用 onNavigationItemSelected 而不是 onOptionsItemSelected :D
猜你喜欢
  • 1970-01-01
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
相关资源
最近更新 更多