java - ActionBarSherlock fragment showing different data than passed -
hello , thank trying help!
i'm trying build app actionbarsherlcok. when click tabs, same fragment instantiate dynamic data based on tab clicked. reason it keeps showing me wrong data, though correct parameter value passed (i verified using breakpoints , watches).
i've read simpleonpagechangelistener, getcurrentitem , of course fragments tutorial. relied on swipeytabs example create this, , built short demo show problem here:
this mainactivity.java
package il.co.gilead.testdynamicfragments; import com.actionbarsherlock.app.actionbar; import com.actionbarsherlock.app.sherlockfragmentactivity; import android.os.bundle; import android.support.v4.view.viewpager; public class mainactivity extends sherlockfragmentactivity { public static viewpager mviewpager; private tabsadapter mtabsadapter; integer intnumofpages = 3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mviewpager = new viewpager(this); mviewpager.setid(r.id.pager); setcontentview(mviewpager); final actionbar bar = getsupportactionbar(); bar.setnavigationmode(actionbar.navigation_mode_tabs); mtabsadapter = new tabsadapter(this, mviewpager); (int i=1; i<=intnumofpages; i++) { mtabsadapter.addtab(bar.newtab().settext("fragment "+i), testfrag.class, null); } } }
this testfrag.java
package il.co.gilead.testdynamicfragments; import com.actionbarsherlock.app.sherlockfragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class testfrag extends sherlockfragment { public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.frag_test, container, false); textview tv = (textview) v.findviewbyid(r.id.textview1); integer pos = (mainactivity.mviewpager.getcurrentitem() + 1); tv.settext("page " + pos.tostring()); return v; } }
my activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <android.support.v4.view.viewpager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pager" /> </relativelayout>
and test_frag.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview" /> </linearlayout>
finally tabsadapter
public class tabsadapter extends fragmentpageradapter implements actionbar.tablistener, viewpager.onpagechangelistener{ private final context mcontext; private final actionbar mactionbar; private final viewpager mviewpager; private final arraylist<tabinfo> mtabs = new arraylist<tabinfo>(); static final class tabinfo{ private final class<?> clss; private final bundle args; tabinfo(class<?> _class, bundle _args){ clss = _class; args = _args; } } public tabsadapter(sherlockfragmentactivity fa, viewpager pager) { super(fa.getsupportfragmentmanager()); mcontext = fa; mactionbar = fa.getsupportactionbar(); mviewpager = pager; mviewpager.setadapter(this); mviewpager.setonpagechangelistener(this); } public void addtab(actionbar.tab tab, class<?> clss, bundle args){ tabinfo info = new tabinfo(clss, args); tab.settag(info); tab.settablistener(this); mtabs.add(info); mactionbar.addtab(tab); notifydatasetchanged(); } @override public void onpagescrollstatechanged(int state) { } @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } @override public void onpageselected(int position) { mactionbar.setselectednavigationitem(position); } @override public void ontabselected(tab tab, fragmenttransaction ft) { mviewpager.setcurrentitem(tab.getposition()); object tag = tab.gettag(); (int = 0; i<mtabs.size(); i++){ if (mtabs.get(i) == tag){ mviewpager.setcurrentitem(i); } } } @override public void ontabunselected(tab tab, fragmenttransaction ft) { } @override public void ontabreselected(tab tab, fragmenttransaction ft) { } @override public fragment getitem(int position) { tabinfo info = mtabs.get(position); return fragment.instantiate(mcontext, info.clss.getname(), info.args); } @override public int getcount() { return mtabs.size(); } }
if you'll play around it, you'll see clicking tab "fragment 2" shows "page 1" , shows "page 3" , shows "page 2". think has pre-loading fragments, or fragment refresh, @ point clueless...
thanks again help!
try change this:
integer pos = (mainactivity.mviewpager.getcurrentitem() + 1);
to this:
integer pos = (getactivity().mviewpager.getcurrentitem() + 1);
Comments
Post a Comment