android - Fragment container with Viewpager, tabs, and drawerlayout doesn't work -
i have drawerlayout, viewpager, , tablayout in activity_main.xml
have 2 fragments homefragment
, bookingfragment
, i'm trying launch bookingfragment
activity's on itemclick. have tried multiple solutions creating framelayout id container
, put pager inside , user in fragment transaction , tried making empty framelayout same id , put above pager, didn't work.
here's code method i'm trying start fragment from
public void starttransaction(){ fragment newfragment = new bookingfragment(); fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); transaction.add(r.id.pager, newfragment); transaction.addtobackstack(null); transaction.commit(); }
here's activity_main.xml
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="net.app.cairobus.cairobus.mainactivity"> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".mainactivity"> <android.support.v7.widget.toolbar android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:background="?attr/colorprimary" android:elevation="6dp" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar" app:popuptheme="@style/themeoverlay.appcompat.light" /> <android.support.design.widget.tablayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/app_bar" android:background="?attr/colorprimary" android:elevation="6dp" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar" /> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_below="@id/tab_layout" android:background="#fff" /> </linearlayout> <android.support.design.widget.navigationview android:id="@+id/main_drawer" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:background="#f2f2f2" app:headerlayout="@layout/drawer_header" app:itemicontint="@color/accent" app:itemtextcolor="@color/secondary_text" app:menu="@menu/menu_drawer" /> </android.support.v4.widget.drawerlayout>
and here's fragment i'm trying start
public class bookingfragment extends fragment { private static final string arg_param1 = "param1"; private static final string arg_param2 = "param2"; private string mparam1; private string mparam2; private onfragmentinteractionlistener mlistener; public static bookingfragment newinstance(string param1, string param2) { bookingfragment fragment = new bookingfragment(); bundle args = new bundle(); args.putstring(arg_param1, param1); args.putstring(arg_param2, param2); fragment.setarguments(args); return fragment; } public bookingfragment() {} @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { mparam1 = getarguments().getstring(arg_param1); mparam2 = getarguments().getstring(arg_param2); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_booking, container, false); } public void onbuttonpressed(uri uri) { if (mlistener != null) { mlistener.onfragmentinteraction(uri); } } @override public void onattach(activity activity) { super.onattach(activity); } @override public void ondetach() { super.ondetach(); mlistener = null; } public interface onfragmentinteractionlistener { // todo: update argument type , name public void onfragmentinteraction(uri uri); } }
you can't programmatically add fragment
viewpager
. that's job of fragmentpageradapter
.
so main activity has homefragment tab , bookingfragment tab. want navigate homefragment tripactivity, , navigate tripactivity bookingfragment.
to navigate homefragment tripactivity, use activity.startactivityforresult()
.
to navigate tripactivity bookingfragment, call activity.setresult()
activity.finish()
. in setresult()
, can set result code means "open bookingfragment" , set other parameters need in data intent.
in mainactivity, override onactivityresult()
. when receive result code bookingfragment, call setcurrentitem(page)
on viewpager
display bookingfragment.
if need set fields in bookingfragment, you'll need keep reference bookingfragment returned adapter.getitem()
can call setters, etc.
Comments
Post a Comment