问题描述
我有一个的FrameLayout和菜单中的ActionBar活动。当用户点击菜单项我替换为相关的新片段的片段。不过,我看不到一个明显的方式来删除的菜单项所选择的片段。
公共类MainActivity扩展ActionBarActivity {
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
如果(savedInstanceState == NULL){
StudyFragment startFragment =新StudyFragment();
startFragment.setArguments(getIntent()getExtras());
getSupportFragmentManager()调用BeginTransaction()。加
(R.id.container,startFragment).commit();
}
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
} @覆盖
公共布尔onOptionsItemSelected(菜单项项){
INT ID = item.getItemId(); 开关(ID){
案例R.id.action_study:
replaceFragment((片段)新StudyFragment());
打破;
案例R.id.action_list:
replaceFragment((片段)新ListFragment());
打破;
//等
}
返回super.onOptionsItemSelected(项目);
} 私人无效replaceFragment(片f){
FragmentTransaction交易=
。getSupportFragmentManager()的BeginTransaction();
transaction.replace(R.id.container,F);
transaction.addToBackStack(NULL); 器transaction.commit();
}
在表示禁用菜单上prepareOptionsMenu - 但我怎么会知道哪些项目被选中
?- 解决方案实现 -
使用穆罕默德Refaat的解决方案,下面我添加了两个新成员到类:
私人菜单activityMenu;
私人菜单项curMenuItem;
设置他们onCreateOptionsMenu
activityMenu =菜单;
curMenuItem = activityMenu.findItem(R.id.action_study);
curMenuItem.setVisible(假);
和改变了他们对onOptionsItemSelected
curMenuItem.setVisible(真);
curMenuItem = activityMenu.findItem(ID);
curMenuItem.setVisible(假);
首先得到你想要删除的项目:
菜单项项目= menu.findItem(R.id.your_action);
然后设置它的能见度假
:
item.setVisible(假);
如果问题是获得菜单(因为它不是在片段),你可以很容易地得到一个背景
从包含菜单,并获得活动通过它的菜单。
I have an ActionBar activity with a FrameLayout and a menu. when the user clicks the menu item I replace the fragment with the relevant new fragment. However, I cannot see an obvious way to remove the menu item for the selected fragment.
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
StudyFragment startFragment = new StudyFragment();
startFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add
(R.id.container, startFragment).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_study:
replaceFragment((Fragment)new StudyFragment());
break;
case R.id.action_list:
replaceFragment((Fragment)new ListFragment());
break;
// etc
}
return super.onOptionsItemSelected(item);
}
private void replaceFragment(Fragment f) {
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, f);
transaction.addToBackStack(null);
transaction.commit();
}
The Google documentation on changing menus says to disable the menu in onPrepareOptionsMenu - but how will I know which item has been selected?
--Solution Implemented--
Using Muhammed Refaat's solution below I added two new members to the class:
private Menu activityMenu;
private MenuItem curMenuItem;
Set them in onCreateOptionsMenu
activityMenu = menu;
curMenuItem = activityMenu.findItem(R.id.action_study);
curMenuItem.setVisible(false);
And changed them on onOptionsItemSelected
curMenuItem.setVisible(true);
curMenuItem = activityMenu.findItem(id);
curMenuItem.setVisible(false);
First get the item you want to remove :
MenuItem item = menu.findItem(R.id.your_action);
then set it's Visibility false
:
item.setVisible(false);
and if the problem is in getting the menu (as it's not in the fragment), you can easily get a context
from the activity that contains the menu and get the menu by it.
这篇关于Android的 - 如何隐藏当前片段菜单选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!