在我的应用中,我聊天,此聊天是片段(ChatFrag)。我从操作栏中的项目打开此片段。此项目设置为可见false,当BattleRoomFrag(片段)可见时,它将显示为true,因为ChatFrag仅适用于此会议室(BattleRoomFrag)。
我该怎么做?
要做到这一点,我试试这个。
custom_drawer_layout
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="br.com.williarts.batalhajuridica.CustomDrawerLayout" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<!--Chat Button Item -->
<item
android:id="@+id/action_chat"
android:icon="@drawable/batalha_icon_chat"
android:title="Chat"
app:showAsAction="always"
android:visible="false"
/>
ActionBarActivity
public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{
private ActionBar ab;
private DrawerLayout dl;
private ListView lv;
private ActionBarDrawerToggle tg;
private List<ItensListView> fragments;
private CharSequence tl; //titulo principal
private CharSequence tlf; //titulo fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_drawerlayout);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
init();
if(savedInstanceState == null){
selectedItem(0);
}
}
private void init(){
//actionbar
onConfigActionBar();
//listview
configItensListView();
lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments));
lv.setOnItemClickListener(this);
//drawerlayout
dl = (DrawerLayout)findViewById(R.id.dl);
//actionbardrawertoggle
tg = new ActionBarDrawerToggle(this, dl, R.drawable.btmenu, R.string.nomeActionBar){
public void onDrawerClosed(View view) {
ab.setTitle(tl);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
ab.setTitle(tlf);
supportInvalidateOptionsMenu();
}
};
dl.setDrawerListener(tg);
tl = tlf = getTitle();
}
/** ativa actionbar e botao home na action bar */
private void onConfigActionBar(){
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
tg.onConfigurationChanged(newConfig);
}
/** necessario */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
tg.syncState();
}
/** necessario */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
FragmentTransaction ft;
Fragment frag;
if(item.getItemId() == R.id.action_chat){
frag = new ChatFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag, "ChatFrag");
ft.addToBackStack("back");
ft.commit();
}
if (tg.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** necessario */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_drawer_layout, menu);
return true;
}
/** necessario */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean status = dl.isDrawerOpen(lv);
menu.findItem(R.id.action_settings).setVisible(!status);
BattleRoomFrag brf = (BattleRoomFrag)getSupportFragmentManager().findFragmentByTag("BattleRoomFrag");
MenuItem m = menu.getItem(R.id.action_chat);
if(brf != null && brf.isVisible()){
m.setVisible(true);
}else{
m.setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}
答案 0 :(得分:2)
试试这个:在片段构造函数中,你可以设置setHasOptionsMenu(true);在onCreateOptionsMenus中膨胀片段菜单inflater.inflate(R.menu.chat_frag,menu); (为chat_frag创建自定义菜单)