[英]Navigation Menu item click not working
I have created a side-menu(Navigation Menu) for my app and have overridden the OnOptionsItemSelected(MenuItem item) method in order to add some custom behavior when each menu item is clicked. 我为我的应用程序创建了一个侧面菜单(导航菜单),并重写了OnOptionsItemSelected(MenuItem item)方法,以便在单击每个菜单项时添加一些自定义行为。 However, after writing some code so that when the first menu item is clicked(nav_home <- id) a Toast will be displayed,nothing happens when i click on it and i cannot figure out the problem here...
但是,在编写了一些代码后,当单击第一个菜单项时(nav_home <-id)将显示Toast,当我单击它时什么也没有发生,并且我无法在此处找出问题...
@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;
}
Here's my whole MainActivity class : 这是我的整个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;
}
}
Main xml file: 主要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>
Hard to tell without the complete code, but a common source for this problem is that the navigation is not on top of all other elements. 没有完整的代码很难说,但是这个问题的一个普遍根源是导航并不位于所有其他元素之上。 In order to ensure this, place the the header/navigation references in your layout to the complete bottom.
为了确保这一点,请将标题/导航参考放置在布局中的整个底部。 The lower an item is in your layout xml, the higher its z-index.
布局xml中的项目越低,其z-index越高。
The final lines of your layout should look something like this: 布局的最后几行应如下所示:
...
<include layout="@layout/header" />
<include layout="@layout/navigation" />
</android.support.v4.widget.DrawerLayout>
Additionally, I don't see you closing your DrawerLayout tag. 此外,我看不到您关闭DrawerLayout标签。
The way I detect clicks in the navigation is generally somewhat different as well. 我在导航中检测点击的方式通常也有所不同。 I implement the header and navigation something like this::
我实现了标题和导航,如下所示:
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();
}
And listening for clicks with following method: 并通过以下方法监听点击:
@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;
}
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.