我创建了片段并根据菜单项的单击事件安排其可见性。当我先点击它,它可以正常工作,但再次点击片段可见性不隐藏。图像说明;当我点击设置灰色片段显示时,再次点击我想隐藏灰色片段,但它没有做这个东西。
这是我的代码;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_settings);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (findViewById(R.id.frameLayout) != null)
{
FragmentTest fragmenttest = new FragmentTest();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (fragmenttest.isVisible()) {
ft.hide(fragmenttest);
} else {
if (fragmenttest.isAdded()) {
} else {
ft.add(R.id.frameLayout, fragmenttest, "Hello Fragment");
}
ft.show(fragmenttest);
}
ft.commit();
}
return false;
}
});
return true;
}
答案 0 :(得分:1)
移动
FragmentTest fragmenttest = new FragmentTest();
到活动的顶部。