1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.haibin.oldcalendarview;
 
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.ViewGroup;
 
 
/**
 * 这是一个自适应高度的View
 */
@SuppressWarnings("unused")
public class WrapViewPager extends ViewPager {
    private int mNextViewHeight, mPreViewHeight, mCurrentViewHeight;
    private boolean isFirstInit = true;
    CalendarLayout mParentLayout;
    public WrapViewPager(Context context) {
        this(context, null);
    }
 
    public WrapViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        addOnPageChangeListener(new OnPageChangeListener() {
            /**
             *
             * @param position position如果小于当前position,则代表往前一页滑动,否则为下一页
             * @param positionOffset 滑动比例
             * @param positionOffsetPixels 滑动像素
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (getTranslationY() != 0) {
                    return;
                }
                int height;
                if (position < getCurrentItem()) {//右滑-1
                    height = (int) ((mPreViewHeight)
                            * (1 - positionOffset) +
                            mCurrentViewHeight
                                    * positionOffset);
                } else {//左滑+!
                    height = (int) ((mCurrentViewHeight)
                            * (1 - positionOffset) +
                            (mNextViewHeight)
                                    * positionOffset);
                }
                ViewGroup.LayoutParams params = getLayoutParams();
                params.height = height;
                setLayoutParams(params);
            }
 
            @Override
            public void onPageSelected(int position) {
                int year = position / 12 + CalendarView.mMinYear;
                int month = position % 12 + 1;
                mCurrentViewHeight = Util.getCardHeight(year, month);
                if (month == 1) {
                    mNextViewHeight = Util.getCardHeight(year, month + 1);
                    mPreViewHeight = Util.getCardHeight(year - 1, 12);
                } else {
                    mPreViewHeight = Util.getCardHeight(year, month - 1);
                    if (month == 12) {
                        mNextViewHeight = Util.getCardHeight(year + 1, 1);
                    } else {
                        mNextViewHeight = Util.getCardHeight(year, month + 1);
                    }
                }
                if (isFirstInit) {
                    ViewGroup.LayoutParams params = getLayoutParams();
                    params.height = mCurrentViewHeight;
                    setLayoutParams(params);
                    isFirstInit = false;
                }
            }
 
            @Override
            public void onPageScrollStateChanged(int state) {
 
            }
        });
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(Util.getCardHeight(0,0),MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
 
 
 
    /**
     * ViewPager平移会调用这个方法
     *
     * @param y y
     * @param isFinish isFinish
     */
    void onTranslate(int y, boolean isFinish) {
 
    }
 
    int getCurrentHeight() {
        return mCurrentViewHeight;
    }
}