Android custom calendar view

I am creating a custom calendar view for my Android application that allows me to scroll between months. I created custom calendar square views that I embedded in the custom calendar month, and everything works fine on a 1-month scale.

Sorry, I'm stuck now. Now I want to embed my own custom calendar views into the endless viewing of the pager so that I can endlessly scroll forward and backward through the calendar.

I tried adapting this horizontal pager to scroll endlessly with a simple trick. I keep an array of 3 of my calendar views and update the list based on where the user scrolls. Example...

  • [July, August , September] (the focus is on August, then the user switches to September)
  • [July, August, September ] (now the focus is on September).
  • [August, August, September ] (shift in August remained one, rewrote July)
  • [August, September, September ] (the shift in September remained one, rewrote August)
  • [August, September , September] (set the view to mid-September so we center again)
  • [August, September , October] (rewrite September 2 with the actual next month)

However, when I do this, a noticeable flash appears on the screen as I scroll to the next month.

Here is the forward scroll code (note: reverse scrolling has the same problem)

private void updateViewsForForwardScroll() { // Note: INDEX_PREV = 0; INDEX_CURR = 1; INDEX_NEXT = 2 ((CalendarMonthView) getChildAt(INDEX_PREV)).showMonth(oneMonthPriorTo(currentMonth)); ((CalendarMonthView) getChildAt(INDEX_CURR)).showMonth(currentMonth); setCurrentScreen(INDEX_CURR, false); ((CalendarMonthView) getChildAt(INDEX_NEXT)).showMonth(oneMonthAfter(currentMonth)); } 

I think the problem is that setCurrentScreen () ends before showMonth (currentMonth), so the view in INDEX_CURR is still updated when the screen is set. I tried to solve this using the following strategy instead ...

  • [July, August , September] (the focus is on August, then the user switches to September)
  • [July, August, September ] (now the focus is on September).
  • [August, September ] (delete July, but save it for recycling)
  • [August, September , October] (restart July to display October instead and add a view)

I have not touched the current focused view at all, but there is still a flash! This time, the screen flashes from September to August, and then until September.

So what am I doing wrong? Is there a way to do what I'm trying to do without knowing the user about it? If not, is there an existing class that I can use?

(As a side question, is it possible to customize Android CalendarView in any way visually? This will really solve all my problems ...)

Thanks in advance!

+4
source share
1 answer

In the end, I started working by expanding the FragmentStatePagerAdapter , and also integrating the user113215 clause. I did not engage in truly endless scrolling, but almost endless (i.e. 10,000 months or 833 years). Here is the main part of my code:

 public static class CalendarAdapter extends FragmentStatePagerAdapter { private static final int NUM_MONTHS = 10000; private static final int INDEX_EPOCH = NUM_MONTHS/2; private static final int MONTHS_PER_YEAR = 12; private static final Calendar calendarMonthEpoch = new GregorianCalendar(2012, Calendar.AUGUST, 1); // ... @Override public int getCount() { return NUM_MONTHS; } @Override public Fragment getItem(int position) { return CalendarMonthFragment.newInstance(calendarMonthAtPosition(position)); } private static Calendar calendarMonthAtPosition(int position) { int offset = position - INDEX_EPOCH; Calendar calMonthAtPosition = (Calendar) calendarMonthEpoch.clone(); calMonthAtPosition.add(Calendar.MONTH, offset % MONTHS_PER_YEAR); calMonthAtPosition.add(Calendar.YEAR, offset / MONTHS_PER_YEAR); return calMonthAtPosition; } // ... } 
+1
source

All Articles