DayPickerViewPager.java revision 78bf1d329a4c0210394f846be1fd1390314aefc0
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.widget; 18 19import com.android.internal.widget.ViewPager; 20 21import android.content.Context; 22import android.graphics.drawable.Drawable; 23import android.util.AttributeSet; 24import android.view.View; 25 26import java.util.ArrayList; 27 28/** 29 * This displays a list of months in a calendar format with selectable days. 30 */ 31class DayPickerViewPager extends ViewPager { 32 private final ArrayList<View> mMatchParentChildren = new ArrayList<>(1); 33 34 public DayPickerViewPager(Context context) { 35 this(context, null); 36 } 37 38 public DayPickerViewPager(Context context, AttributeSet attrs) { 39 this(context, attrs, 0); 40 } 41 42 public DayPickerViewPager(Context context, AttributeSet attrs, int defStyleAttr) { 43 this(context, attrs, defStyleAttr, 0); 44 } 45 46 public DayPickerViewPager(Context context, AttributeSet attrs, int defStyleAttr, 47 int defStyleRes) { 48 super(context, attrs, defStyleAttr, defStyleRes); 49 } 50 51 @Override 52 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 53 populate(); 54 55 // Everything below is mostly copied from FrameLayout. 56 int count = getChildCount(); 57 58 final boolean measureMatchParentChildren = 59 MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY || 60 MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY; 61 62 int maxHeight = 0; 63 int maxWidth = 0; 64 int childState = 0; 65 66 for (int i = 0; i < count; i++) { 67 final View child = getChildAt(i); 68 if (child.getVisibility() != GONE) { 69 measureChild(child, widthMeasureSpec, heightMeasureSpec); 70 final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 71 maxWidth = Math.max(maxWidth, child.getMeasuredWidth()); 72 maxHeight = Math.max(maxHeight, child.getMeasuredHeight()); 73 childState = combineMeasuredStates(childState, child.getMeasuredState()); 74 if (measureMatchParentChildren) { 75 if (lp.width == LayoutParams.MATCH_PARENT || 76 lp.height == LayoutParams.MATCH_PARENT) { 77 mMatchParentChildren.add(child); 78 } 79 } 80 } 81 } 82 83 // Account for padding too 84 maxWidth += getPaddingLeft() + getPaddingRight(); 85 maxHeight += getPaddingTop() + getPaddingBottom(); 86 87 // Check against our minimum height and width 88 maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); 89 maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); 90 91 // Check against our foreground's minimum height and width 92 final Drawable drawable = getForeground(); 93 if (drawable != null) { 94 maxHeight = Math.max(maxHeight, drawable.getMinimumHeight()); 95 maxWidth = Math.max(maxWidth, drawable.getMinimumWidth()); 96 } 97 98 setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), 99 resolveSizeAndState(maxHeight, heightMeasureSpec, 100 childState << MEASURED_HEIGHT_STATE_SHIFT)); 101 102 count = mMatchParentChildren.size(); 103 if (count > 1) { 104 for (int i = 0; i < count; i++) { 105 final View child = mMatchParentChildren.get(i); 106 107 final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 108 final int childWidthMeasureSpec; 109 final int childHeightMeasureSpec; 110 111 if (lp.width == LayoutParams.MATCH_PARENT) { 112 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec( 113 getMeasuredWidth() - getPaddingLeft() - getPaddingRight(), 114 MeasureSpec.EXACTLY); 115 } else { 116 childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 117 getPaddingLeft() + getPaddingRight(), 118 lp.width); 119 } 120 121 if (lp.height == LayoutParams.MATCH_PARENT) { 122 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec( 123 getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), 124 MeasureSpec.EXACTLY); 125 } else { 126 childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, 127 getPaddingTop() + getPaddingBottom(), 128 lp.height); 129 } 130 131 child.measure(childWidthMeasureSpec, childHeightMeasureSpec); 132 } 133 } 134 135 mMatchParentChildren.clear(); 136 } 137} 138