我的FragmentPagerAdapter带有3页ViewPager.OffScreenPageLimit并始终显示最后加载的页面
我使用ViewPager来显示片段。
我的ViewPager
private static final int MONTH_SIZE = 12;
viewPager.setAdapter(new CalendarFragmentPagerAdapter(getChildFragmentManager()));
viewPager.setCurrentItem(GregorianCalendar.getInstance().get(Calendar.MONTH));
viewPager.setClickable(true);
viewPager.setSaveEnabled(false);
viewPager.setOffscreenPageLimit(1);
My FragmentPageAdapter:
public class CalendarFragmentPagerAdapter extends FragmentPagerAdapter {
/**
* @param fm
*/
public CalendarFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/**
*/
@Override
public Fragment getItem(int pageRequest) {
return new CalendarFragment(pageRequest);
}
/**
*/
@Override
public int getCount() {
return MONTH_SIZE - 1;
}
/**
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.monthJanuary);
case 1:
return getString(R.string.monthFebruary);
case 2:
return getString(R.string.monthMarch);
case 3:
return getString(R.string.monthApril);
case 4:
return getString(R.string.monthMay);
case 5:
return getString(R.string.monthJune);
case 6:
return getString(R.string.monthJuly);
case 7:
return getString(R.string.monthAugust);
case 8:
return getString(R.string.monthSeptember);
case 9:
return getString(R.string.monthOctober);
case 10:
return getString(R.string.monthNovember);
case 11:
return getString(R.string.monthDecember);
default:
return "";
}
}
/**
* @hide
*/
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
我的片段:
// Nothing especial
List<CalendarEntry> events = getEvents(indexByCalendarFragmentPagerAdapter);
listView = (ListView) view.findViewById(R.id.listView_calendar_screen);
listView.setAdapter(new CalendarCustomAdapter(getActivity().getBaseContext(), events));
listView.setClickable(true);
错误是例如显示五月份,即二月的事件
已经尝试了几种方法但无法解决问题,有人可以帮助我吗?
答案 0 :(得分:1)
只需将名为getCount()的代码或函数更改为:
@Override
public int getCount() {
return events.length;
}
答案 1 :(得分:-5)
我用自己的PagerAdapter解决了。 我使用了blog Eilfa
上的示例代码