onOptionsItemSelected 侦听器不工作

onOptionsItemSelected listener not working

我正在开发一个 android 应用程序,但选项菜单有问题。按下菜单项时不会调用 onOptionsItemSelected() 方法,我使用的是 actionLayout,因此,正如本网站上发布的许多问题一样,我设置了自己的侦听器。似乎没有任何效果,我看不出我的解决方案与其他解决方案有何不同。

这是我的代码:

On MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    Log.d("Listener", "Created");
    getMenuInflater().inflate(R.menu.main, menu);
    for (int i = 0; i < menu.size(); i++) {
        final MenuItem item = menu.getItem(i);
        if (item.getItemId() == R.id.action_cart) {
            View itemChooser = item.getActionView();
            if (itemChooser != null) {
                itemChooser.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d("Listener", "Clicked1");
                        onOptionsItemSelected(item);
                    }
                });
            }
        }
    }
    return super.onCreateOptionsMenu(menu);
}

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
        case R.id.action_cart:
            Log.d("Listener", "Clicked");
            CartFragment newFragment = new CartFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);
            // Commit the transaction
            transaction.commit();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}

On menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:title="cart"
    android:id="@+id/action_cart"
    app:showAsAction="always"
    app:actionLayout="@layout/badge_layout"/>

</menu>

On layout/badge_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:addStatesFromChildren="true">

<com.joanzapata.iconify.widget.IconButton
    android:layout_width="44dp"
    android:layout_height="44dp"
    android:textSize="24sp"
    android:textColor="#ffffff"
    android:background="@mipmap/ic_cart"
    android:id="@+id/badge_icon_button"/>

<TextView
    android:id="@+id/badge_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/badge_icon_button"
    android:layout_alignLeft="@id/badge_icon_button"
    android:layout_alignStart="@id/badge_icon_button"
    android:text="10"
    android:paddingEnd="12dp"
    android:paddingRight="0dp"
    android:paddingLeft="0dp"
    android:gravity="center"
    android:textColor="#ffffff"
    android:textSize="13sp"
    android:textStyle="bold"
    android:background="@drawable/badge_circle"/>
</FrameLayout>

至于日志,"Listener: Created" 是唯一打印的。

我已经尝试了几个相关问题的解决方案,例如this one and this one。 actionlayout 代表购物车内的徽章,我知道它与此有关,因为在 actionlayout 之前它起作用了。

感谢任何帮助。谢谢。

删除 onOptionsItemSelected 方法并将 oncreateOptionsMenu 更改为

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem item = menu.findItem(R.id.action_cart);

    if (item != null) {

        item.getActionView().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("Listener", "Clicked");
            }
        });
    }

    return true;
}

和布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true">

    <com.joanzapata.iconify.widget.IconButton
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:clickable="false"
        android:textSize="24sp"
        android:textColor="#ffffff"
        android:background="@mipmap/ic_cart"
        android:id="@+id/badge_icon_button"/>

    <TextView
        android:id="@+id/badge_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/badge_icon_button"
        android:layout_alignLeft="@id/badge_icon_button"
        android:layout_alignStart="@id/badge_icon_button"
        android:text="10"
        android:paddingEnd="12dp"
        android:paddingRight="0dp"
        android:paddingLeft="0dp"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="13sp"
        android:textStyle="bold"
        android:background="@android:color/holo_red_dark"/>
</RelativeLayout>

What really solved the problem was android:clickable="false" on com.joanzapata.iconify.widget.IconButton