android - Swipe view with different actionbars -
currently have action bars each fragment, when swipe fragment new actionbar appears in place of old one. want when swipe, want swipe away actionbar , place new one. each fragment should have own actionbar.
i'm not sure if making complete sense drew quick demonstration of i'm trying do.
in viewpager container (activity or fragment), add next line in page change listener:
invalidateoptionsmenu(); supportinvalidateoptionsmenu();//if using actionbar support library getactivity().invalidateoptionsmenu();//if viewpager container fragment
then in oncreateoptionsmenu(),
@override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { super.oncreateoptionsmenu(menu, inflater); inflater.inflate(r.menu.your_menu_layout, menu); }
the onprepareoptionsmenu() callback method called before menu shown, , going use make menu items visible depending on current fragment:
@override public boolean onprepareoptionsmenu(menu menu) { super.onprepareoptionsmenu(menu); int page = yourviewpager.getcurrentitem(); switch(page) { case 0: menu.finditem(r.id.item_f1).setvisible(true); menu.finditem(r.id.item_f2).setvisible(false); menu.finditem(r.id.item_f3).setvisible(false); break; case 1: menu.finditem(r.id.item_f1).setvisible(false); menu.finditem(r.id.item_f2).setvisible(true); menu.finditem(r.id.item_f3).setvisible(false); break; case 2: menu.finditem(r.id.item_f1).setvisible(false); menu.finditem(r.id.item_f2).setvisible(false); menu.finditem(r.id.item_f3).setvisible(true); break; } return true;
if viewpager container fragment, add sethasoptionsmenu(true), if activity it's not necessary.
reference - swipe view different actionbar items in each swipe fragment
Comments
Post a Comment