java - How to use FragmentTransaction's add() method to set a Fragment in a RelativeLayout -
i'am using drawerlayout slide right left , accomplish task i'have created below's method :
xml code :
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:facebook="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" > <!-- framelayout display fragments --> <relativelayout android:id="@+id/mainview" android:layout_width="match_parent" android:layout_height="match_parent" > </relativelayout> <!-- listview display slider menu --> <relativelayout android:id="@+id/drawerview" android:layout_width="240dp" android:layout_height="wrap_content" android:layout_gravity="start" > <listview android:id="@+id/list_slidermenu" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/list_background" android:divider="@color/list_divider" android:dividerheight="1dp" /> </relativelayout>
java code :
public class profileactivity extends actionbaractivity { .... private drawerlayout mdrawerlayout; private actionbardrawertoggle mdrawertoggle; relativelayout drawerview; relativelayout mainview; .... @override protected void oncreate(bundle savedinstancestate) { ............. // .............// drawerview = (relativelayout) findviewbyid(r.id.drawerview); mainview = (relativelayout) findviewbyid(r.id.mainview); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.drawable.ic_drawer, r.string.app_name, r.string.app_name) { public void ondrawerclosed(view view) { supportinvalidateoptionsmenu(); } public void ondraweropened(view drawerview) { supportinvalidateoptionsmenu(); } @override public void ondrawerslide(view drawerview, float slideoffset) { super.ondrawerslide(drawerview, slideoffset); mainview.settranslationx(slideoffset * drawerview.getwidth()); mdrawerlayout.bringchildtofront(drawerview); mdrawerlayout.requestlayout(); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); } }
so want add fragment in relativelayout code .
any idea ?
fragmentmanager fm = getfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); view containerview = findviewbyid(r.id.mainview); ft.add(r.id.mainview, fragment); ft.commit(); fm.executependingtransactions();
you can call code in oncreate().
Comments
Post a Comment