【问题标题】:How to make menu items clickable? [duplicate]如何使菜单项可点击? [复制]
【发布时间】:2019-09-03 14:44:33
【问题描述】:

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

我尝试以与之前设置菜单(操作栏)相同的方式设置导航活动,但它只是不起作用。同样使用单击侦听器,项目不会做出反应。 我现在的问题是如何使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" />

acitivty_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" />

【问题讨论】:

  • 导航框架应该自动处理Fragment 开关。您是否在尝试调试时实现了OnNavigationItemSelectedListener?请edit您的问题添加包含&lt;DrawerLayout&gt;的布局。
  • &lt;DrawerLayout&gt; 中的抽屉必须排在最后,才能正确接收触摸事件。将&lt;NavigationView&gt; 移动到activity_nav 中的&lt;include&gt; 之后。如果您最初没有自己修改该文件,则可能是由于 Android Studio 3.5 升级中的问题导致 XML 重新排列不正确。如果这是您的情况,请查看this post 以了解如何解决该问题。有关导航内容的更多信息,请参阅最后一个链接副本上的this post
  • 有效!感谢您的帮助和时间!干杯

标签: java android android-fragments navigation


【解决方案1】:

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

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);
    }

【讨论】:

  • 把这个放在哪里?应该解决什么问题?
  • @NooruddinLakhani,正如 OP 指出的那样,您应该更具描述性并提供完整的答案。
  • 我已经更新了我的答案,请检查
猜你喜欢
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
相关资源
最近更新 更多