java - How to implement ListFragment and NavigationDrawerFragment -


i want build android application fragment contains list of option , if click on 1 of option, can see, fragment other option.

so build this:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/drawer_layout"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".activity.mainactivity"     tools:ignore="mergerootframe" >      <framelayout         android:id="@+id/container"         android:layout_width="match_parent"         android:layout_height="match_parent" />      <linearlayout         android:layout_width="480dp"         android:layout_height="match_parent"         android:layout_gravity="start"         tools:layout="@layout/fragment_navigation_drawer" >          <listview             android:id="@+id/left_list"             android:layout_width="240dp"             android:layout_height="match_parent"             android:background="#6dc659"             android:foreground="@color/white">         </listview>          <fragment             android:id="@+id/navigation_drawer"             android:name="it.eresult.decipher.fragment.navigationdrawerfragment"             android:layout_width="240dp"             android:layout_height="match_parent"             android:layout_gravity="start"             tools:layout="@layout/fragment_navigation_drawer" />      </linearlayout> </android.support.v4.widget.drawerlayout> 

this mainactivity.java

public class mainactivity extends actionbaractivity implements navigationdrawerfragment.navigationdrawercallbacks{      private drawerlayout drawer;     private listview leftlist;     private string[] leftliststrings;     private string[] rightliststrings;     private navigationdrawerfragment mnavigationdrawerfragment;       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);           drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);         leftlist = (listview) findviewbyid(r.id.left_list);          leftliststrings = getresources().getstringarray(r.array.principal_menu);          leftlist.setadapter(new arrayadapter<string>(this,android.r.layout.simple_list_item_1,leftliststrings));          mnavigationdrawerfragment = (navigationdrawerfragment)                 getsupportfragmentmanager().findfragmentbyid(r.id.navigation_drawer);          mnavigationdrawerfragment.setup(                 r.id.navigation_drawer,                 (drawerlayout) findviewbyid(r.id.drawer_layout));          leftlist.setonitemclicklistener(new onitemclicklistener() {              @override             public void onitemclick(adapterview<?> arg0, view arg1, int arg2,                                     long arg3) {                   arg1.setbackgroundcolor(getresources().getcolor(r.color.selected_menu_option));                  if(arg2 == 0){                   }              }          });          drawer.setdrawerlistener(new drawerlayout.drawerlistener() {              @override             public void ondrawerstatechanged(int arg0) {                 // todo auto-generated method stub              }              @override             public void ondrawerslide(view arg0, float arg1) {                 // todo auto-generated method stub              }              @override             public void ondraweropened(view arg0) {                 // todo auto-generated method stub              }              @override             public void ondrawerclosed(view arg0) {                 // todo auto-generated method stub                 //rightlist.setvisibility(view.invisible);             }         });     }      @override     public void onnavigationdraweritemselected(int position) {         //todo      }      class mayadapter extends arrayadapter<string>{          list<string> mylist = null;         public mayadapter(context context, int resource, list<string> objects) {             super(context, resource,  objects);             mylist = objects;         }         public list<string> getmylist() {             return mylist;         }         public void setmylist(list<string> mylist) {             this.mylist = mylist;         }     }      public void restoreactionbar() {         actionbar actionbar = getsupportactionbar();         actionbar.setnavigationmode(actionbar.navigation_mode_standard);         actionbar.setdisplayshowtitleenabled(true);         actionbar.settitle("omnia");     }      @override     public boolean oncreateoptionsmenu(menu menu) {     return super.oncreateoptionsmenu(menu);     }      @override     public boolean onoptionsitemselected(menuitem item) {         int id = item.getitemid();         return super.onoptionsitemselected(item);     } } 

this navigationdrawerfragment.java

public class navigationdrawerfragment extends fragment {       private static final string state_selected_position = "selected_navigation_drawer_position";     private navigationdrawercallbacks mcallbacks;     private actionbardrawertoggle mdrawertoggle;      private drawerlayout mdrawerlayout;     private listview mdrawerlistview;     private view mfragmentcontainerview;     private int mcurrentselectedposition = 0;     toolbar toolbar;     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          if (savedinstancestate != null) {             mcurrentselectedposition = savedinstancestate.getint(state_selected_position);         }           // select default item.         selectitem(mcurrentselectedposition);      }      @override     public void onactivitycreated(bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);          // fragment has menu items contribute         sethasoptionsmenu(true);     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         mdrawerlistview = (listview) inflater.inflate(                 r.layout.fragment_navigation_drawer, container, false);         mdrawerlistview.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview parent, view view, int position, long id) {                 selectitem(position);             }         });         mdrawerlistview.setadapter(new arrayadapter(                 getactionbar().getthemedcontext(),                 android.r.layout.simple_list_item_activated_1,                 android.r.id.text1,                 new string[]{                         getstring(r.string.social_history),                         getstring(r.string.encounter),                         getstring(r.string.problems),                 }));         mdrawerlistview.setitemchecked(mcurrentselectedposition, true);         return mdrawerlistview;     }      private actionbar getactionbar() {         return ((appcompatactivity) getactivity()).getsupportactionbar();     }      public boolean isdraweropen() {         return mdrawerlayout != null && mdrawerlayout.isdraweropen(mfragmentcontainerview);     }       public void setup(int fragmentid, drawerlayout drawerlayout) {         mfragmentcontainerview = getactivity().findviewbyid(fragmentid);         mdrawerlayout = drawerlayout;          // set custom shadow overlays main content when drawer opens         //mdrawerlayout.setdrawershadow(r.drawable.drawer_shadow, gravitycompat.start);          // set drawer's list view items , click listener         actionbar actionbar = getactionbar();         actionbar.setdisplayhomeasupenabled(true);         actionbar.sethomebuttonenabled(true);          // actionbardrawertoggle ties the proper interactions         // between navigation drawer , action bar app icon.         mdrawertoggle = new actionbardrawertoggle(                 getactivity(),                 mdrawerlayout,                 null,                 r.string.navigation_drawer_open,                 r.string.navigation_drawer_close         ) {             @override             public void ondrawerclosed(view drawerview) {                 super.ondrawerclosed(drawerview);                 if (!isadded()) {                     return;                 }                 getactivity().supportinvalidateoptionsmenu();             }              @override             public void ondraweropened(view drawerview) {                 super.ondraweropened(drawerview);                 if (!isadded()) {                     return;                 }                 getactivity().supportinvalidateoptionsmenu();             }         };           mdrawerlayout.post(new runnable() {             @override             public void run() {                 mdrawertoggle.syncstate();             }         });         mdrawerlayout.setdrawerlistener(mdrawertoggle);         mdrawertoggle.setdrawerindicatorenabled(true);     }      private void selectitem(int position) {         mcurrentselectedposition = position;         if (mdrawerlistview != null) {             mdrawerlistview.setitemchecked(position, true);         }         if (mdrawerlayout != null) {             mdrawerlayout.closedrawer(mfragmentcontainerview);         }         if (mcallbacks != null) {             mcallbacks.onnavigationdraweritemselected(position);         }     }      @override     public void onattach(activity activity) {         super.onattach(activity);         try {             mcallbacks = (navigationdrawercallbacks) activity;         } catch (classcastexception e) {             throw new classcastexception("activity must implement navigationdrawercallbacks.");         }     }      @override     public void ondetach() {         super.ondetach();         mcallbacks = null;     }      @override     public void onsaveinstancestate(bundle outstate) {         super.onsaveinstancestate(outstate);         outstate.putint(state_selected_position, mcurrentselectedposition);     }      @override     public void onconfigurationchanged(configuration newconfig) {         super.onconfigurationchanged(newconfig);         mdrawertoggle.onconfigurationchanged(newconfig);     }      @override     public boolean onoptionsitemselected(menuitem item) {         if (mdrawertoggle.onoptionsitemselected(item)) {             return true;         }         return super.onoptionsitemselected(item);     }      public static interface navigationdrawercallbacks {         void onnavigationdraweritemselected(int position);     } } 

with code, if try run application, can see complete menu this

enter image description here

instead, want see left menu, , if click on 1 items, see right menu


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -