简体   繁体   English

Android Actionbar菜单项未选择

[英]Android Actionbar Menu Item not Selecting

I have an android Activity with a bottom menu navigation bar, That navigates between three fragments and a single top menu item that should open a new activity, But the top menu ActionBar item is not responding to click events. 我有一个带有底部菜单导航栏的android Activity,它在三个片段和一个顶部菜单项之间导航,该顶部菜单项应打开一个新活动,但是顶部菜单ActionBar项未响应click事件。 Maybe there is something am missing? 也许缺少什么? Or should I be handling the menu from within my fragments? 还是应该从片段中处理菜单?

Here is my code: 这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // Set Network Connection Listener
    setConnectionListener(this);

    //check the network connectivity when activity is created
    checkConnection();

    BottomNavigationView bottomNavigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    bottomNavigation.setOnNavigationItemSelectedListener(this);

    transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout, HomeFragment.newInstance());
    transaction.commit();

    // Used to select item programmatically
    // bottomNavigation.getMenu().getItem(0).setChecked(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.top, menu);
    // return super.onCreateOptionsMenu(menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.i(TAG, "Menu Clicked " + item.getItemId());
    switch (item.getItemId()){
        case R.id.tab_cart:
            Intent intent = new Intent(this, CartActivity.class);
            startActivity(intent);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

My Menu top.xml res menu file 我的菜单top.xml res菜单文件

<?xml version="1.0" encoding="utf-8"?>
<menu
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"
tools:context="com.shopcentra.activities.MainActivity">
<item
    android:id="@+id/tab_cart"
    android:icon="@mipmap/cart"
    android:title="@string/cart"
    app:actionLayout="@layout/notification_layout"
    app:showAsAction="always">
</item>
</menu>

I can't see where you've added a View.OnClickListener on the "tab_cart". 我看不到您在“ tab_cart”上添加了View.OnClickListener的位置。 If you have set one, please share that code as well otherwise, add an OnCLickListener on that view and see if the problem persists then as well. 如果已设置,请也共享该代码,否则,请在该视图上添加一个OnCLickListener,然后查看问题是否仍然存在。
EDIT : code to add OnClickListener on menu item: 编辑 :在菜单项上添加OnClickListener的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem tabCartMenuItem = menu.findItem(R.id.tab_cart);
    View notificationActionView = menuItem.getActionView();
    notificationActionView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onOptionsItemSelected(tabCartMenuItem));
        }
    });
}

Try adding this code to your onCreateOptionsMenu() method. 尝试将此代码添加到onCreateOptionsMenu()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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