Android:片段的onOptionsItemSelected不会被调用

时间:2015-06-07 08:57:50

标签: android android-fragments android-menu android-actionbaractivity

我有一个ActionBarActivity和一个片段。活动没有菜单膨胀,而片段有一个带两个按钮的菜单。片段菜单可见,但点击时按钮完全没有反应。在调试时,我可以看到调用Fragment和Activity的onCreateOptionsMenu(),但是当点击按钮时,不会调用onOptionsItemSelected(),也不会调用Activity和Fragment。

活动

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return false;
}

片段

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mainActivity = (NavigationActivity)getActivity();

    setHasOptionsMenu(true);
}

 @Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState){
    return (ScrollView) inflater.inflate(R.layout.tutoring_detail, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.query_details_menu, menu);

    super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.accept_query:
            respondToQuery(true);
            return true;
        case R.id.decline_query:
            respondToQuery(false);
            return true;
        default:
            break;
    }

    return false;
}

要在片段中显示的菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

<item
    android:id="@+id/accept_query"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:checkable="true"
    style="?android:attr/borderlessButtonStyle"
    app:actionViewClass="android.widget.ImageButton"/>

<item
    android:id="@+id/decline_query"
    android:orderInCategory="101"
    app:showAsAction="always"
    android:checkable="true"
    style="?android:attr/borderlessButtonStyle"
    app:actionViewClass="android.widget.ImageButton"/>
</menu>

7 个答案:

答案 0 :(得分:4)

在Activity类中,

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);   
                return false;
}

在片段中,

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(getActivity(), "called " + item.getItemId(), Toast.LENGTH_SHORT).show();
    return super.onOptionsItemSelected(item);
}

答案 1 :(得分:3)

您必须在父活动的init()方法中使用super.onOptionsItemSelected(item)

来自Activity | Android Developers

  

派生类应该调用基类来执行默认菜单处理。

答案 2 :(得分:1)

尝试在片段中的setHasOptionsMenu(true)方法中移动onCreateView()而不是onCreate()

答案 3 :(得分:1)

在为我的动作按钮制作特殊布局后,我遇到了类似的问题(我制作了一个图像按钮,需要填充它并更改其他一些东西,所以我不得不使用布局)。 然后onOptionsItemSelected与此imageButton失去了连接,所以我只是在onCreateOptionsMenu中使用clickListener。这可能不是最好的做法,也许有更好的解决方案,但这就是解决我问题的方法。

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.picker_list_menu, menu);

    MenuItem itemDone = menu.findItem(R.id.menu_done);
    MenuItemCompat.setActionView(itemDone, R.layout.menu_layout_done);
    menuDoneIB = (ImageButton) MenuItemCompat.getActionView(itemDone);
    itemDone.getActionView().setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
            //your code..
       }
     });
 }

答案 4 :(得分:0)

尝试使用oncreateview

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

答案 5 :(得分:0)

如果您有动态更改的菜单项,例如徽章菜单(https://stackoverflow.com/a/16648170/2914140https://stackoverflow.com/a/26017587/2914140),则应在onCreateOptionsMenu中初始化该项目,并在每次更改项目后重新设置setOnClickListeners。 就我而言:

private MenuItem menuItem;

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_catalog, menu);

    menuItem = menu.findItem(R.id.action_badge);
    writeBadge(0);

    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_badge) {
        // Your code.
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void writeBadge(int count) {
    MenuItemCompat.setActionView(menuItem, R.layout.item_badge);
    RelativeLayout layout = (RelativeLayout) MenuItemCompat.getActionView(menuItem);
    // A TextView with number.
    TextView tv = (TextView) layout.findViewById(R.id.badge);
    if (count == 0) {
        tv.setVisibility(View.INVISIBLE);
    } else {
        tv.setVisibility(View.VISIBLE);
        tv.setText(String.valueOf(count));
    }
    // An icon, it also must be clicked.
    ImageView imageView = (ImageView) layout.findViewById(R.id.image);
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onOptionsItemSelected(menuItem);
        }
    };
    menuItem.getActionView().setOnClickListener(onClickListener);
    imageView.setOnClickListener(onClickListener);
}

答案 6 :(得分:0)

我将click事件传递给活动的片段。这对我有用。

import itertools


a = [1, 4, 4, 9]  # any list of nums
groups = 2
result = 8
combinations = [combination for combination in itertools.combinations(a, groups)]
output = [combination for combination in combinations if sum(combination) == result]

print(output)
>>> [(4, 4)]