onOptionsItemSelected侦听器无法正常工作

时间:2016-08-05 08:36:30

标签: android onclicklistener menuitem

我正在开发一个Android应用程序,我的选项菜单有问题。按下菜单项时不会调用onOptionsItemSelected()方法,我使用actionLayout,因此,在本网站发布的许多问题中,我设置了自己的监听器。似乎没有任何作用,我无法看到我的解决方案与其他解决方案的区别。

这是我的代码:

  

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

}
  

在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>
  

在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>

至于日志,&#34;听众:创建&#34;是唯一印刷过的。

我已经尝试了几个相关问题的解决方案,例如this onethis one。 actionlayout代表购物车内的徽章,我知道它与动作布局工作之前有关。

感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

删除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>
  

真正解决问题的是com:joanzapata.iconify.widget.IconButton上的android:clickable =“false”

相关问题