Skip to content

Commit 1ddd59e

Browse files
committed
* fix scroll reset
1 parent f840264 commit 1ddd59e

File tree

2 files changed

+75
-31
lines changed

2 files changed

+75
-31
lines changed

TabLayout/src/main/java/com/angcyo/tablayout/DslTabIndicator.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ open class DslTabIndicator(val tabLayout: DslTabLayout) : DslGradientDrawable()
445445
result = getChildTargetWidth(indicatorContentView(childView) ?: childView)
446446
}
447447
}
448+
448449
ViewGroup.LayoutParams.MATCH_PARENT -> {
449450
tabLayout.dslSelector.visibleViewList.getOrNull(index)?.also { childView ->
450451
result = childView.measuredWidth
@@ -464,6 +465,7 @@ open class DslTabIndicator(val tabLayout: DslTabLayout) : DslGradientDrawable()
464465
result = getChildTargetHeight(indicatorContentView(childView) ?: childView)
465466
}
466467
}
468+
467469
ViewGroup.LayoutParams.MATCH_PARENT -> {
468470
tabLayout.dslSelector.visibleViewList.getOrNull(index)?.also { childView ->
469471
result = childView.measuredHeight
@@ -600,7 +602,7 @@ open class DslTabIndicator(val tabLayout: DslTabLayout) : DslGradientDrawable()
600602
//居中绘制
601603
else -> paddingTop + viewDrawHeight / 2 - drawHeight / 2 + indicatorYOffset -
602604
animExHeight +
603-
(tabLayout._maxConvexHeight - _childConvexHeight(currentIndex)) / 2
605+
(tabLayout._maxConvexSize - _childConvexHeight(currentIndex)) / 2
604606
}
605607

606608
indicatorDrawable?.apply {
@@ -840,14 +842,16 @@ open class DslTabIndicator(val tabLayout: DslTabLayout) : DslGradientDrawable()
840842
//右边/底部绘制
841843
viewWidth - drawWidth - indicatorXOffset
842844
}
845+
843846
INDICATOR_STYLE_TOP -> {
844847
//左边/顶部绘制
845848
0 + indicatorXOffset
846849
}
850+
847851
else -> {
848852
//居中绘制
849853
paddingLeft + indicatorXOffset + (viewDrawWidth / 2 - drawWidth / 2) -
850-
(tabLayout._maxConvexHeight - _childConvexHeight(currentIndex)) / 2
854+
(tabLayout._maxConvexSize - _childConvexHeight(currentIndex)) / 2
851855
}
852856
}
853857

TabLayout/src/main/java/com/angcyo/tablayout/DslTabLayout.kt

+69-29
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ open class DslTabLayout(
332332
return
333333
}
334334
dslSelector.selector(index, true, notify, fromUser)
335+
if (!notify) {
336+
if (!needScroll) {
337+
_scrollToDefault(tabIndicator.indicatorAnim)
338+
}
339+
}
335340
}
336341

337342
/**关联[ViewPagerDelegate]*/
@@ -436,6 +441,18 @@ open class DslTabLayout(
436441

437442
open fun updateTabLayout() {
438443
dslSelector.updateVisibleList()
444+
445+
_childAllSizeSum = 0
446+
val visibleChildList = dslSelector.visibleViewList
447+
visibleChildList.forEachIndexed { index, childView ->
448+
val lp = childView.layoutParams as LayoutParams
449+
_childAllSizeSum += if (isHorizontal()) {
450+
childView.measuredWidth + lp.marginStart + lp.marginEnd
451+
} else {
452+
childView.measuredHeight + lp.topMargin + lp.bottomMargin
453+
}
454+
}
455+
439456
dslSelector.updateStyle()
440457
dslSelector.updateClickListener()
441458
}
@@ -450,9 +467,9 @@ open class DslTabLayout(
450467
//自定义的背景
451468
tabConvexBackgroundDrawable?.apply {
452469
if (isHorizontal()) {
453-
setBounds(0, _maxConvexHeight, right - left, bottom - top)
470+
setBounds(0, _maxConvexSize, right - left, bottom - top)
454471
} else {
455-
setBounds(0, 0, measuredWidth - _maxConvexHeight, bottom - top)
472+
setBounds(0, 0, measuredWidth - _maxConvexSize, bottom - top)
456473
}
457474

458475
if (scrollX or scrollY == 0) {
@@ -647,11 +664,11 @@ open class DslTabLayout(
647664

648665
//<editor-fold desc="布局相关">
649666

650-
//所有child的总宽度, 不包含parent的padding
651-
var _childAllWidthSum = 0
667+
//所有child的总宽度/高度, 不包含parent的padding
668+
var _childAllSizeSum = 0
652669

653-
//最大的凸起高度
654-
var _maxConvexHeight = 0
670+
//最大的凸起高度/宽度
671+
var _maxConvexSize = 0
655672

656673
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
657674

@@ -694,7 +711,7 @@ open class DslTabLayout(
694711
var heightSize = MeasureSpec.getSize(heightMeasureSpec)
695712
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
696713

697-
_maxConvexHeight = 0
714+
_maxConvexSize = 0
698715

699716
var childWidthSpec: Int = -1
700717

@@ -779,7 +796,7 @@ open class DslTabLayout(
779796

780797
//...end
781798

782-
_childAllWidthSum = 0
799+
_childAllSizeSum = 0
783800

784801
//没有设置weight属性的child宽度总和, 用于计算剩余空间
785802
var allChildUsedWidth = 0
@@ -826,7 +843,7 @@ open class DslTabLayout(
826843
childView.measure(childWidthSpec, childHeightSpec)
827844
}
828845
if (childConvexHeight > 0) {
829-
_maxConvexHeight = max(_maxConvexHeight, childConvexHeight)
846+
_maxConvexSize = max(_maxConvexSize, childConvexHeight)
830847
//需要凸起
831848
val spec = exactlyMeasure(childView.measuredHeight + childConvexHeight)
832849
childView.measure(childWidthSpec, spec)
@@ -870,7 +887,7 @@ open class DslTabLayout(
870887

871888
childMaxHeight = max(childMaxHeight, childView.measuredHeight)
872889
allChildUsedWidth += childUsedWidth
873-
_childAllWidthSum += childUsedWidth
890+
_childAllSizeSum += childUsedWidth
874891
}
875892

876893
//剩余空间
@@ -900,7 +917,7 @@ open class DslTabLayout(
900917
childMaxHeight = max(childMaxHeight, childView.measuredHeight)
901918

902919
//上面已经处理了分割线和margin的距离了
903-
_childAllWidthSum += childView.measuredWidth
920+
_childAllSizeSum += childView.measuredWidth
904921
}
905922
}
906923
//...end
@@ -909,7 +926,7 @@ open class DslTabLayout(
909926
//wrap_content 情况下, 重新测量所有子view
910927
val childHeightSpec = exactlyMeasure(
911928
max(
912-
childMaxHeight - _maxConvexHeight,
929+
childMaxHeight - _maxConvexSize,
913930
suggestedMinimumHeight - paddingTop - paddingBottom
914931
)
915932
)
@@ -919,7 +936,7 @@ open class DslTabLayout(
919936
}
920937

921938
if (widthMode != MeasureSpec.EXACTLY) {
922-
widthSize = min(_childAllWidthSum + paddingStart + paddingEnd, widthSize)
939+
widthSize = min(_childAllSizeSum + paddingStart + paddingEnd, widthSize)
923940
}
924941

925942
if (visibleChildList.isEmpty()) {
@@ -930,12 +947,12 @@ open class DslTabLayout(
930947
}
931948
} else if (heightMode != MeasureSpec.EXACTLY) {
932949
heightSize = max(
933-
childMaxHeight - _maxConvexHeight + paddingTop + paddingBottom,
950+
childMaxHeight - _maxConvexSize + paddingTop + paddingBottom,
934951
suggestedMinimumHeight
935952
)
936953
}
937954

938-
setMeasuredDimension(widthSize, heightSize + _maxConvexHeight)
955+
setMeasuredDimension(widthSize, heightSize + _maxConvexSize)
939956
}
940957

941958
fun measureVertical(widthMeasureSpec: Int, heightMeasureSpec: Int) {
@@ -964,7 +981,7 @@ open class DslTabLayout(
964981
var heightSize = MeasureSpec.getSize(heightMeasureSpec)
965982
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
966983

967-
_maxConvexHeight = 0
984+
_maxConvexSize = 0
968985

969986
//child高度测量模式
970987
var childHeightSpec: Int = -1
@@ -1045,7 +1062,7 @@ open class DslTabLayout(
10451062

10461063
//...end
10471064

1048-
_childAllWidthSum = 0
1065+
_childAllSizeSum = 0
10491066

10501067
var wrapContentWidth = false
10511068

@@ -1060,7 +1077,7 @@ open class DslTabLayout(
10601077
lp.marginEnd = 0
10611078

10621079
val childConvexHeight = lp.layoutConvexHeight
1063-
_maxConvexHeight = max(_maxConvexHeight, childConvexHeight)
1080+
_maxConvexSize = max(_maxConvexSize, childConvexHeight)
10641081

10651082
val widthHeight = calcLayoutWidthHeight(
10661083
lp.layoutWidth, lp.layoutHeight,
@@ -1152,7 +1169,7 @@ open class DslTabLayout(
11521169
}
11531170

11541171
allChildUsedHeight += childUsedHeight
1155-
_childAllWidthSum += childUsedHeight
1172+
_childAllSizeSum += childUsedHeight
11561173
}
11571174

11581175
//剩余空间
@@ -1180,13 +1197,13 @@ open class DslTabLayout(
11801197
measureChild(childView)
11811198

11821199
//上面已经处理了分割线和margin的距离了
1183-
_childAllWidthSum += childView.measuredHeight
1200+
_childAllSizeSum += childView.measuredHeight
11841201
}
11851202
}
11861203
//...end
11871204

11881205
if (heightMode != MeasureSpec.EXACTLY) {
1189-
heightSize = min(_childAllWidthSum + paddingTop + paddingBottom, heightSize)
1206+
heightSize = min(_childAllSizeSum + paddingTop + paddingBottom, heightSize)
11901207
}
11911208

11921209
if (visibleChildList.isEmpty()) {
@@ -1197,7 +1214,7 @@ open class DslTabLayout(
11971214
}
11981215
}
11991216

1200-
setMeasuredDimension(widthSize + _maxConvexHeight, heightSize)
1217+
setMeasuredDimension(widthSize + _maxConvexSize, heightSize)
12011218
}
12021219

12031220
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
@@ -1280,7 +1297,7 @@ open class DslTabLayout(
12801297

12811298
childBottom = when (verticalGravity) {
12821299
Gravity.CENTER_VERTICAL -> measuredHeight - paddingBottom -
1283-
((measuredHeight - paddingTop - paddingBottom - _maxConvexHeight) / 2 -
1300+
((measuredHeight - paddingTop - paddingBottom - _maxConvexSize) / 2 -
12841301
childView.measuredHeight / 2)
12851302

12861303
Gravity.BOTTOM -> measuredHeight - paddingBottom
@@ -1332,7 +1349,7 @@ open class DslTabLayout(
13321349
}
13331350

13341351
childLeft = when (horizontalGravity) {
1335-
Gravity.CENTER_HORIZONTAL -> paddingStart + ((measuredWidth - paddingStart - paddingEnd - _maxConvexHeight) / 2 -
1352+
Gravity.CENTER_HORIZONTAL -> paddingStart + ((measuredWidth - paddingStart - paddingEnd - _maxConvexSize) / 2 -
13361353
childView.measuredWidth / 2)
13371354

13381355
Gravity.RIGHT -> measuredWidth - paddingRight - childView.measuredWidth - lp.rightMargin
@@ -1626,10 +1643,10 @@ open class DslTabLayout(
16261643

16271644
/**view最大的宽度*/
16281645
val maxWidth: Int
1629-
get() = _childAllWidthSum + paddingStart + paddingEnd
1646+
get() = _childAllSizeSum + paddingStart + paddingEnd
16301647

16311648
val maxHeight: Int
1632-
get() = _childAllWidthSum + paddingTop + paddingBottom
1649+
get() = _childAllSizeSum + paddingTop + paddingBottom
16331650

16341651
open fun onFlingChange(velocity: Float /*瞬时值*/) {
16351652
if (needScroll) {
@@ -1709,12 +1726,14 @@ open class DslTabLayout(
17091726
postInvalidate()
17101727
}
17111728

1712-
fun startScroll(dv: Int) {
1729+
/**[dv] 主方向滚动的距离
1730+
* [cdv] 交叉方向滚动的距离*/
1731+
fun startScroll(dv: Int, cdv: Int = 0) {
17131732
_overScroller.abortAnimation()
17141733
if (isHorizontal()) {
1715-
_overScroller.startScroll(scrollX, scrollY, dv, 0, scrollAnimDuration)
1734+
_overScroller.startScroll(scrollX, scrollY, dv, cdv, scrollAnimDuration)
17161735
} else {
1717-
_overScroller.startScroll(scrollX, scrollY, 0, dv, scrollAnimDuration)
1736+
_overScroller.startScroll(scrollX, scrollY, cdv, dv, scrollAnimDuration)
17181737
}
17191738
ViewCompat.postInvalidateOnAnimation(this)
17201739
}
@@ -1795,6 +1814,7 @@ open class DslTabLayout(
17951814
/**将[index]位置显示在TabLayout的中心*/
17961815
fun _scrollToTarget(index: Int, scrollAnim: Boolean) {
17971816
if (!needScroll) {
1817+
_scrollToDefault(scrollAnim)
17981818
return
17991819
}
18001820

@@ -1873,6 +1893,26 @@ open class DslTabLayout(
18731893
}
18741894
}
18751895

1896+
/**滚动到默认位置, 通常是0,0的位置*/
1897+
fun _scrollToDefault(scrollAnim: Boolean) {
1898+
if (scrollX != 0 || scrollY != 0) {
1899+
if (isInEditMode || !scrollAnim) {
1900+
_overScroller.abortAnimation()
1901+
scrollBy(-scrollX, -scrollY)
1902+
} else {
1903+
_overScroller.abortAnimation()
1904+
_overScroller.startScroll(
1905+
scrollX,
1906+
scrollY,
1907+
-scrollX,
1908+
-scrollY,
1909+
scrollAnimDuration
1910+
)
1911+
ViewCompat.postInvalidateOnAnimation(this)
1912+
}
1913+
}
1914+
}
1915+
18761916
//</editor-fold desc="滚动相关">
18771917

18781918
//<editor-fold desc="动画相关">

0 commit comments

Comments
 (0)