android - How to display Fragment in a tab Activity -


i'm facing quite frustrating bug in app, let me explain: have mainactivity appbar containing 3 tabs , each tab displays fragment. problem comes when try lunch fragment , make replace screen. new fragment displayed on replaced 1 , 2 visible.

here code:

the mainactivity:  public class mainactivity extends appcompatactivity {  public static model model; public static controller controller;  private sectionspageradapter msectionspageradapter; private viewpager mviewpager;  private static final int permission_galerie = 10;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main_layout);      this.model = new model();     this.controller = new controller(model,this);      final toolbar toolbar = (toolbar)findviewbyid(r.id.toolbar);     setsupportactionbar(toolbar);     msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager());     mviewpager = (viewpager)findviewbyid(r.id.container);     mviewpager.setadapter(msectionspageradapter);      tablayout tablayout = (tablayout)findviewbyid(r.id.tabs);     tablayout.setupwithviewpager(mviewpager); }  @override public boolean oncreateoptionsmenu(android.view.menu menu) {     getmenuinflater().inflate(r.menu.settings,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.db_setting) {         getfragmentmanager().begintransaction().replace(r.id.mainlayout, new dbsettingfragment()).commit();     }      return super.onoptionsitemselected(item); }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);    }  @override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) {     if(requestcode==permission_galerie){         if(grantresults.length==1 && grantresults[0]== packagemanager.permission_granted){             snackbar.make(getcurrentfocus(),"permission accordée",snackbar.length_short);         }     }     else{         super.onrequestpermissionsresult(requestcode, permissions, grantresults);     } }  public class sectionspageradapter extends fragmentpageradapter {      public sectionspageradapter(fragmentmanager fm) {         super(fm);     }      @override     public android.support.v4.app.fragment getitem(int position) {         fragment menusmanagementfragment = new menusmanagementfragment();         switch (position) {             case 0:                 return menusmanagementfragment;             case 1:                 ordersmanagementfragment ordersmanagementfragment = new ordersmanagementfragment();                 return ordersmanagementfragment;             case 2:                 deliversmanagementfragment deliversmanagementfragment = new deliversmanagementfragment();                 return deliversmanagementfragment;              default: return menusmanagementfragment;         }      }      @override     public int getcount() {         return 3;     }      @override     public charsequence getpagetitle(int position) {         switch (position) {             case 0:                 return "gestion des menus";             case 1:                 return "gestion des commandes";             case 2:                 return "gestiond des livreurs";         }         return null;     } }} 

the main_layout:

<android.support.design.widget.coordinatorlayout 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:id="@+id/mainlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true">  <android.support.design.widget.appbarlayout     android:id="@+id/appbar"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:paddingtop="16dp"     android:theme="@style/apptheme.appbaroverlay">      <android.support.v7.widget.toolbar         android:id="@+id/toolbar"         android:layout_width="match_parent"         android:layout_height="?attr/actionbarsize"         android:background="?attr/colorprimary"         app:popuptheme="@style/apptheme.popupoverlay">      </android.support.v7.widget.toolbar>      <android.support.design.widget.tablayout         android:id="@+id/tabs"         android:layout_width="match_parent"         android:layout_height="wrap_content" />  </android.support.design.widget.appbarlayout>  <android.support.v4.view.viewpager     android:id="@+id/container"     android:layout_width="match_parent"     android:layout_height="match_parent"     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

and in deliversmanagementfragment do:

deliverstatsbutton.setonclicklistener(new view.onclicklistener() {                     @override                     public void onclick(view view) {                         getfragmentmanager().begintransaction().replace(r.id.mainlayout,new deliverstatsfragment()).commit();                     }                 }); 

if kind. thanks.

you don't need replace fragment

getfragmentmanager().begintransaction().replace(r.id.mainlayout,new deliverstatsfragment()).commit(); 

you need use viewpager.setcurrentitem() changing position

or

make deliverstatsfragment background color white , set clickable="true"


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -