Wednesday, 15 September 2010

android - FragmentStatePagerAdapter.getItem() returns wrong position -



android - FragmentStatePagerAdapter.getItem() returns wrong position -

i implementing fragmentstatepageradapter works fine, getitem(int position) not have right value time.

public class eventpageradapter extends fragmentstatepageradapter { private static string log = "eventpager"; private calendar currentdate; public eventpageradapter(fragmentmanager fm) { super(fm); currentdate = calendar.getinstance(); } public void setcurrentdate(date date) { currentdate.settime(date); } @override public fragment getitem(int i) { log.i(log, "item " + i); // reset date every time currentdate.settime(new date()); simpledateformat fmt = new simpledateformat("yyyymmdd"); int days = (i > 50 ? i-50 : -50+i); currentdate.add(calendar.date, days); string dateformatted = fmt.format(currentdate.gettime()); bundle args = new bundle(); args.putstring("calendardate", dateformatted); fragment fragment = new eventfragment(); fragment.setarguments(args); homecoming fragment; } @override public int getcount() { homecoming 100; } @override public charsequence getpagetitle(int position) { log.i(log, "position " + position); int days = 0; days = (position > 49 ? position-49 : -49+position); currentdate.settime(new date()); currentdate.add(calendar.date, days); simpledateformat fmt = new simpledateformat("e, d.mmm"); string dateformatted = fmt.format(currentdate.gettime()); homecoming dateformatted; }

i want have 100 fragments displaying fragment 49 current date first fragment. swiping left increment date 1 day, swiping right decrease it.

eventpageradapter eventpageradapter = new eventpageradapter(getsupportfragmentmanager()); viewpager viewpager = (viewpager) findviewbyid(r.id.pager); viewpager.setadapter(eventpageradapter); viewpager.setcurrentitem(49);

this works fine getpagetitle() method, has right index. getitem() not. thought why? occuring because these fragments cached?

because getitem() called 1 time every item, prepare problem implementing sparsearray.

@override public fragment getitem(int position) { log.i(log, "item " + position); // reset date every time currentdate.settime(new date()); int days = (position > 49 ? position-49 : -49+position); currentdate.add(calendar.date, days); string dateformatted = comparedateformat.format(currentdate.gettime()); bundle args = new bundle(); args.putstring("calendardate", dateformatted); eventfragment fragment = new eventfragment(); fragment.setarguments(args); fragments.put(position, fragment); homecoming fragment; } @override public object instantiateitem(viewgroup view, int position) { eventfragment fragment = fragments.get(position); if (fragment != null) { homecoming fragment; } else { homecoming super.instantiateitem(view, position); } }

android

No comments:

Post a Comment