How to replace an Android fragment with another fragment inside a ViewPager? -
i have view pager consisting of 4 tabs. each tab holds own fragment.
how able use fragment transaction replace fragment in tab 3 new fragment?
i've tried lot of things, have not been able come solution @ all. i've tried looking around on google , stackoverflow, no success.
i assume fragment has button put in center. clicking on it, can change layout stays under of button. content/layout of first fragment mentioned should replaced wrappera , content/layout of second 1 should replaced wrapperb. put simple red background wrappera distinguish wrapperb, wrapperb green due same reason. hope want:
public class switchingfragment extends fragment { button switchfragsbutton; relativelayout wrappera, wrapperb; view rootview; @override public view oncreateview(layoutinflater inflater, final viewgroup container, bundle savedinstancestate) { rootview = inflater.inflate(r.layout.layout_switch, container, false); wrappera = (relativelayout) rootview.findviewbyid(r.id.wrappera); wrapperb = (relativelayout) rootview.findviewbyid(r.id.wrapperb); switchfragsbutton = (button) rootview.findviewbyid(r.id.switchfragsbutton); switchfragsbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { if (wrapperb.getvisibility() == view.gone) { wrapperb.setvisibility(view.visible); // there no need change visibility of wrappera since stays behind when wrapperb visible. // wrappera.setvisibility(view.gone); } else { wrapperb.setvisibility(view.gone); // again, there no need. // wrappera.setvisibility(view.visible); } } }); return rootview; } }
the layout:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:orientation="vertical"> <!-- content of first fragment should in "wrappera". --> <relativelayout android:id="@+id/wrappera" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/red" android:visibility="visible"> </relativelayout> <!-- content of second fragment should in "wrapperb". --> <relativelayout android:id="@+id/wrapperb" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/green" android:visibility="gone"> </relativelayout> <!-- said, assume layout switcher button stable , should in front of switching layouts. --> <button android:id="@+id/switchfragsbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:background="@color/black" android:text="switch" android:textsize="20sp" android:textstyle="bold" android:textcolor="@color/white"/> </relativelayout>
alternatively, can try change fragment directly notifying fragmentpageradapter described in link: replace fragment fragment inside viewpager
Comments
Post a Comment