eclipse - How to access controls(views)in tabs from another one in android? -
i'm working on application in eclipse have 5 tab , have spent time have big problem :
when 1 of tabs activated next tab , prev tab loading ,
oncreated()
method called don't need need callingoncreated()
method when switching tabs
this project files
mainactivity.java
package info.androidhive.tabsswipe; import info.androidhive.tabsswipe.adapter.tabspageradapter; import ir.zinoo.mankan.r; import android.app.actionbar; import android.app.actionbar.tab; import android.app.fragmenttransaction; import android.graphics.color; import android.graphics.drawable.colordrawable; import android.view.menu; import android.view.menuitem; import android.widget.toast; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v4.view.viewpager; import android.content.sharedpreferences; public class mainactivity extends fragmentactivity implements actionbar.tablistener { private viewpager viewpager; private tabspageradapter madapter; private actionbar actionbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // initilization viewpager = (viewpager) findviewbyid(r.id.pager); actionbar = getactionbar(); madapter = new tabspageradapter(getsupportfragmentmanager()); viewpager.setadapter(madapter); actionbar.sethomebuttonenabled(false); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); //adding tabs (string tab_name : tabs) { actionbar.addtab(actionbar.newtab().settablistener(this)); } actionbar.setstackedbackgrounddrawable(new colordrawable(color.parsecolor("#333333"))); actionbar.setbackgrounddrawable(new colordrawable(color.parsecolor("#333333"))); viewpager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpageselected(int position) { // on changing page // make respected tab selected actionbar.setselectednavigationitem(position); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } } @override public void onpagescrollstatechanged(int arg0) { } }); } @override public void ontabreselected(tab tab, fragmenttransaction ft) { } @override public void ontabselected(tab tab, fragmenttransaction ft) { // on tab selected // show respected fragment view viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tab tab, fragmenttransaction ft) { } 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(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
tabpageradapter.java class
package info.androidhive.tabsswipe.adapter; import info.androidhive.tabsswipe.bmifragment; import info.androidhive.tabsswipe.calorifragment; import info.androidhive.tabsswipe.fatfragment; import info.androidhive.tabsswipe.kamarfragment; import info.androidhive.tabsswipe.ostokhanfragment; import info.androidhive.tabsswipe.otherfragment; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; public class tabspageradapter extends fragmentpageradapter { public tabspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int index) { switch (index) { case 0: // top rated fragment activity return new bmifragment(); case 1: // games fragment activity return new calorifragment(); case 2: // movies fragment activity return new kamarfragment(); case 3: // movies fragment activity return new ostokhanfragment(); case 4: // movies fragment activity return new fatfragment(); case 5: // movies fragment activity return new otherfragment(); } return null; } @override public int getcount() { //get item count - equal number of tabs return 6; } }
now question how avoid calling on created method of other tabs when in switch special tab?
from android documentation fragmentpageradapter
:
this version of pager best use when there handful of typically more static fragments paged through, such set of tabs. fragment of each page user visits kept in memory, though view hierarchy may destroyed when not visible. can result in using significant amount of memory since fragment instances can hold on arbitrary amount of state. larger sets of pages, consider fragmentstatepageradapter
fragmentstatepageradapter :
this version of pager more useful when there large number of pages, working more list view. when pages not visible user, entire fragment may destroyed, keeping saved state of fragment. allows pager hold on less memory associated each visited page compared fragmentpageradapter @ cost of potentially more overhead when switching between pages.
so, using fragmentpageradapter
, frgmentmanager add fragment in memory while user using app. furthermore, when user switches page, fragmentmanager loads previous
, next
page , call oncreate()
of tabs.
as have 6 tabs, may want add more tabs, fragmentstatepageradapter
more suitable.
for checking fragment visible user, can :
public class tabspageradapter extends fragmentpageradapter { /* declare fragment objects here globally */ bmifragment mbmi = new bmifragment(); calorifragment mcalori = new calorifragment(); kamarfragment mkamar = new kamarfragment(); public tabspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int index) { switch (index) { case 0: // top rated fragment activity mbmi.setuservisiblehint(false); return mbmi; case 1: // games fragment activity mcalori.setuservisiblehint(false); return mcalori; case 2: // movies fragment activity mkamar.setuservisiblehint(false); return mkamar; /* case*/ } return null; } }
set onpagechangelistener viewpager activity :
mpager.setonpagechangelistener(new onpagechangelistener() { @override public void onpageselected(int position) { // position tab visible user switch(position) { case 0 : mbmi.setuservisiblehint(true); break; /* case */ } } });
override setuservisiblehint() method in fragments :
public class <your_fragment> extends fragment { @override public void setuservisiblehint(boolean isvisibletouser) { super.setuservisiblehint(isvisibletouser); if (isvisibletouser) { // something, fragment visible } else { // fragment loaded, not visible user } } }
Comments
Post a Comment