android - Dynamically changing the fragments inside a fragment tab host? -


i have 1 main activity fragment activity here setting 2 tabs 2 fragments , b in b fragment have 1 button when user click on button want change fragment b fragment c. tabs above visible...

how can achieve replacing fragments inside tabs?

any solution appreciated.

basic concept- can achieve creating container. each tab assigned specific container. when need new fragment replace same using container.

kindly follow undermentioned code step step have better understanding. step-1: create tabs app. "home.java". contain code creating tabs using fragment.

    import android.os.bundle;     import android.support.v4.app.fragmentactivity;     import android.support.v4.app.fragmenttabhost;     import android.widget.textview;     import app.drugs.talksooner.container.gocontainerfragment;     import app.drugs.talksooner.container.learncontainerfragment;     import app.drugs.talksooner.container.morecontainerfragment;     import app.drugs.talksooner.container.talkcontainerfragment;     import app.drugs.talksooner.container.watchcontainerfragment;     import app.drugs.talksooner.utils.basecontainerfragment;      public class home extends fragmentactivity {          private static final string tab_1_tag = "tab_1";         private static final string tab_2_tag = "tab_2";         private static final string tab_3_tag = "tab_3";         private static final string tab_4_tag = "tab_4";         private static final string tab_5_tag = "tab_5";         private fragmenttabhost mtabhost;          @override         protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.home);             initview();         }          private void initview() {             mtabhost = (fragmenttabhost)findviewbyid(android.r.id.tabhost);             mtabhost.setup(this, getsupportfragmentmanager(), r.id.realtabcontent);             // mtabhost.addtab(mtabhost.newtabspec(tab_1_tag).setindicator("talk", getresources().getdrawable(r.drawable.ic_launcher)), talkcontainerfragment.class, null);             mtabhost.addtab(mtabhost.newtabspec(tab_1_tag).setindicator("talk"), talkcontainerfragment.class, null);             mtabhost.addtab(mtabhost.newtabspec(tab_2_tag).setindicator("learn"), learncontainerfragment.class, null);             mtabhost.addtab(mtabhost.newtabspec(tab_3_tag).setindicator("go"), gocontainerfragment.class, null);             mtabhost.addtab(mtabhost.newtabspec(tab_4_tag).setindicator("watch"), watchcontainerfragment.class, null);             mtabhost.addtab(mtabhost.newtabspec(tab_5_tag).setindicator("more"), morecontainerfragment.class, null);              /* increase tab height programatically               * tabs.gettabwidget().getchildat(1).getlayoutparams().height = 150;               */              (int = 0; < mtabhost.gettabwidget().getchildcount(); i++) {                 final textview tv = (textview) mtabhost.gettabwidget().getchildat(i).findviewbyid(android.r.id.title);                 if (tv == null)                 continue;                 else                 tv.settextsize(10);              }          }          @override         public void onbackpressed() {             boolean ispopfragment = false;             string currenttabtag = mtabhost.getcurrenttabtag();             if (currenttabtag.equals(tab_1_tag)) {                 ispopfragment = ((basecontainerfragment)getsupportfragmentmanager().findfragmentbytag(tab_1_tag)).popfragment();             } else if (currenttabtag.equals(tab_2_tag)) {                 ispopfragment = ((basecontainerfragment)getsupportfragmentmanager().findfragmentbytag(tab_2_tag)).popfragment();             } else if (currenttabtag.equals(tab_3_tag)) {                 ispopfragment = ((basecontainerfragment)getsupportfragmentmanager().findfragmentbytag(tab_3_tag)).popfragment();             } else if (currenttabtag.equals(tab_4_tag)) {                 ispopfragment = ((basecontainerfragment)getsupportfragmentmanager().findfragmentbytag(tab_4_tag)).popfragment();             } else if (currenttabtag.equals(tab_5_tag)) {                 ispopfragment = ((basecontainerfragment)getsupportfragmentmanager().findfragmentbytag(tab_5_tag)).popfragment();             }             if (!ispopfragment) {                 finish();             }         }       } 

your home.xml file

    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical" >           <framelayout             android:id="@+id/realtabcontent"             android:layout_width="match_parent"             android:layout_height="0dip"             android:layout_weight="1" />           <android.support.v4.app.fragmenttabhost             android:id="@android:id/tabhost"             android:layout_width="match_parent"             android:layout_height="wrap_content"             >              <framelayout                 android:id="@android:id/tabcontent"                 android:layout_width="0dip"                 android:layout_height="0dip"                 android:layout_weight="0" />          </android.support.v4.app.fragmenttabhost>      </linearlayout> 

step-2: define base container fragment helpful backtracking , replacment of fragments "check out replacefragement() ". our class "basecontainerfragment.java"

    import android.support.v4.app.fragment;     import android.support.v4.app.fragmenttransaction;     import android.util.log;     import app.drugs.talksooner.r;      public class basecontainerfragment extends fragment {          public void replacefragment(fragment fragment, boolean addtobackstack) {             fragmenttransaction transaction = getchildfragmentmanager().begintransaction();             if (addtobackstack) {                 transaction.addtobackstack(null);             }             transaction.replace(r.id.container_framelayout, fragment);             transaction.commit();             getchildfragmentmanager().executependingtransactions();         }          public boolean popfragment() {             log.e("test", "pop fragment: " + getchildfragmentmanager().getbackstackentrycount());             boolean ispop = false;             if (getchildfragmentmanager().getbackstackentrycount() > 0) {                 ispop = true;                 getchildfragmentmanager().popbackstack();             }             return ispop;         }      } 

step3: here considering 1 fragment hoping rest can handled in same fashion. defining container fragment class. each tab have specific container. talkcontainerfragment.java first tab

    import android.os.bundle;     import android.util.log;     import android.view.layoutinflater;     import android.view.view;     import android.view.viewgroup;     import app.drugs.talksooner.r;     import app.drugs.talksooner.talk;     import app.drugs.talksooner.utils.basecontainerfragment;      public class talkcontainerfragment extends basecontainerfragment {          private boolean misviewinited;          @override         public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {             log.e("test", "tab 1 oncreateview");             return inflater.inflate(r.layout.container_fragment, null);         }          @override         public void onactivitycreated(bundle savedinstancestate) {             super.onactivitycreated(savedinstancestate);             log.e("test", "tab 1 container on activity created");             if (!misviewinited) {                 misviewinited = true;                 initview();             }         }          private void initview() {             log.e("test", "tab 1 init view");             replacefragment(new talk(), false);         }      } 

it's xml file. "container_fragment.xml" xml container contains framelayout. use id replace different fragments.

    <?xml version="1.0" encoding="utf-8"?>     <framelayout xmlns:android="http://schemas.android.com/apk/res/android"         android:id="@+id/container_framelayout"         android:layout_width="match_parent"         android:layout_height="match_parent">       </framelayout> 

your main class. "talk.java"

    public class talk extends fragment {          /** define global variables on here */         //private progressdialog pdialog;         staticapilist sal;         talkmodelall tma;         jsonobject myjasonobject = null;         private listview lv;         private arraylist<talkmodelall> m_arraylist = null;         //arraylist<string> stringarraylist = new arraylist<string>();         talkarrayadapter taa;         set<string> uniquevalues = new hashset<string>();         textview rowtextview = null;         boolean vivek = false;          int postid;         string title;         string thumsrc;         string largeimg;         string excert;         string description;         string cat;         string myurl;         string jsonstring;         int mcurcheckposition;         string check_state = null;         string ccc;         linearlayout mylinearlayout;          @override         public view oncreateview(layoutinflater inflater, viewgroup container,                 bundle savedinstancestate) {              view rootview = inflater.inflate(r.layout.talk, container, false);              button btn = (button) rootview.findviewbyid(r.id.your_btn_id);             btn.setonclicklistener(new onclicklistener() {                  @override                 public void onclick(view v) {                     // todo auto-generated method stub //here talkdetail name of class needs open                     talkdetail fragment = new talkdetail();                     // if u need pass data                      bundle bundle = new bundle();                      bundle.putstring("title", m_arraylist.get(arg2).title);                     bundle.putstring("largeimg", m_arraylist.get(arg2).largeimg);                     bundle.putstring("excert", m_arraylist.get(arg2).excert);                     bundle.putstring("description", m_arraylist.get(arg2).description);                     bundle.putstring("cat", m_arraylist.get(arg2).cat);                     //bundle.putint("postid", m_arraylist.get(arg2).postid);                      fragment.setarguments(bundle);                     ((basecontainerfragment)getparentfragment()).replacefragment(fragment, true);                 }             });              return rootview;         }     } 

that's it. go. whole magic lies in calling r.id. instead of r.layout. cheers!


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -