我在应用程序中添加了一个导航抽屉,但是,当我单击导航抽屉中的图标时,它不执行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" />
答案 0 :(得分:0)
您似乎需要删除这一行代码...
答案 1 :(得分:0)
您确定activity_navigation__drawer_drawer.xml
中的ID与onNavigationItemSelected()
内部检查的ID相同吗?
因为还有另一个名称类似navigation__drawer
的菜单,所以我看到您在onCreateOptionsMenu()
中膨胀了。
这些行的3d多余:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setNavigationViewListner();
它通过调用setNavigationViewListner()
来重复前两行。
另外,您还要两次检查R.id.Logout
:在switch
中,然后在else if
语句中
答案 2 :(得分:0)
如果您已经从Toast消息中选中了按钮
在Toast中将 getApplicationContext()替换为 this
Toast.makeText(此,“按钮已被单击”,Toast.LENGTH_SHORT).show();
我认为这会对您有所帮助。
答案 3 :(得分:0)
有同样的问题,这是我的解决方法。
我正在以编程方式将HeaderView添加到NavigationView,因此我已经有了NavigationView
我叫navigationView.bringToFront();
这是上下文的代码段
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.bringToFront();