android - How to toggle between fragments -




i have app has 1 main activity , 5 main fragments. when mainactivity created create list containing each of 5 fragments. user presented tab bar on bottom of screen he/she can use navigate between fragments. how set when user selects tab, corresponding fragment shown without creating new instance of it? want change view on screen created fragment.

i using bottombar https://github.com/roughike/bottombar calls "ontabselected" interface method when tab pressed.

you can use 5 fragments library specified this. layout file should this

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto">  <!-- fragment container, or --> <framelayout     android:id="@+id/contentcontainer"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_above="@+id/bottombar" />  <com.roughike.bottombar.bottombar     android:id="@+id/bottombar"     android:layout_width="match_parent"     android:layout_height="60dp"     android:layout_alignparentbottom="true"     app:bb_tabxmlresource="@xml/bottombar_tabs" />  </relativelayout> 

the containing activity class replace framelayout fragment depending on fragment selected bottombar view. simple example

public class main3activity extends appcompatactivity {  private fragment fragment; private fragmentmanager fragmentmanager;  @override protected void oncreate(@nullable bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_three_tabs);      bottombar bottombar = (bottombar) findviewbyid(r.id.bottombar);     bottombar.setontabselectlistener(new ontabselectlistener() {         @override         public void ontabselected(@idres int tabid) {             if(tabid == r.id.tab_home){                 fragment = new homefragment();             }             if(tabid == r.id.tab_favorite){                 fragment = new favoritefragment();             }         }         fragmenttransaction ft = getsupportfragmentmanager().begintransaction();         ft.replace(r.id.contentcontainer, fragment).commit();     });      bottombar.setontabreselectlistener(new ontabreselectlistener() {         @override         public void ontabreselected(@idres int tabid) {             toast.maketext(getapplicationcontext(), tabmessage.get(tabid, true), toast.length_long).show();         }     }); }  } 

then can create individual fragment content below

public class favoritefragment extends fragment {     public favoritefragment() {    }     @override    public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {        return inflater.inflate(r.layout.fragment_favorite, container, false     );   } } 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -