i have application witch contains drawer menu navigation , fragment activity in fragment shown depending on drawer selection. have 2 fragments, simple 1 (settingsfragment) , 1 containing fragmenttabhost (searchfragment). fragmenttabhost displayed when application opened. problem if switch between fragments - go settings fragment searchfragment tabhost's content not displayed anymore (the oncreateview of fragment contained in first tab not called) unless switch between tabs.
so change between fragments using replace in activity:
public void replacerightfragment(int fragmentid) { fragment newfragment = null; switch (fragmentid) { case fragment_search_property: newfragment = getsupportfragmentmanager().findfragmentbytag(searchpropertyfragment.class.getsimplename()); if (newfragment == null) { newfragment = new searchpropertyfragment(); } break; case fragment_settings: newfragment = getsupportfragmentmanager().findfragmentbytag(settingsfragment.class.getsimplename()); if (newfragment == null) { newfragment = new settingsfragment(); } break; } if (newfragment != null) { if (!newfragment.isvisible()) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); // replace whatever in fragment_container view fragment, // , add transaction stack fragmenttransaction.replace(r.id.main_view, newfragment, newfragment.getclass().getsimplename()); // fragmenttransaction.remove(mainfragment); // fragmenttransaction.add(r.id.main_view, newfragment, newfragment.getclass().getsimplename()); // fragmenttransaction.addtobackstack(null); // commit transaction fragmenttransaction.commitallowingstateloss(); } hideslidemenu(); hidekeyboard(); } }
my tabhost layout:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <include layout="@layout/header_bar"/> <android.support.v4.app.fragmenttabhost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <tabwidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:orientation="horizontal" /> <framelayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> <framelayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </linearlayout> </android.support.v4.app.fragmenttabhost> </linearlayout>
... , tabhost setup in fragment:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_search_property, container, false); mtabhost = (fragmenttabhost) view.findviewbyid(android.r.id.tabhost); mtabhost.setup(getactivity(), getactivity().getsupportfragmentmanager(), r.id.realtabcontent); mtabhost.addtab( mtabhost.newtabspec(sell_fragment_tag).setindicator( getstring(r.string.search_property_sell_tab_title)), searchpropertysellfragmenttab.class, null); mtabhost.addtab( mtabhost.newtabspec(rent_fragment_tag).setindicator( getstring(r.string.search_property_rent_tab_title)), searchpropertyrentfragmenttab.class, null); mtabhost.addtab( mtabhost.newtabspec(add_advert_fragment_tag).setindicator( getstring(r.string.search_property_add_tab_title)), searchpropertyaddadvertfragmenttab.class, null); imagebutton mdrawerbutton = (imagebutton) view.findviewbyid(r.id.drawer_button); mdrawerbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { ((drawermenucontroller) getactivity()).ondrawerbuttonpressed(); // if (mdrawerlayout.isdraweropen(mdrawerlist)) { // mdrawerlayout.closedrawer(mdrawerlist); // } else { // mdrawerlayout.opendrawer(mdrawerlist); // } } }); mtabhost.ontabchanged(sell_fragment_tag); mtabhost.setontabchangedlistener(this); return view; } @override public void ondestroyview() { super.ondestroyview(); mtabhost.removeallviews(); mtabhost = null; }
any hints?
do not need tabwidget, fragmenttabhost add automaticly. put wedgit above realtabcontent, if want tabs @ top!
but can customise did...
<?xml version="1.0" encoding="utf-8"?>
<framelayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@android:id/tabhost" /> <framelayout android:id="@+id/homecontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@android:id/tabhost" android:layout_margintop="@dimen/button_height" android:background="#ffffff" android:visibility="gone" > </framelayout> <relativelayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:background="#ffffff" android:orientation="horizontal" > <imageview android:id="@+id/btn_homescreen_tab" android:layout_width="@dimen/button_height" android:layout_height="@dimen/button_height" android:layout_alignparentleft="true" android:src="@drawable/grange_logo" /> <android.support.v4.app.fragmenttabhost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerinparent="true" android:layout_marginleft="@dimen/button_height" android:layout_marginright="@dimen/button_height" /> <imageview android:id="@+id/ph_lable_title" android:layout_width="@dimen/button_height" android:layout_height="@dimen/button_height" android:layout_alignparentright="true" android:src="@drawable/contact_1" /> </relativelayout>
Comments
Post a Comment