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:
private static int NUM_AWESOME_VIEWS = 3;
viewPager = (ViewPager) v.findViewById(R.id.pager);
cAdapter = new CalendarPagerAdapter(this.getActivity().getApplicationContext(), date_value, fragmentManager, month, year);
viewPager.setAdapter(cAdapter);
viewPager.setOffscreenPageLimit(3);
The Calendar starts from November and then scrolls till January. I would like the Calendar to display unlimited views forward and backward.
PagerAdapter:
public CalendarPagerAdapter(Context context, int dv, FragmentManager fm, int month, int year){
this.context = context;
this.dv = dv;
this.fm = fm;
this.month = month;
this.year = year;
}
public Object instantiateItem(View collection, int position) {
Log.d("Object", "Instantiated");
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.simple_calendar_view, null);
_calendar.set(Calendar.MONTH, month);
_calendar.set(Calendar.YEAR, year);
month = _calendar.get(Calendar.MONTH) + 1;
year = _calendar.get(Calendar.YEAR);
currentMonth = (Button) v.findViewById(R.id.currentMonth);
currentMonth.setText(hijri.getMonthForInt(month-1).toUpperCase() + " " + year);
calendarView = (GridView) v.findViewById(R.id.calendar);
calendarWeek = (GridView) v.findViewById(R.id.calendarweek);
calendarWeek.setAdapter(new CalendarWeekAdapter(context, firstDay));
// Initialised
adapter = new GridCellAdapter(calendarView, context, R.id.calendar_day_gridcell, month, year, 0, 0, 0);
adapter.notifyDataSetChanged();
calendarView.setAdapter(adapter);
((ViewPager) collection).addView(v, 0);
return v;
}
Based on Jon Willis’ code, this is what I have come up with, but when the position of the viewpager is 0 or 2, the page goes blank and then displays the next month + 1 (that is, after I scroll January, the page flickers and displays March). It does not scroll smoothly. I am not sure how else to do it.
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE)
Log.d("Calendar ViewPager", "Calendar ViewPager" + viewPager.getCurrentItem());
if (focusedPage == 0) {
_calendar.add(Calendar.MONTH, -1);
month = _calendar.get(Calendar.MONTH);
year = _calendar.get(Calendar.YEAR);
setGridCellAdapter(context, month, year);
} else if (focusedPage == 2) {
_calendar.add(Calendar.MONTH, +1);
month = _calendar.get(Calendar.MONTH);
year = _calendar.get(Calendar.YEAR);
setGridCellAdapter(context, month, year);
}
viewPager.setCurrentItem(1, false);
}
}
@Override
public void onPageSelected(int position) {
focusedPage = position;
}
});
public void setGridCellAdapter(Context ctx, int month, int year)
{
cAdapter = new CalendarPagerAdapter(ctx, date_value, fragmentManager, month, year);
Log.d("Object", "Re-instantiatted" + month + year);
cAdapter.notifyDataSetChanged();
viewPager.setAdapter(cAdapter);
}
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
in
onPageScrollStateChangedin
onPageSelectedIn MonthPageAdapter
In MonthViewContentFragment
This is not perfect because when user swift too fast. The scroll is not change to idle yet then
ViewPagerreach its end.