android - Navigation Drawer does not open when I use ViewPager as Fragment -



had scenario navigationdrawer not popup when use viewpager fragment launched when click 1 of navigationdrawer items.i using material design both navigationdrawer , viewpager both fragments. code b

   package com.andr.viewpager;  import android.app.activity; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentstatepageradapter; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.support.v4.widget.drawerlayout; import android.support.v7.widget.toolbar; import android.view.layoutinflater; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.toast;  import com.andr.navdrawerdemo.fragmentdrawer; import com.andr.navdrawerdemo.friendsfragment; import com.andr.navdrawerdemo.homefragment; import com.andr.navdrawerdemo.messagesfragment; import com.andr.navdrawerdemo.r;  public class viewpagerfagment extends fragment{      private viewpager mpager;      private view view;      private pageradapter mpageradapter;      private static final int num_pages = 3;       @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         view =   inflater.inflate(r.layout.activity_screen_slide, container, false);          mpager = (viewpager)view.findviewbyid(r.id.pager);          mpager.setpagetransformer(true, new depthpagetransformer());         mpageradapter = new screenslidepageradapter(getactivity().getsupportfragmentmanager());         mpager.setadapter(mpageradapter);         return view;     }        /**          * simple pager adapter represents 5 screenslidepagefragment objects, in          * sequence.          */         private class screenslidepageradapter extends fragmentstatepageradapter {             public screenslidepageradapter(fragmentmanager fm) {                 super(fm);             }              @override             public fragment getitem(int position) {                  switch(position) {                   case 0: return new messagesfragment();                  case 1: return  new homefragment();                  case 2: return new friendsfragment();                  default: return new friendsfragment();                  }             }              @override             public int getcount() {                 return num_pages;             }         }          @override         public void onattach(activity activity) {             super.onattach(activity);         }          @override         public void ondetach() {             super.ondetach();         }          @override         public void onresume() {             super.onresume();         }   } 


navigationdrawer fragment:

    package com.andr.navdrawerdemo;   import android.content.context; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.view.gesturedetector; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.view.viewgroup;  import java.util.arraylist; import java.util.list;  import com.andr.adapter.navigationdraweradapter; import com.andr.model.navdraweritem; import com.andr.navdrawerdemo.r;    public class fragmentdrawer extends fragment {      private static string tag = fragmentdrawer.class.getsimplename();      private recyclerview recyclerview;     private actionbardrawertoggle mdrawertoggle;     private drawerlayout mdrawerlayout;     private com.andr.adapter.navigationdraweradapter adapter;     private view containerview;     private static string[] titles = null;     private fragmentdrawerlistener drawerlistener;      public fragmentdrawer() {      }      public void setdrawerlistener(fragmentdrawerlistener listener) {         this.drawerlistener = listener;     }      public static list<navdraweritem> getdata() {         list<navdraweritem> data = new arraylist<>();           // preparing navigation drawer items         (int = 0; < titles.length; i++) {             navdraweritem navitem = new navdraweritem();             navitem.settitle(titles[i]);             data.add(navitem);         }         return data;     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          // drawer labels         titles = getactivity().getresources().getstringarray(r.array.nav_drawer_labels);     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflating view layout         view layout = inflater.inflate(r.layout.fragment_navigation_drawer, container, false);         recyclerview = (recyclerview) layout.findviewbyid(r.id.drawerlist);          adapter = new navigationdraweradapter(getactivity(), getdata());         recyclerview.setadapter(adapter);         recyclerview.setlayoutmanager(new linearlayoutmanager(getactivity()));         recyclerview.addonitemtouchlistener(new recyclertouchlistener(getactivity(), recyclerview, new clicklistener() {             @override             public void onclick(view view, int position) {                 drawerlistener.ondraweritemselected(view, position);                 mdrawerlayout.closedrawer(containerview);             }              @override             public void onlongclick(view view, int position) {              }         }));          return layout;     }       public void setup(int fragmentid, drawerlayout drawerlayout, final toolbar toolbar) {         containerview = getactivity().findviewbyid(fragmentid);         mdrawerlayout = drawerlayout;         mdrawertoggle = new actionbardrawertoggle(getactivity(), drawerlayout, toolbar, r.string.drawer_open, r.string.drawer_close) {             @override             public void ondraweropened(view drawerview) {                 super.ondraweropened(drawerview);                 getactivity().invalidateoptionsmenu();             }              @override             public void ondrawerclosed(view drawerview) {                 super.ondrawerclosed(drawerview);                 getactivity().invalidateoptionsmenu();             }              @override             public void ondrawerslide(view drawerview, float slideoffset) {                 super.ondrawerslide(drawerview, slideoffset);                 toolbar.setalpha(1 - slideoffset / 2);             }         };          mdrawerlayout.setdrawerlistener(mdrawertoggle);         mdrawerlayout.post(new runnable() {             @override             public void run() {                 mdrawertoggle.syncstate();             }         });      }      public static interface clicklistener {         public void onclick(view view, int position);          public void onlongclick(view view, int position);     }      static class recyclertouchlistener implements recyclerview.onitemtouchlistener {          private gesturedetector gesturedetector;         private clicklistener clicklistener;          public recyclertouchlistener(context context, final recyclerview recyclerview, final clicklistener clicklistener) {             this.clicklistener = clicklistener;             gesturedetector = new gesturedetector(context, new gesturedetector.simpleongesturelistener() {                 @override                 public boolean onsingletapup(motionevent e) {                     return true;                 }                  @override                 public void onlongpress(motionevent e) {                     view child = recyclerview.findchildviewunder(e.getx(), e.gety());                     if (child != null && clicklistener != null) {                         clicklistener.onlongclick(child, recyclerview.getchildposition(child));                     }                 }             });         }          @override         public boolean onintercepttouchevent(recyclerview rv, motionevent e) {              view child = rv.findchildviewunder(e.getx(), e.gety());             if (child != null && clicklistener != null && gesturedetector.ontouchevent(e)) {                 clicklistener.onclick(child, rv.getchildposition(child));             }             return false;         }          @override         public void ontouchevent(recyclerview rv, motionevent e) {         }     }      public interface fragmentdrawerlistener {         public void ondraweritemselected(view view, int position);     } } 


my individual fragments:

    package com.andr.navdrawerdemo;  import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup;  import com.andr.navdrawerdemo.r;   public class friendsfragment extends fragment {       public friendsfragment() {         // required empty public constructor     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);      }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         return inflater.inflate(r.layout.fragment_friends, container, false);     } }   package com.andr.navdrawerdemo;   import android.app.activity; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup;  import com.andr.navdrawerdemo.r;   public class homefragment extends fragment {      public homefragment() {         // required empty public constructor     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);      }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         view rootview = inflater.inflate(r.layout.fragment_home, container, false);           // inflate layout fragment         return rootview;     }      @override     public void onattach(activity activity) {         super.onattach(activity);     }      @override     public void ondetach() {         super.ondetach();     } } 


package com.andr.navdrawerdemo;  import android.app.activity; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.toast;  import com.andr.navdrawerdemo.r;   public class messagesfragment extends fragment {       public messagesfragment() {         // required empty public constructor     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);      }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         return inflater.inflate(r.layout.fragment_messages, container, false);     }      @override     public void onattach(activity activity) {         // todo auto-generated method stub         super.onattach(activity);     }      @override     public void onresume() {         // todo auto-generated method stub         super.onresume();     } } 


mainactivity

package com.andr.navdrawerdemo;   import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbaractivity; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.toast;  import com.andr.navdrawerdemo.r; import com.andr.viewpager.viewpagerfagment;  //http://www.androidhive.info/2015/04/android-getting-started-with-material-design/ public class mainactivity extends appcompatactivity implements fragmentdrawer.fragmentdrawerlistener {      private static string tag = mainactivity.class.getsimplename();      private toolbar mtoolbar;     private fragmentdrawer drawerfragment;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         /* mtoolbar = (toolbar) findviewbyid(r.id.toolbar);          setsupportactionbar(mtoolbar);         getsupportactionbar().setdisplayshowhomeenabled(true);          drawerfragment = (fragmentdrawer)                 getsupportfragmentmanager().findfragmentbyid(r.id.fragment_navigation_drawer);         drawerfragment.setup(r.id.fragment_navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout), mtoolbar);         drawerfragment.setdrawerlistener(this);          // display first navigation drawer view on app launch         displayview(0);*/          initializeslidingmenu();     }      public void initializeslidingmenu(){            mtoolbar = (toolbar) findviewbyid(r.id.toolbar);             setsupportactionbar(mtoolbar);            getsupportactionbar().setdisplayshowhomeenabled(true);             drawerfragment = (fragmentdrawer)                    getsupportfragmentmanager().findfragmentbyid(r.id.fragment_navigation_drawer);            drawerfragment.setup(r.id.fragment_navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout), mtoolbar);            drawerfragment.setdrawerlistener(this);             // display first navigation drawer view on app launch            displayview(0);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          if(id == r.id.action_search){             toast.maketext(getapplicationcontext(), "search action selected!", toast.length_short).show();             return true;         }          return super.onoptionsitemselected(item);     }      @override     public void ondraweritemselected(view view, int position) {             displayview(position);     }      private void displayview(int position) {         fragment fragment = null;         string title = getstring(r.string.app_name);         switch (position) {             case 0:                 fragment = new homefragment();                 title = getstring(r.string.title_home);                 break;             case 1:                 fragment = new friendsfragment();                 title = getstring(r.string.title_friends);                 break;             case 2:                 fragment = new messagesfragment();                 title = getstring(r.string.title_messages);                 break;             case 3:                 fragment = new viewpagerfagment();                 title = getstring(r.string.title_slider);                 break;             default:                 break;         }          if (fragment != null) {             fragmentmanager fragmentmanager = getsupportfragmentmanager();             fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();             fragmenttransaction.replace(r.id.container_body, fragment);             fragmenttransaction.commit();              // set toolbar title             getsupportactionbar().settitle(title);         }     } } 


// layouts

<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:layout_width="match_parent"     android:layout_height="match_parent"      android:id="@+id/drawer_layout"     tools:context="com.andr.navdrawerdemo.mainactivity" >      <linearlayout         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_alignparenttop="true"         android:orientation="vertical">          <include             android:id="@+id/toolbar"             layout="@layout/toolbar" />     </linearlayout>      <framelayout             android:id="@+id/container_body"             android:layout_width="fill_parent"             android:layout_height="0dp"             android:layout_weight="1" />      <fragment         android:id="@+id/fragment_navigation_drawer"         android:name="com.andr.navdrawerdemo.fragmentdrawer"         android:layout_width="@dimen/nav_drawer_width"         android:layout_height="match_parent"         android:layout_gravity="start"         app:layout="@layout/fragment_navigation_drawer"         tools:layout="@layout/fragment_navigation_drawer" />  </android.support.v4.widget.drawerlayout> 


viewpager layout

<android.support.v4.view.viewpager     xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/pager"     android:layout_width="match_parent"     android:layout_height="match_parent" /> 

can me y slidingmenu n't popup in viewpager fragment

i believe viewpager should inside main layout. i'm still struggling making work 100%, doing:

<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="thehighestprimenumber.com.losprincipios.chapteractivity">   <android.support.v4.view.viewpager     android:id="@+id/pager"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <framelayout android:id="@+id/containerbox" android:layout_width="match_parent"         android:layout_height="match_parent" /> </android.support.v4.view.viewpager>  <listview android:id="@+id/drawer"     ...     />  </android.support.v4.widget.drawerlayout> 

this way navigationdrawer opens.

my problem is, when click on item on navigation drawer, fragment doesn't update.

if know how solve this, you'd make day =)


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -