Skip to content

Commit 2ea5213

Browse files
author
xubo
committed
修复滚动超过一屏幕的bug
1 parent b5ead13 commit 2ea5213

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ add the dependency:
4444
dependencies {
4545
   ...
4646
47-
implementation 'com.github.xubo.ceilinglayout:CeilingLayout:1.1.0'
47+
implementation 'com.github.xubo.ceilinglayout:CeilingLayout:1.1.1'
4848
}
4949
```
5050

ceilinglayout/src/main/java/com/github/xubo/ceilinglayout/CeilingLayout.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ protected void onAttachedToWindow() {
9494

9595
@Override
9696
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
97+
//最小控制高
98+
int minMeasuredHeight = MeasureSpec.getSize(heightMeasureSpec);
99+
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 1, MeasureSpec.EXACTLY);
97100
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
101+
int measuredWidth = getMeasuredWidth();
98102
int childCount = getChildCount();
99103
if (ceilingChildIndex < 0 || ceilingChildIndex >= childCount) {
100104
throw new IllegalStateException("吸顶子View位置索引错误,CeilingLayout没有索引为" + ceilingChildIndex + "的子View");
@@ -110,18 +114,21 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
110114
}
111115
LayoutParams params = (LayoutParams) childView.getLayoutParams();
112116
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偏移高度不能大于吸顶高度");
117121
}
118122
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);
123129

124130
//最后子View重新分配高度
131+
int lastChildViewHeight = getMeasuredHeight() - ceilingChildView.getMeasuredHeight() - ceilingChildParams.topMargin - ceilingChildParams.bottomMargin - ceilingOffset;
125132
View lastChildView = getChildAt(ceilingChildIndex + 1);
126133
int lastChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
127134
int lastChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(lastChildViewHeight, MeasureSpec.EXACTLY);

ceilinglayout/version.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ ext {
22
//版本code
33
versionCode = 3
44
//版本名
5-
versionName = "1.1.0"
5+
versionName = "1.1.1"
66
}

0 commit comments

Comments
 (0)