From 7ffb83257997a78f06568a20e395f6c5b7e0153d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E4=BB=BB=E5=BD=A6?= Date: Thu, 17 Jun 2021 14:35:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DscrollToChild=E5=88=B0?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AAView=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConsecutiveScrollerLayout.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java index b7b2c0a..e2dd0bc 100644 --- a/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java +++ b/consecutivescroller/src/main/java/com/donkingliang/consecutivescroller/ConsecutiveScrollerLayout.java @@ -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)); @@ -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 @@ -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); } @@ -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; @@ -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; @@ -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); @@ -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; @@ -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);