In my activity, I\'m using ViewPager to swipe left/right and show month view accordingly. I\'m passing the month and year values from onCreate
to the PagerAdapter:<
I have achieve to implement this but not perfectly by using each page as 3 fragments. When User swift right or left, It will set back to middle and update new data source for each fragment.
In Main
MonthViewContentFragment[] fragList = new MonthViewContentFragment[3];
fragList[0] = MonthViewContentFragment.newInstance(prevMonth);
fragList[1] = MonthViewContentFragment.newInstance(currentMonth);
fragList[2] = MonthViewContentFragment.newInstance(nextMonth);
ViewPager monthPage = (ViewPager) v.findViewById(R.id.monthview_content);
MonthPageAdapter monthPageAdapter = new MonthPageAdapter(getFragmentManager(), fragList);
monthPage.setAdapter(monthPageAdapter);
monthPage.setOnPageChangeListener(this);
monthPage.setCurrentItem(1, false);
in onPageScrollStateChanged
if (state == ViewPager.SCROLL_STATE_IDLE) {
if (focusPage < PAGE_CENTER) {
currentMonth.add(Calendar.MONTH, -1);
} else if (focusPage > PAGE_CENTER) {
currentMonth.add(Calendar.MONTH, 1);
}
monthPageAdapter.setCalendar(currentMonth);
monthPage.setCurrentItem(1, false);
updateTitle();
}
in onPageSelected
public void onPageSelected(int position) {
// TODO Auto-generated method stub
focusPage = position;
}
In MonthPageAdapter
MonthViewContentFragment[] fragList;
MonthViewContentFragment temp;
int fragNumber = 3;
public MonthPageAdapter(FragmentManager fm, MonthViewContentFragment[] fragList) {
super(fm);
this.fragList = fragList;
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
return fragList[position];
}
@Override
public int getCount() {
return 3;
}
public void setCalendar(Calendar currentMonth) {
Calendar prevMonth = Calendar.getInstance();
prevMonth.setTime(currentMonth.getTime());
prevMonth.add(Calendar.MONTH, -1);
Calendar nextMonth = Calendar.getInstance();
nextMonth.setTime(currentMonth.getTime());
nextMonth.add(Calendar.MONTH, 1);
//update all 3 fragments
fragList[0].updateUI(prevMonth);
fragList[1].updateUI(currentMonth);
fragList[2].updateUI(nextMonth);
}
In MonthViewContentFragment
@Override
public void updateUI(Calendar monthYear) {
View v = getView();
if (v == null)
return;
//setNewDataSource calcurate new List<String> date
calendarViewAdapter.setNewDataSource(monthYear);
calendarViewAdapter.notifyDataSetChanged();
}
This is not perfect because when user swift too fast. The scroll is not change to idle yet then ViewPager
reach its end.
I'm not sure i understand this (never used this viewPager, have always used another HorizontalViewSlider instead), but for your onPageScrollStateChanged() why do you have a special case for focusedPage==0 and focusedPage==2 ?
if nothing is special about Jan and March, maybe remove everything inside onPageScrollStateChanged() ?
my guess is that the flickering is from when you call setGridCellAdapter() because that will refresh the page from calling notifyDataSetChanged.