FragmentStatePagerAdapter를 통해 Fragments와 함께 작동하는 ViewPager가 포함 된 SherlockFragment 기반 조각이 있습니다. "무한"스크롤을 구현하므로 어댑터는 getCount() 1000을 반환합니다. 이 문제는 메인 액티비티의 주 프래그먼트를 다른 데이터로 대체 할 때 발생합니다. 첫 번째 초기화 중에 호출 된 main 프래그먼트의 onCreateView : 어댑터를 다시 만들고 ViewPager로 설정하고 notifyDataSetChanged()를 실행하고 관련 뷰를 설정합니다. setCurrentItem(). 이 시점에서 getItem()이 호출되지 않았기 때문에 Fragment가 표시되지 않습니다.ViewPager를 다시로드해도 처음 세 조각이 생성되지 않습니다
@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);
}
}