android - contextual action mode in fragment - close if not focused? -
i implemented contextual action mode bar in nested fragement. fragment part of view pager , view pager fragment , part of navigation drawer.
my problem: want close contextual action mode bar if fragment no more focused. so, if swipe through view pager action mode bar should close. if use onpause()
method of nested fragment, method not called directly. waits until swiped 2 or 3 times forward... here pictures:
in second picture can see action mode bar still there. question is: in method should call actionmodebar.finish()
method, close directly action mode bar if leave fragment?
maybe code of fragment helps you:
public class editorfragment extends fragment { private static final string key_position="position"; listview listview; private boolean ismultiplelist = false; private actionmode acmode; private int counterchecked = 0; private actionmode.callback modecallback = new actionmode.callback() { public boolean onprepareactionmode(actionmode mode, menu menu){ return false; } public void ondestroyactionmode(actionmode mode) { listview.clearchoices(); (int = 0; < listview.getchildcount(); i++) listview.setitemchecked(i, false); listview.post(new runnable() { @override public void run() { listview.setchoicemode(listview.choice_mode_none); } }); ismultiplelist = false; counterchecked = 0; mode = null; } public boolean oncreateactionmode(actionmode mode, menu menu) { mode.settitle("1 aufgabe"); mode.getmenuinflater().inflate(r.menu.actionmode, menu); return true; } public boolean onactionitemclicked(actionmode mode, menuitem item) { switch (item.getitemid()) { case r.id.actionmode_delete: int choicecount = listview.getcount(); sparsebooleanarray spboolarray = listview.getcheckeditempositions(); dbaufgaben db = new dbaufgaben(mainactivity.getmcontext()); db.open(); (int = 0; < choicecount; i++) { if(spboolarray.get(i)){ db.deletcontact(listview.getitemidatposition(i)); } } cursor cursor = db.getallrecords(); adaptereingang adaptere = new adaptereingang(mainactivity.getmcontext(), cursor, 0); listview.setadapter(adaptere); db.close(); mode.finish(); break; case r.id.actionmode_cancel: mode.finish(); break; } return false; } }; //......// @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = null; int position = getarguments().getint(key_position, -1); switch(position){ case 0: rootview = inflater.inflate(r.layout.pager_list, null); listview = (listview) rootview.findviewbyid(r.id.pager_list); context context = mainactivity.getmcontext(); dbaufgaben db = new dbaufgaben(context); db.open(); cursor cursor = db.getallrecords(); adaptereingang adaptere = new adaptereingang(context, cursor, 0); listview.setadapter(adaptere); db.close(); listview.setonitemlongclicklistener(new onitemlongclicklistener(){ @override public boolean onitemlongclick(adapterview<?> adapterview, view view, int position, long id) { if(!ismultiplelist){ acmode = mainactivity.getinstance().startactionmode(modecallback); listview.setchoicemode(listview.choice_mode_multiple); listview.setitemchecked(position, true); ismultiplelist = true; counterchecked++; setnewtitle(); } else { listview.setitemchecked(position, true); counterchecked++; setnewtitle(); } return true; } }); listview.setonitemclicklistener(new onitemclicklistener(){ @override public void onitemclick(adapterview<?> adapterview, view view, int position, long id) { log.d(gettag(), "datensatz: "+string.valueof(id)); if(ismultiplelist){ if(listview.isitemchecked(position)){ listview.setitemchecked(position, true); counterchecked++; setnewtitle(); } else { listview.setitemchecked(position, false); counterchecked--; setnewtitle(); } } } }); break; default: rootview = inflater.inflate(r.layout.frag_dummy, null); textview txt = (textview) rootview.findviewbyid(r.id.dummy_txt); txt.settext(string.valueof(position)); break; } return(rootview); } public void setnewtitle(){ if(counterchecked == 1){ acmode.settitle(counterchecked+" aufgabe"); } else { acmode.settitle(counterchecked+" aufgaben"); } } @override public void onpause(){ super.onpause(); if(ismultiplelist){ acmode.finish(); } } }
viewpagers keep multiple pages active @ 1 time (by default, page before , page after shown page), hence why onpause() not called until swipe 2 pages away.
your best bet use viewpager.onpagechangelistener
, , show , hide actionmode in onpageselected(..)
(i.e. if page selected isn't 1 actionmode, hide actionmode). you'll have implement in activity hosts viewpager.
Comments
Post a Comment