android - Navigation drawer default fragment -
i novice developer , i´m integrating navigation drawer in app android-support v7 , have 1 question. when start app main layout this:
<?xml version="1.0" encoding="utf-8"?>
<!-- main content view --> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- navigation drawer --> <listview android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/white" android:choicemode="singlechoice" android:divider="@android:color/transparent" android:dividerheight="0dp" />
and main activity:
drawerlist.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { fragment fragment = null; switch (position) { case 0: fragment = new fragment1(); break; case 1: fragment = new fragment2(); break; case 2: fragment = new fragment3(); break; case 3: fragment = new fragment4(); break; } fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.content_frame, fragment) .commit(); drawerlist.setitemchecked(position, true); tituloseccion = opcionesmenu[position]; getsupportactionbar().settitle(tituloseccion); drawerlayout.closedrawer(drawerlist); } });
how can set default fragment main layout of app? thank you
if ok load default fragment every time activity created, can put fragmenttransaction
in oncreate()
looks this:
@override public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); fragmenttransaction tx = getsupportfragmentmanager().begintransaction(); tx.replace(r.id.content_frame, new fragment1()); tx.commit(); }
if want more sophisticated way of doing (for example switching different fragment when go main activity), can use intent
extras determining fragment in oncreate()
, put default fragment in defaultvalue
upon loading extra:
int position = getintent().getintextra("position", 1); switch(position){ ... }
Comments
Post a Comment