【问题标题】:NavigationView actionaBar onOptionsItemSelected R.id.home not triggeredNavigationView actionaBar onOptionsItemSelected R.id.home 未触发
【发布时间】:2016-08-08 15:18:09
【问题描述】:

我想在一个只有一个活动的 android 应用程序中实现向上按钮,该活动用不同的片段更改其内容。 我使用了 android studio 提供的默认导航抽屉活动,我在 content_main 中添加了一个 frameLayout。 在我希望显示向上按钮的片段中,我在 onCreateView 方法中添加了这行代码:

ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

在 onCreate 方法中添加这一行:

setHasOptionsMenu(true);

我添加了捕捉点击的方法:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        Log.w("second fragment","clicked back");
        return true;
    }
    return super.onOptionsItemSelected(item);
}

在活动中我这样设置 onCreateOptionsMenu:

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

但没有触发它的点击。 我试图添加一个设置按钮,它被触发了。 我已经阅读了很多关于此的问题,但我不知道如何解决它

【问题讨论】:

    标签: android android-fragments navigation-drawer android-navigationview


    【解决方案1】:

    setHasOptionsMenu(true) 应该在方法onCreate() 中调用,让 FragmentManager 知道您的片段需要接收选项菜单回调。

    【讨论】:

    • 我把它放在了我忘了添加问题中
    • 如何实现onCreateOptionsMenu
    • 一切似乎都很好:你调用了 setHasOptionsMenu,你使用 android.R.id.home 而不是 R.id.home,onCreateOptionsMenu 就可以了。我不知道。 orz....
    • 已经尝试了两者都没有工作。奇怪的是,即使在R.id.home 上的活动onOptionsItemSelected 也没有被触发
    【解决方案2】:

    我相信您将this constructor 用于ActionBarDrawerToggle,它将Toolbar 作为参数。如果您使用此构造函数,则在您单击指标时不会调用onOptionsItemSelected。有关于这个问题的问题/答案,例如:

    AppCompat v7 Toolbar onOptionsItemSelected not called

    但对我来说,它们都不是完美的,所以我使用了特定于工具栏的构造函数,这是我的情况的解决方法。我在工具栏顶部放置了一个透明视图,只有当我显示后退按钮并且我自己处理点击时它才“可见”(在我的情况下调用 onBackPressed())。

    我知道这是一种 hack,但它需要并且对我有用。

    <?xml version="1.0" encoding="utf-8"?>
    <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:fitsSystemWindows="true"
        tools:openDrawer="start">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/main_background">
    
                <android.support.design.widget.AppBarLayout
                    android:id="@+id/appBar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:theme="@style/AppTheme.AppBarOverlay">
    
                    <android.support.v7.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"/>
    
                </android.support.design.widget.AppBarLayout>
    
                <FrameLayout
                    android:id="@+id/layoutMainContent"
                    android:layout_below="@id/appBar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
    
            </RelativeLayout>
    
            <!-- The important view -->
            <View
                android:id="@+id/viewFakeBack"
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:clickable="true"
                android:visibility="gone"/>
    
        </FrameLayout>
    
        <android.support.design.widget.NavigationView
            android:id="@+id/navigationView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:menu="@menu/navigation_drawer"/>
    
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

    • 谢谢,最后我从各种问题中找到了另一种解决方法,因为对我来说也不行
    【解决方案3】:

    尝试使用 switch(...) case 语句。

     @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        //this will make Hamburger button clickable.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
    
        //this will make the HomeAsUpIndicator button clickable.
        switch (item.getItemId()) {
            case android.R.id.home:
               Log.w("second fragment","clicked back");
               break;
        }
        return super.onOptionsItemSelected(item);
    }
    

    希望这会对你有所帮助。

    【讨论】:

    • 抱歉,在我的情况下,没有调用整个 onOptionsItemSelected。
    • 这没有任何意义。为什么开关与问题有关?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多