Skip to content

Commit

Permalink
修复scrollToChild到最后一个View的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
梁任彦 committed Jun 17, 2021
1 parent e2e9e3b commit 7ffb832
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightUsed = 0;

// 测量底部view,并且需要自动调整高度时,计算吸顶部分占用的空间高度,作为测量子view的条件。
if (mAutoAdjustHeightAtBottomView && child == getChildAt(getChildCount() - 1)) {
heightUsed = getAdjustHeight();
}
heightUsed = getAdjustHeightForChild(child);

measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, heightUsed);
contentWidth = Math.max(contentWidth, getContentWidth(child));
Expand All @@ -336,6 +334,20 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureSize(heightMeasureSpec, contentHeight + getPaddingTop() + getPaddingBottom()));
}

/**
* 返回底部view需要调整的高度
* 只有mAutoAdjustHeightAtBottomView=true,并且child是底部view时有值,否则返回0
*
* @param child
* @return
*/
private int getAdjustHeightForChild(View child) {
if (mAutoAdjustHeightAtBottomView && child == getChildAt(getChildCount() - 1)) {
return getAdjustHeight();
}
return 0;
}

/**
* 返回底部view需要调整的高度
* 普通吸顶模式:最后的吸顶view高度 + mAdjustHeightOffset
Expand Down Expand Up @@ -1039,6 +1051,7 @@ private void scrollUp(int offset) {
if (mScrollToIndex != -1) {
View view = getChildAt(mScrollToIndex);
scrollAnchor = view.getTop() - mScrollToIndexWithOffset;
scrollAnchor -= getAdjustHeightForChild(view);
if (mScrollToIndexWithOffset < 0) {
viewScrollOffset = getViewsScrollOffset(mScrollToIndex);
}
Expand Down Expand Up @@ -1105,6 +1118,7 @@ private void scrollDown(int offset) {
if (mScrollToIndex != -1) {
View view = getChildAt(mScrollToIndex);
scrollAnchor = view.getTop() - mScrollToIndexWithOffset;
scrollAnchor -= getAdjustHeightForChild(view);
viewScrollOffset = getViewsScrollOffset(mScrollToIndex);
if (getScrollY() + getPaddingTop() + viewScrollOffset <= scrollAnchor || isScrollTop()) {
mScrollToIndex = -1;
Expand Down Expand Up @@ -2069,6 +2083,7 @@ public void scrollToChildWithOffset(View view, int offset) {
if (scrollToIndex != -1) {

int scrollAnchor = view.getTop() - offset;
scrollAnchor -= getAdjustHeightForChild(view);

// 滑动方向。
int scrollOrientation = 0;
Expand All @@ -2078,6 +2093,8 @@ public void scrollToChildWithOffset(View view, int offset) {
scrollOrientation = -1;
} else if (getScrollY() + getPaddingTop() < scrollAnchor) {
scrollOrientation = 1;
} else if (ScrollUtils.canScrollVertically(view, -1)) {
scrollOrientation = -1;
}
} else {
int viewScrollOffset = getViewsScrollOffset(scrollToIndex);
Expand Down Expand Up @@ -2119,6 +2136,7 @@ public void smoothScrollToChildWithOffset(View view, int offset) {
if (scrollToIndex != -1) {

int scrollAnchor = view.getTop() - offset;
scrollAnchor -= getAdjustHeightForChild(view);

// 滑动方向。
int scrollOrientation = 0;
Expand All @@ -2128,6 +2146,8 @@ public void smoothScrollToChildWithOffset(View view, int offset) {
scrollOrientation = -1;
} else if (getScrollY() + getPaddingTop() < scrollAnchor) {
scrollOrientation = 1;
} else if (ScrollUtils.canScrollVertically(view, -1)) {
scrollOrientation = -1;
}
} else {
int viewScrollOffset = getViewsScrollOffset(scrollToIndex);
Expand Down

0 comments on commit 7ffb832

Please sign in to comment.