java - 导航抽屉 onNavigationItemSelected() 没有响应
问题描述
我在我的应用程序中添加了一个导航抽屉,但是,当我单击导航抽屉中的图标时,它不会执行 onNavigationItemSelected() 方法中定义的操作。相反,它会关闭导航抽屉。如何找到一种方法来确保单击图标时它确实执行 onNavigationItemSelected() 中定义的操作?
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
<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" />
解决方案
你确定里面的id和activity_navigation__drawer_drawer.xml
里面检查的一样onNavigationItemSelected()
吗?
因为还有另一个名称相似的菜单navigation__drawer
,我看到你在onCreateOptionsMenu()
.
这些线的 3d 是多余的:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setNavigationViewListner();
它通过调用重复前 2 行setNavigationViewListner()
。
您还检查了R.id.Logout
两次:在声明中switch
和稍后else if
推荐阅读
- google-colaboratory - 是否可以使用 VNC 或 SSH 等协议连接到 Colab 的主机?
- java - 如何将枚举的一个变量中的所有元素加载到数组中?
- vb.net - 如何使用 VB 获得所有重复排列的总和?
- php - 尽管成功拨打电话,Twilio Ajax 响应始终触发“失败”
- python - 使用带有一个查询的烧瓶将数据插入到多个 mySQL 表中
- c++ - C++ 当 std::allocator 超出范围时会发生什么?
- amazon-kinesis - 将快速数据写入亚马逊运动流的最简单方法?
- bash - sed中的音译
- amazon-s3 - 用于 SPA Angular 6 应用程序的 AWS S3 和 AWS ELB,而不是 AWS Elastic beanstalk
- python - 如何仅将数据框的列输出到文件