简体   繁体   English

onNavigationItemSelected 未注册用户选择

[英]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:为此,这就是我所做的:

  • I have implemented NavigationView.OnNavigationItemSelectedListener in my main activity我已经在我的主要活动中实现了NavigationView.OnNavigationItemSelectedListener
  • I have added the listener to the navigation view: navigationView.setNavigationItemSelectedListener(this);我已将侦听器添加到导航视图: navigationView.setNavigationItemSelectedListener(this);
  • I have overridden the onNavigationItemSelected method in my main activity我在我的主要活动中覆盖了onNavigationItemSelected方法

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:最简单的方法是:

  1. In your activity define a function with public access modifier so you can access it outside of your class.在您的活动中定义一个带有公共访问修饰符的函数,以便您可以在类之外访问它。
void someMethodOnMainActivity() {}
  1. In your fragment:在你的片段中:
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM