[英]onNavigationItemSelected not registering user selection
I have created a new Android project starting from the NavigationDrawer activity template, so the following code is as close to default behavior as possible.我从 NavigationDrawer 活动模板开始创建了一个新的 Android 项目,因此以下代码尽可能接近默认行为。
Currently my navigation drawer includes the three default entries: Home, Gallery and Slideshow.目前我的导航抽屉包括三个默认条目:主页、画廊和幻灯片。 When I click on any of them, the corresponding fragment is displayed.
当我单击其中任何一个时,会显示相应的片段。 What I would like to do is call an external activity when these items are pressed, get some results, and then display the results in the fragment (instead of calling the fragment directly).
我想做的是在按下这些项目时调用外部活动,得到一些结果,然后将结果显示在片段中(而不是直接调用片段)。
To do this, this is what I have done:为此,这就是我所做的:
NavigationView.OnNavigationItemSelectedListener
in my main activityNavigationView.OnNavigationItemSelectedListener
navigationView.setNavigationItemSelectedListener(this);
navigationView.setNavigationItemSelectedListener(this);
onNavigationItemSelected
method in my main activityonNavigationItemSelected
方法Here is the complete code for MainActivity.java
(I haven't touched any other project files so far):这是
MainActivity.java
的完整代码(到目前为止我还没有触及任何其他项目文件):
package com.example.test;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.NavigationView;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private AppBarConfiguration mAppBarConfiguration;
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = 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();
}
});
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.nav_gallery: {
Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_SHORT).show();
}
}
//close navigation drawer
drawer.closeDrawers();
return true;
}
}
When I click on the Gallery menu item, the Toast message "clicked" should appear.当我单击 Gallery 菜单项时,应该会出现 Toast 消息“clicked”。 Instead, the corresponding fragment is called.
而是调用相应的片段。 How can I make this work so that selecting a menu item calls an external activity, gets the results from it, and displays them in the corresponding fragment?
我怎样才能使这个工作,以便选择一个菜单项调用一个外部活动,从中获取结果,并将它们显示在相应的片段中?
There are a couple of ways to achieve this.有几种方法可以实现这一点。 The easisest one is to:
最简单的方法是:
void someMethodOnMainActivity() {}
Activity activity = requireActivity();
if (activity instanceof MainActivity) {
((MainActivity) activity).someMethodOnMainActivity();
}
You can also invoke onNavigationItemSelected()
in your MainActivity.您还可以在 MainActivity 中调用
onNavigationItemSelected()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.