我正在使用活动中的选项菜单,因为我已经为菜单项充气了菜单和onItemSelected。在片段中,我隐藏了不需要在片段上显示的菜单项。
这很好,我可以在片段上看到所需的菜单项。但在一些片段我不想显示任何菜单项。所以对于这个我试图清除菜单,我也尝试将setVisible false设置为所有项目,也尝试setHasOptionsMenu(false); ,但没有一个工作。我可以看到该片段上的所有三个菜单项。
此外,我尝试了这个答案:隐藏/显示不同片段的操作栏选项菜单项
来自onPreparedOptionsMenu的搜索商家片段我做了menu.setGroupVisible(R.id.main_menu_group,false);
仍然无法正常工作。
菜单
<?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">
<group
android:id="@+id/main_menu_group">
<item
android:id="@+id/action_search"
android:orderInCategory="100"
android:title="customer"
app:showAsAction="always"
android:icon="@drawable/ic_search"/>
<item
android:id="@+id/action_create"
android:orderInCategory="100"
android:title="customer"
app:showAsAction="always"
android:icon="@drawable/ic_create"/>
<item
android:id="@+id/action_edit"
android:orderInCategory="100"
android:title="customer"
app:showAsAction="always"
android:icon="@drawable/ic_save"/>
</group>
</menu>
家庭活动:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// menu.clear();
getMenuInflater().inflate(R.menu.menu_customer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create:
String favMerchantId = pref.getString("favMerchantId","");
String favKiranaName = pref.getString("favKiranaName","");
if(pref.getString("favMerchantId","").equals("null") && pref.getString("favKiranaName","").equals("null")){
CommonUtils.showAlert(HomeActivity.this,"There is no merchant yet.Please search for merchant.","No Merchant");
}
else {
FragmentManager fragmentManager = getSupportFragmentManager();
AddOrderFragment fragment1 = new AddOrderFragment();
Bundle bundle = new Bundle();
bundle.putString("kiranaName", favKiranaName);
bundle.putString("merchant_id", String.valueOf(favMerchantId));
bundle.putBoolean("ordersFrag", true);
fragment1.setArguments(bundle);
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment1, "RETRIEVE_ADDORDER_FRAGMENT").addToBackStack("B").commit();
}
break;
case R.id.action_edit:
if (CommonUtils.isConnectedToInternet(HomeActivity.this)) {
if(AddOrderFragment.mOrderItemsList.isEmpty())
{
CommonUtils.showAlert(HomeActivity.this,getResources().getString(R.string.listAlert),"List");
// showAlert(getString(R.string.listAlert));
}
else {
Log.e("itemList", String.valueOf(AddOrderFragment.mOrderItemsList.size()));
Gson gson = new Gson();
String toServer = gson.toJson(
Collections.singletonMap("items", AddOrderFragment.mOrderItemsList)
);
String date_time = String.valueOf(CommonUtils.getCurrentDateTime());
new AddOrderAsyncTask(HomeActivity.this,AddOrderFragment.ordersFrag).execute(toServer,pref.getString("api_key",""), pref.getString("sessionUserId",""), AddOrderFragment.mMerchantId,date_time);
}
}
else {
// showAlert(getString(R.string.check_network));
CommonUtils.showAlert(HomeActivity.this,getResources().getString(R.string.check_network),"Check network");
}
break;
case R.id.action_search:
// go to search fragment
FragmentManager fragmentManager = HomeActivity.this.getSupportFragmentManager();
SearchMerchantFragment fragment1 = new SearchMerchantFragment();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment1).addToBackStack("D").commit();
return true;
default:
break;
}
return true;
}
order's fragment to hide some menu items :
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem someMenuItem3 = menu.findItem(R.id.action_create);
someMenuItem3.setVisible(true);
// This does work
MenuItem someMenuItem = menu.findItem(R.id.action_search);
someMenuItem.setVisible(false);
MenuItem someMenuItem1 = menu.findItem(R.id.action_edit);
someMenuItem1.setVisible(false);
}
搜索商家片段:在此我要隐藏菜单或所有菜单项。
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem someMenuItem3 = menu.findItem(R.id.action_create);
someMenuItem3.setVisible(false);
// This does work
MenuItem someMenuItem = menu.findItem(R.id.action_search);
someMenuItem.setVisible(false);
MenuItem someMenuItem1 = menu.findItem(R.id.action_edit);
someMenuItem1.setVisible(false);
}
如何隐藏此菜单?
请帮忙......谢谢..
答案 0 :(得分:9)
你可以试试这个,
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
}
和
片段onCreate中的 setHasOptionsMenu(true);
。
理想情况下setHasOptionsMenu(false);
应该可以使用,但您可以尝试其他选项。