Child fragment from onListItemClick within viewPager behaves unexpectedly -
i have 3 listfragments being handled viewpager (managed fragmentadapter) - work perfectly. when user clicks item in listfragment #1, new fragment should open details. it's behaving strangely in following manner:
only clicking list item twice opens detailfragment, yet debugging shows first click indeed goes detailfragment, doesn't show view (the view still shows current listfragment).
after clicking 2nd time, detailfragment show it's layout, not elements within (like textview, etc).
if user 'accidently' swipes screen when detailfragment showing, viewpager sets in place of 2nd listfragment! when pressing on detailfragment view 'reset' viewpager it's correct listfragment. of course if user swipes when in detailfragment, next listfragment of viewpager should appear, , detailfragment should removed.
thanks tips muddling through android's odd world of fragments , views :)
public class planetfragment extends listfragment{ layoutinflater inflater; listview list; arraylist<hashmap<string, string>> planetlistarray; hashmap<string, string> planetmap; activity activity; context context; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.planets_tab_layout, container, false); inflater=(layoutinflater)getlayoutinflater(savedinstancestate); activity = getactivity(); context = planetfragment.this.getactivity(); string dbtablename = "table_planets"; sqlitehelper info = new sqlitehelper(getactivity().getbasecontext()); info.open(); arraylist<hashmap<string, string>> datafromsql = info.getdata(dbtablename); if(!datafromsql.isempty()){ planetlistarray = new arraylist<hashmap<string, string>>(); (int = 0; i<datafromsql.size(); i++){ planetmap = new hashmap<string, string>(); planetmap.put(planet_id, datafromsql.get(i).get(key_planet_id)); planetmap.put(zodiac_id, datafromsql.get(i).get(key_zodiac_id)); planetmap.put(degrees, datafromsql.get(i).get(key_degrees)); planetmap.put(content, datafromsql.get(i).get(key_content)); planetlistarray.add(planetmap); } info.close(); } list = (listview) v.findviewbyid(android.r.id.list); planetadapter adapter=new planetadapter(getactivity(), r.layout.planets_row, planetlistarray); list.setadapter(adapter); return v; } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); //the dividers getlistview().setdivider(getresources().getdrawable(r.drawable.purplebartop)); } @override public void onlistitemclick(listview l, view v, int position, long id) { super.onlistitemclick(l, v, position, id); hashmap<string, string> item = planetlistarray.get(position); bundle bundle = new bundle(); bundle.putserializable("itemmap", item); bundle.putint("position", position); fragment frag = detailfragment.newinstance(); frag.setarguments(bundle); if (frag != null) { getactivity().getsupportfragmentmanager() .begintransaction() .replace(r.id.pager, frag, "frag") .addtobackstack("frag") .commit(); } } } public class detailfragment extends fragment{ context context; activity activity; textview planetname; public static android.support.v4.app.fragment newinstance() { detailfragment f = new detailfragment(); return f; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { inflater=(layoutinflater)getlayoutinflater(savedinstancestate); view v = inflater.inflate(r.layout.dialog_details, container, false); activity = getactivity(); context = detailfragment.this.getactivity(); planetname = (textview)v.findviewbyid(r.id.planetnameexpanded); planetname.settext("planetx"); return v; } }
edit: instead of getactivity().getsupportfragmentmanager()
have tried getchildfragmentmanager()
gives error: method getchildfragmentmanager()
undefined type planetfragment.
when click on list item, indeed constructing new details fragment , telling fragment manager replace tag "frag" fragment. however, not telling view pager switch on fragment.
since have back-pointer activity, use findviewbyid find view pager, , call viewpager.setcurrentitem.
i think might asking trouble constructing new details fragment inside of list fragment. when use fragmentpageradapter, adapter constructs fragments. have implemented letting adapter make fragments, , in onlistitemclick find existing details fragment , call method on configure new data. maybe setcurrentitem fix problem.
edit
first, write fragmentpageradapter can use getitem fetch existing fragment, without creating new 1 each time.
public class planetfragmentadapter extends fragmentpageradapter { private fragment [] fragments = new fragments[3]; public planetfragmentadapter(fragmentmanager fm) { super(fm); } @override public int getcount() { return 3; } @override public fragment getitem(int position) { fragment fragment = fragments[position]; if (fragment == null) { switch (position) { case 0: fragment = new planetfragment(); break; case 1: fragment = new detailfragment(); break; case 2: fragment = new mysteryfragment(); break; } fragments[position] = fragment; } return fragment; } }
also add functions in activity work fragments:
public void setpage(int position) { viewpager.setcurrentitem(position); } public detailfragment getdetailfragment() { return (detailfragment) viewpager.getitem(1); // doesn't create new instance // use getsupportfragmentmanager().findfragmentbyid() here }
now when click on item in list fragment, can existing detail fragment, configure it, , set viewpager show detail fragment.
@override public void onlistitemclick(listview l, view v, int position, long id) { super.onlistitemclick(l, v, position, id); hashmap<string, string> item = planetlistarray.get(position); bundle bundle = new bundle(); bundle.putserializable("itemmap", item); bundle.putint("position", position); planetactivity pa = (planetactivity) activity; detailfragment frag = pa.getdetailfragment(); frag.setarguments(bundle); pa.setcurrentitem(1); }
Comments
Post a Comment