我有一个基于 SherlockFragment 的片段,其中包括一个通过 FragmentStatePagerAdapter 与片段一起操作的 ViewPager。我实现了“无限”滚动,因此适配器返回 getCount() 1000。它可以工作。当我用另一个数据替换()来自主活动的主片段时出现问题,这个主片段的 onCreateView 在第一次初始化期间调用:我重新创建适配器并将其设置为 ViewPager,运行 notifyDataSetChanged()并设置相关视图设置当前项()。此时我没有看到 Fragment,因为 getItem() 没有被调用。如果我向左或向右滑动 ViewPager,则显示中间的第三个片段,然后一切正常,即 getItem() 仅在滑动期间被调用:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_timebar, container, false);
mPager = (ViewPager)view.findViewById(R.id.timebar_pager);
mTitleIndicator = (TitlePageIndicator)view.findViewById(R.id.timebar_titles);
mPagerAdapter = new MyAdapter(container.getContext(), getSherlockActivity().getSupportFragmentManager(), mPager);
mPager.setAdapter(mPagerAdapter);
mPagerAdapter.setDate(mStartDate, mPagerAdapter.MIDDLE_POSITION);
return view;
}
public class MyAdapter extends FragmentStatePagerAdapter {
public final int FULL_COUNT = 1000;
private final int MIDDLE_POSITION = FULL_COUNT/2;
private long mMiddleFragmentDate;
private ViewPager mViewPager;
public MyAdapter(Context context, FragmentManager fragmentManager, ViewPager viewPager)
{
super(fragmentManager);
mMiddleFragmentDate = mStartDate;
mViewPager = viewPager;
}
public long getMiddlePosDate()
{
return mMiddleFragmentDate;
}
private void refreshData(int position)
{
Log.d("TIMEBAR","@@@@@ refreshData pos=" + position);
mViewPager.setCurrentItem(position);
notifyDataSetChanged();
View localView = mViewPager.findViewWithTag(Integer.valueOf(position));
if (localView != null)
{
Log.d("TIMEBAR","@@@@@ refreshData localView found & != null");
localView.invalidate();
}
mTitleIndicator.notifyDataSetChanged();
}
public Calendar getDateByPosition(int position)
{
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(mMiddleFragmentDate);
cal.add(Calendar.DAY_OF_YEAR, (position - MIDDLE_POSITION) * mNumOfDaysPerView);
return cal;
}
@Override
public void destroyItem(ViewGroup viewGroup, int position, Object object)
{
super.destroyItem(viewGroup, position, object);
}
@Override
public int getCount()
{
return FULL_COUNT;
}
@Override
public Fragment getItem(int position)
{
GregorianCalendar cal = (GregorianCalendar)getDateByPosition(position);
return TimebarFragment.newInstance(position, cal.getTimeInMillis(), mNumOfDaysPerView);
}
@Override
public int getItemPosition(Object paramObject)
{
return POSITION_NONE;
}
@Override
public CharSequence getPageTitle(int position)
{
Calendar cal = getDateByPosition(position);
StringBuilder sb = new StringBuilder();
sb.append(mCalendarUtils.getDayOfMonth(cal));
sb.append(" ");
if (mNumOfDaysPerView > 1) {
sb.append(mCalendarUtils.getMonthNameShort(cal));
} else {
sb.append(mCalendarUtils.getMonthNameLong(cal));
}
if (mNumOfDaysPerView > 1) {
sb.append(" - ");
cal.add(Calendar.DAY_OF_YEAR, mNumOfDaysPerView - 1);
sb.append(mCalendarUtils.getDayOfMonth(cal));
sb.append(" ");
sb.append(mCalendarUtils.getMonthNameShort(cal));
}
if (calNow.get(Calendar.YEAR) != cal.get(Calendar.YEAR)) {
sb.append(",");
sb.append(cal.get(Calendar.YEAR));
}
return sb.toString();
}
public void setDate(long date, int position)
{
mMiddleFragmentDate = date;
refreshData(position);
}
}