@@ -94,7 +94,11 @@ protected void onAttachedToWindow() {
94
94
95
95
@ Override
96
96
protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
97
+ //最小控制高
98
+ int minMeasuredHeight = MeasureSpec .getSize (heightMeasureSpec );
99
+ heightMeasureSpec = MeasureSpec .makeMeasureSpec (Integer .MAX_VALUE >> 1 , MeasureSpec .EXACTLY );
97
100
super .onMeasure (widthMeasureSpec , heightMeasureSpec );
101
+ int measuredWidth = getMeasuredWidth ();
98
102
int childCount = getChildCount ();
99
103
if (ceilingChildIndex < 0 || ceilingChildIndex >= childCount ) {
100
104
throw new IllegalStateException ("吸顶子View位置索引错误,CeilingLayout没有索引为" + ceilingChildIndex + "的子View" );
@@ -110,18 +114,21 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
110
114
}
111
115
LayoutParams params = (LayoutParams ) childView .getLayoutParams ();
112
116
ceilingHeight += (childView .getMeasuredHeight () + params .topMargin + params .bottomMargin );
113
- scrollRange = ceilingHeight - ceilingOffset ;
114
- if ( scrollRange < 0 ) {
115
- throw new IllegalStateException ( "CeilingLayout偏移高度不能大于吸顶高度" );
116
- }
117
+ }
118
+ scrollRange = ceilingHeight - ceilingOffset ;
119
+ if ( scrollRange < 0 ) {
120
+ throw new IllegalStateException ( "CeilingLayout偏移高度不能大于吸顶高度" );
117
121
}
118
122
View ceilingChildView = getChildAt (ceilingChildIndex );
119
- LayoutParams params = (LayoutParams ) ceilingChildView .getLayoutParams ();
120
- int measuredWidth = MeasureSpec .getSize (widthMeasureSpec );
121
- int measuredHeight = MeasureSpec .getSize (heightMeasureSpec );
122
- int lastChildViewHeight = measuredHeight - ceilingChildView .getMeasuredHeight () - params .topMargin - params .bottomMargin ;
123
+ LayoutParams ceilingChildParams = (LayoutParams ) ceilingChildView .getLayoutParams ();
124
+ int measuredHeight = minMeasuredHeight ;
125
+ if (ceilingHeight + ceilingChildView .getMeasuredHeight () + ceilingChildParams .topMargin + ceilingChildParams .bottomMargin > minMeasuredHeight ) { //最小控制高无法排下
126
+ measuredHeight = ceilingHeight + ceilingChildView .getMeasuredHeight () + ceilingChildParams .topMargin + ceilingChildParams .bottomMargin ;
127
+ }
128
+ setMeasuredDimension (measuredWidth , measuredHeight );
123
129
124
130
//最后子View重新分配高度
131
+ int lastChildViewHeight = getMeasuredHeight () - ceilingChildView .getMeasuredHeight () - ceilingChildParams .topMargin - ceilingChildParams .bottomMargin - ceilingOffset ;
125
132
View lastChildView = getChildAt (ceilingChildIndex + 1 );
126
133
int lastChildWidthMeasureSpec = MeasureSpec .makeMeasureSpec (measuredWidth , MeasureSpec .EXACTLY );
127
134
int lastChildHeightMeasureSpec = MeasureSpec .makeMeasureSpec (lastChildViewHeight , MeasureSpec .EXACTLY );
0 commit comments