package com.othershe.calendarview;

import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.othershe.calendarview.utils.CalendarUtil;

public class CalendarPagerAdapter extends PagerAdapter {

    private AttributeSet attrs;
    //缓存上一次回收的MonthView
    private MonthView cacheView;

    public CalendarPagerAdapter(AttributeSet attrs) {
        this.attrs = attrs;
    }

    @Override
    public int getCount() {
        return 150 * 12;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        MonthView view;
        if (cacheView != null) {
            view = cacheView;
        } else {
            view = new MonthView(container.getContext(), attrs);
        }

        int[] date = CalendarUtil.positionToDate(position);

        view.setDateList(CalendarUtil.getMonthDate(date[0], date[1]));

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        cacheView = (MonthView) object;
    }
}

@@ -220,16 +220,46 @@
 rUtil;%0A%0A
+import java.util.LinkedList;%0A%0A
 public c
@@ -376,16 +376,27 @@
 private 
+LinkedList%3C
 MonthVie
@@ -396,26 +396,44 @@
 onthView
+%3E
  cache
-View
+ = new LinkedList%3C%3E()
 ;%0A%0A    p
@@ -831,25 +831,24 @@
 if (
+!
 cache
-View != null
+.isEmpty()
 ) %7B%0A
@@ -871,20 +871,30 @@
  = cache
-View
+.removeFirst()
 ;%0A      
@@ -1330,15 +1330,17 @@
 ache
-View = 
+.addLast(
 (Mon
@@ -1349,22 +1349,23 @@
 View) object
+)
 ;%0A    %7D%0A%7D%0A

fix bug