android - how to hide fragment(in FrameLayout) and show viewpager fragment? -


i having activity contains viewpager,tablayout , navigationdrawer. navigation drawer contains items(say 10). got framelayout load fragments when navigation drawer items clicked.

here in viewpager got 3 fragments(say breakfast,lunch,dinner). in navigation drawer list items got same items in viewpager.

now when click first 3 items viewpager fragments works fine.after clicking fourth item in drawer(i.e check calories) check calories fragment of framelayout loaded successfully.now when click breakfast,lunch or dinner item,the viewpager fragment not loading replacing framelayout fragment. come solution.

here code

public class homescreen extends appcompatactivity {

tablayout tablayout; viewpager viewpager; relativelayout menunavigation, menuimagelayout; drawerlayout mdrawerlayout; navigationlistitemadapter navigationlistitemadapter; listview mdrawerlist; imageview homepagemenu, locationimage; de.hdodenhof.circleimageview.circleimageview profileimage; button checkout; textview title; floatingactionbutton filteractionbutton; framelayout fragmentcontainer; fragment fragment = null; boolean doublebacktoexitpressedonce = false;  int imagearray[] = {r.raw.break_fast_white, r.raw.lunch_white, r.raw.dinner_white, r.raw.calories_white, r.raw.profile_white,         r.raw.about_us_white, r.raw.contact_us_white, r.raw.help_white, r.raw.policy_info_white, r.raw.logout_color}; string titlearray[] = {"break fast", "lunch", "dinner", "check calories", "my profile", "about us", "contact us", "help", "privacy info", "logout"};  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activty_home_screen);     statusbartheme.setstatusbarcolor(this);     initiateview();     fontstyle();      try {          lunchboxutil.setimagedrawable(this, r.raw.menu, homepagemenu);         lunchboxutil.setimagedrawable(this, r.raw.filter, filteractionbutton);         lunchboxutil.setimagedrawable(this, r.raw.location, locationimage);         lunchboxutil.setimagedrawable(this, r.raw.photo_circle, profileimage);         title.settext(r.string.app_name);          setupviewpager(viewpager);         tablayout.setupwithviewpager(viewpager);         navigationlistitemadapter = new navigationlistitemadapter(getapplicationcontext(), titlearray, imagearray);         mdrawerlist.setadapter(navigationlistitemadapter);          menuimagelayout.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 boolean draweropen = mdrawerlayout.isdraweropen(menunavigation);                 if (!draweropen) {                     mdrawerlayout.opendrawer(menunavigation);                 } else {                     mdrawerlayout.closedrawer(menunavigation);                 }             }         });          filteractionbutton.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 intent intent = new intent(homescreen.this, filterbycategory.class);                 startactivity(intent);                 //overridependingtransition(r.anim.expand_in, r.anim.demo_slide_out);             }         });          mdrawerlist.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view view, int position, long id) {                 displayfragments(position);             }         });          checkout.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 intent intent = new intent(homescreen.this, demoscreenmain.class);                 startactivity(intent);                 overridependingtransition(r.anim.demo_slide_in, r.anim.demo_slide_out);             }         });          if (savedinstancestate == null) {             fragmentcontainer = new framelayout(this);         }      } catch (exception e) {         e.printstacktrace();     } }  @override public void onbackpressed() {      if (mdrawerlayout.isdraweropen(menunavigation)) {         mdrawerlayout.closedrawer(menunavigation);     } else {          if (doublebacktoexitpressedonce) {             super.onbackpressed();             return;         }          this.doublebacktoexitpressedonce = true;         toast.maketext(this, "press again exit", toast.length_short).show();          new handler().postdelayed(new runnable() {              @override             public void run() {                 doublebacktoexitpressedonce = false;              }         }, 2000);     } }  private void displayfragments(int position) {     switch (position) {          case 0:             mdrawerlayout.closedrawer(menunavigation);             fragmentcontainer.setvisibility(view.gone);             viewpager.setcurrentitem(0);             break;          case 1:             mdrawerlayout.closedrawer(menunavigation);             fragmentcontainer.setvisibility(view.gone);             viewpager.setcurrentitem(1);             break;          case 2:             mdrawerlayout.closedrawer(menunavigation);             fragmentcontainer.setvisibility(view.gone);             viewpager.setcurrentitem(2);             break;          case 3:             mdrawerlayout.closedrawer(menunavigation);             fragment = new calculatecalories();             title.settext(r.string.calories);             filteractionbutton.setvisibility(view.gone);             break;          case 4:             mdrawerlayout.closedrawer(menunavigation);             intent intent = new intent(this, userprofile.class);             startactivity(intent);             break;          case 5:             mdrawerlayout.closedrawer(menunavigation);             fragment = new addressdetails();             title.settext(r.string.address_details);             filteractionbutton.setvisibility(view.gone);             break;          case 6:             mdrawerlayout.closedrawer(menunavigation);             fragment = new contactus();             title.settext(r.string.contactus);             filteractionbutton.setvisibility(view.gone);             break;     }      if (fragment != null) {         fragmentmanager fragmentmanager = getsupportfragmentmanager();         fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();         fragmenttransaction.replace(r.id.frame_container_fragment, fragment);         fragmenttransaction.commit();     } }  private void initiateview() {     mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout);     menunavigation = (relativelayout) findviewbyid(r.id.relative_navigtion);     menuimagelayout = (relativelayout) findviewbyid(r.id.menu_image_rl);     homepagemenu = (imageview) findviewbyid(r.id.home_menu_img);     locationimage = (imageview) findviewbyid(r.id.user_location_img);     profileimage = (de.hdodenhof.circleimageview.circleimageview) findviewbyid(r.id.profile_picture);     filteractionbutton = (floatingactionbutton) findviewbyid(r.id.fab);     fragmentcontainer = (framelayout) findviewbyid(r.id.frame_container_fragment);     mdrawerlist = (listview) findviewbyid(r.id.drawer_menu_items_list);     title = (textview) findviewbyid(r.id.home_title_txt);     viewpager = (viewpager) findviewbyid(r.id.viewpager);     tablayout = (tablayout) findviewbyid(r.id.tabs);     checkout = (button) findviewbyid(r.id.checkout_btn); }  private void fontstyle() {      typeface robotoregular = fontutility.getrobotoregular(this);     typeface robotomedium = fontutility.getrobotomedium(this);      title.settypeface(robotomedium);     checkout.settypeface(robotoregular); }  private void setupviewpager(viewpager viewpager) {     viewpageradapter adapter = new viewpageradapter(getsupportfragmentmanager());     adapter.addfragment(new breakfastfragment(), "breakfast");     adapter.addfragment(new lunchfragment(), "lunch");     adapter.addfragment(new dinnerfragment(), "dinner");     viewpager.setadapter(adapter); }  class viewpageradapter extends fragmentpageradapter {     private final list<fragment> mfragmentlist = new arraylist<>();     private final list<string> mfragmenttitlelist = new arraylist<>();      public viewpageradapter(fragmentmanager manager) {         super(manager);     }      @override     public fragment getitem(int position) {         return mfragmentlist.get(position);     }      @override     public int getcount() {         return mfragmentlist.size();     }      public void addfragment(fragment fragment, string title) {         mfragmentlist.add(fragment);         mfragmenttitlelist.add(title);     }      @override     public charsequence getpagetitle(int position) {         return mfragmenttitlelist.get(position);     } } 

}

here xml layout code

<android.support.design.widget.coordinatorlayout     android:id="@+id/coordinatorlayout"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <relativelayout         android:id="@+id/main_lay"         android:layout_width="match_parent"         android:layout_height="match_parent">          <include             android:id="@+id/action_bar"             layout="@layout/tool_bar_layout" />          <android.support.design.widget.tablayout             android:id="@+id/tabs"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_below="@+id/action_bar"             android:background="?attr/colorprimary"             android:minheight="?attr/actionbarsize"             android:theme="@style/themeoverlay.appcompat.dark.actionbar"             app:tabtextappearance="@style/mycustomtextappearance" />          <android.support.v4.view.viewpager             android:id="@+id/viewpager"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:layout_above="@+id/checkout_btn"             android:layout_below="@+id/tabs" />          <button             android:id="@+id/checkout_btn"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_alignparentbottom="true"             android:background="@color/standard_brown"             android:text="checkout"             android:textcolor="@color/coloraccent"             android:textsize="@dimen/text_size_18sp" />      </relativelayout>      <framelayout         android:id="@+id/frame_container_fragment"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_margintop="?actionbarsize" />      <android.support.design.widget.floatingactionbutton         android:id="@+id/fab"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="bottom|right"         android:layout_marginbottom="100dp"         android:layout_marginright="20dp"         android:clickable="true"         android:visibility="visible"         app:backgroundtint="@color/standard_brown"         app:fabsize="normal" />  </android.support.design.widget.coordinatorlayout>  <relativelayout     android:id="@+id/relative_navigtion"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_gravity="start"     android:clickable="false">      <include         android:id="@+id/navgation_drawer"         layout="@layout/navigation_drawer_layout" />  </relativelayout> 


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)