Java 如何使菜单项可点击?

Java 如何使菜单项可点击?,java,android,android-fragments,navigation,Java,Android,Android Fragments,Navigation,我正在尝试在应用程序中设置导航活动。问题是我无法在菜单片段之间切换。看起来菜单项不可单击 我试着用之前设置菜单(操作栏)的方法设置导航活动,但它不起作用。此外,对于单击侦听器,项目不会做出反应。 我现在的问题是如何使onNavigationItemSelected()方法工作 抱歉,代码太多了 public class NavActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedL

我正在尝试在应用程序中设置导航活动。问题是我无法在菜单片段之间切换。看起来菜单项不可单击

我试着用之前设置菜单(操作栏)的方法设置导航活动,但它不起作用。此外,对于单击侦听器,项目不会做出反应。 我现在的问题是如何使
onNavigationItemSelected()
方法工作

抱歉,代码太多了

public class NavActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav);
    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();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // 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,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.nav, 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(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_home: {
            Toast.makeText(NavActivity.this, "Anmeldung Fehlgeschlagen", Toast.LENGTH_LONG).show();
            return true;
        }

        case R.id.nav_gallery:
            // User chose the "Favorite" action, mark the current item
            // as a favorite...
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
app_bar_nav.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
tools:context=".NavActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"
    app:srcCompat="@android:drawable/ic_dialog_email" />

<include layout="@layout/content_nav" />

activity_nav_drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:App="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_menu_camera"
        android:title="@string/menu_home"
        android:clickable="true"/>
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_menu_gallery"
        android:title="@string/menu_gallery"
        android:clickable="true"/>
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="@string/menu_slideshow"
        App:showAsAction="always"
        android:clickable="true"/>
    <item
        android:id="@+id/nav_tools"
        android:icon="@drawable/ic_menu_manage"
        android:title="@string/menu_tools"
        android:clickable="true"/>
</group>

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="@string/menu_share"
            android:clickable="true"/>
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="@string/menu_send"
            android:clickable="true"/>
    </menu>
</item>

mobile_navigation.xml

<navigation 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:id="@+id/mobile_navigation"
app:startDestination="@+id/nav_home">

<fragment
    android:id="@+id/nav_home"
    android:name="com.example.login.ui.home.HomeFragment"
    android:label="@string/menu_home"
    tools:layout="@layout/fragment_home"
    android:clickable="true"/>

<fragment
    android:id="@+id/nav_gallery"
    android:name="com.example.login.ui.gallery.GalleryFragment"
    android:label="@string/menu_gallery"
    tools:layout="@layout/fragment_gallery" />

<fragment
    android:id="@+id/nav_slideshow"
    android:name="com.example.login.ui.slideshow.SlideshowFragment"
    android:label="@string/menu_slideshow"
    tools:layout="@layout/fragment_slideshow" />

<fragment
    android:id="@+id/nav_tools"
    android:name="com.example.login.ui.tools.ToolsFragment"
    android:label="@string/menu_tools"
    tools:layout="@layout/fragment_tools" />

<fragment
    android:id="@+id/nav_share"
    android:name="com.example.login.ui.share.ShareFragment"
    android:label="@string/menu_share"
    tools:layout="@layout/fragment_share" />

<fragment
    android:id="@+id/nav_send"
    android:name="com.example.login.ui.send.SendFragment"
    android:label="@string/menu_send"
    tools:layout="@layout/fragment_send" />

acitivity_nav.xml

<androidx.drawerlayout.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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<com.google.android.material.navigation.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_nav"
    app:menu="@menu/activity_nav_drawer" />

<include
    layout="@layout/app_bar_nav"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


我已更新我的答案,请检查我是否添加了侦听器

public class NavActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav);
    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();
        }
    });
    DrawerLayout 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,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.nav, 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(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_home: {
            Toast.makeText(NavActivity.this, "Anmeldung Fehlgeschlagen", Toast.LENGTH_LONG).show();
            return true;
        }

        case R.id.nav_gallery:
            // User chose the "Favorite" action, mark the current item
            // as a favorite...
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

我已经更新了我的答案,请检查我是否添加了侦听器

public class NavActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav);
    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();
        }
    });
    DrawerLayout 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,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.nav, 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(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_home: {
            Toast.makeText(NavActivity.this, "Anmeldung Fehlgeschlagen", Toast.LENGTH_LONG).show();
            return true;
        }

        case R.id.nav_gallery:
            // User chose the "Favorite" action, mark the current item
            // as a favorite...
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

导航框架应该自动处理
片段
开关。您是否在尝试调试时实现了OnNavigationItemSelectedListener的
?请您的问题添加包含
的布局。
中的抽屉必须列在最后,才能正确接收触摸事件。将
移动到
活动导航中的
之后。如果您最初没有自己修改该文件,那么很可能是由于Android Studio 3.5升级中的一个问题导致XML被错误地重新排列。如果这是你的情况,看看如何解决。请参阅最后一个链接副本,了解更多有关导航内容的信息。它很有效!谢谢你的帮助和时间!Cheers导航框架应该自动处理
片段
开关。您是否在尝试调试时实现了OnNavigationItemSelectedListener的
?请您的问题添加包含
的布局。
中的抽屉必须列在最后,才能正确接收触摸事件。将
移动到
活动导航中的
之后。如果您最初没有自己修改该文件,那么很可能是由于Android Studio 3.5升级中的一个问题导致XML被错误地重新排列。如果这是你的情况,看看如何解决。请参阅最后一个链接副本,了解更多有关导航内容的信息。它很有效!谢谢你的帮助和时间!干杯,把这个放在哪里?它应该解决什么问题?@NooruddinLakhani,你应该更具描述性,并提供一个完整的答案,正如OP所指出的。我已经更新了我的答案,请检查一下放在哪里?它应该解决什么问题?@NooruddinLakhani,你应该更具描述性,并提供一个完整的答案,正如OP所指出的。我已经更新了我的答案,请检查