@@ -332,6 +332,11 @@ open class DslTabLayout(
332
332
return
333
333
}
334
334
dslSelector.selector(index, true , notify, fromUser)
335
+ if (! notify) {
336
+ if (! needScroll) {
337
+ _scrollToDefault (tabIndicator.indicatorAnim)
338
+ }
339
+ }
335
340
}
336
341
337
342
/* *关联[ViewPagerDelegate]*/
@@ -436,6 +441,18 @@ open class DslTabLayout(
436
441
437
442
open fun updateTabLayout () {
438
443
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
+
439
456
dslSelector.updateStyle()
440
457
dslSelector.updateClickListener()
441
458
}
@@ -450,9 +467,9 @@ open class DslTabLayout(
450
467
// 自定义的背景
451
468
tabConvexBackgroundDrawable?.apply {
452
469
if (isHorizontal()) {
453
- setBounds(0 , _maxConvexHeight , right - left, bottom - top)
470
+ setBounds(0 , _maxConvexSize , right - left, bottom - top)
454
471
} else {
455
- setBounds(0 , 0 , measuredWidth - _maxConvexHeight , bottom - top)
472
+ setBounds(0 , 0 , measuredWidth - _maxConvexSize , bottom - top)
456
473
}
457
474
458
475
if (scrollX or scrollY == 0 ) {
@@ -647,11 +664,11 @@ open class DslTabLayout(
647
664
648
665
// <editor-fold desc="布局相关">
649
666
650
- // 所有child的总宽度, 不包含parent的padding
651
- var _childAllWidthSum = 0
667
+ // 所有child的总宽度/高度 , 不包含parent的padding
668
+ var _childAllSizeSum = 0
652
669
653
- // 最大的凸起高度
654
- var _maxConvexHeight = 0
670
+ // 最大的凸起高度/宽度
671
+ var _maxConvexSize = 0
655
672
656
673
override fun onMeasure (widthMeasureSpec : Int , heightMeasureSpec : Int ) {
657
674
@@ -694,7 +711,7 @@ open class DslTabLayout(
694
711
var heightSize = MeasureSpec .getSize(heightMeasureSpec)
695
712
val heightMode = MeasureSpec .getMode(heightMeasureSpec)
696
713
697
- _maxConvexHeight = 0
714
+ _maxConvexSize = 0
698
715
699
716
var childWidthSpec: Int = - 1
700
717
@@ -779,7 +796,7 @@ open class DslTabLayout(
779
796
780
797
// ...end
781
798
782
- _childAllWidthSum = 0
799
+ _childAllSizeSum = 0
783
800
784
801
// 没有设置weight属性的child宽度总和, 用于计算剩余空间
785
802
var allChildUsedWidth = 0
@@ -826,7 +843,7 @@ open class DslTabLayout(
826
843
childView.measure(childWidthSpec, childHeightSpec)
827
844
}
828
845
if (childConvexHeight > 0 ) {
829
- _maxConvexHeight = max(_maxConvexHeight , childConvexHeight)
846
+ _maxConvexSize = max(_maxConvexSize , childConvexHeight)
830
847
// 需要凸起
831
848
val spec = exactlyMeasure(childView.measuredHeight + childConvexHeight)
832
849
childView.measure(childWidthSpec, spec)
@@ -870,7 +887,7 @@ open class DslTabLayout(
870
887
871
888
childMaxHeight = max(childMaxHeight, childView.measuredHeight)
872
889
allChildUsedWidth + = childUsedWidth
873
- _childAllWidthSum + = childUsedWidth
890
+ _childAllSizeSum + = childUsedWidth
874
891
}
875
892
876
893
// 剩余空间
@@ -900,7 +917,7 @@ open class DslTabLayout(
900
917
childMaxHeight = max(childMaxHeight, childView.measuredHeight)
901
918
902
919
// 上面已经处理了分割线和margin的距离了
903
- _childAllWidthSum + = childView.measuredWidth
920
+ _childAllSizeSum + = childView.measuredWidth
904
921
}
905
922
}
906
923
// ...end
@@ -909,7 +926,7 @@ open class DslTabLayout(
909
926
// wrap_content 情况下, 重新测量所有子view
910
927
val childHeightSpec = exactlyMeasure(
911
928
max(
912
- childMaxHeight - _maxConvexHeight ,
929
+ childMaxHeight - _maxConvexSize ,
913
930
suggestedMinimumHeight - paddingTop - paddingBottom
914
931
)
915
932
)
@@ -919,7 +936,7 @@ open class DslTabLayout(
919
936
}
920
937
921
938
if (widthMode != MeasureSpec .EXACTLY ) {
922
- widthSize = min(_childAllWidthSum + paddingStart + paddingEnd, widthSize)
939
+ widthSize = min(_childAllSizeSum + paddingStart + paddingEnd, widthSize)
923
940
}
924
941
925
942
if (visibleChildList.isEmpty()) {
@@ -930,12 +947,12 @@ open class DslTabLayout(
930
947
}
931
948
} else if (heightMode != MeasureSpec .EXACTLY ) {
932
949
heightSize = max(
933
- childMaxHeight - _maxConvexHeight + paddingTop + paddingBottom,
950
+ childMaxHeight - _maxConvexSize + paddingTop + paddingBottom,
934
951
suggestedMinimumHeight
935
952
)
936
953
}
937
954
938
- setMeasuredDimension(widthSize, heightSize + _maxConvexHeight )
955
+ setMeasuredDimension(widthSize, heightSize + _maxConvexSize )
939
956
}
940
957
941
958
fun measureVertical (widthMeasureSpec : Int , heightMeasureSpec : Int ) {
@@ -964,7 +981,7 @@ open class DslTabLayout(
964
981
var heightSize = MeasureSpec .getSize(heightMeasureSpec)
965
982
val heightMode = MeasureSpec .getMode(heightMeasureSpec)
966
983
967
- _maxConvexHeight = 0
984
+ _maxConvexSize = 0
968
985
969
986
// child高度测量模式
970
987
var childHeightSpec: Int = - 1
@@ -1045,7 +1062,7 @@ open class DslTabLayout(
1045
1062
1046
1063
// ...end
1047
1064
1048
- _childAllWidthSum = 0
1065
+ _childAllSizeSum = 0
1049
1066
1050
1067
var wrapContentWidth = false
1051
1068
@@ -1060,7 +1077,7 @@ open class DslTabLayout(
1060
1077
lp.marginEnd = 0
1061
1078
1062
1079
val childConvexHeight = lp.layoutConvexHeight
1063
- _maxConvexHeight = max(_maxConvexHeight , childConvexHeight)
1080
+ _maxConvexSize = max(_maxConvexSize , childConvexHeight)
1064
1081
1065
1082
val widthHeight = calcLayoutWidthHeight(
1066
1083
lp.layoutWidth, lp.layoutHeight,
@@ -1152,7 +1169,7 @@ open class DslTabLayout(
1152
1169
}
1153
1170
1154
1171
allChildUsedHeight + = childUsedHeight
1155
- _childAllWidthSum + = childUsedHeight
1172
+ _childAllSizeSum + = childUsedHeight
1156
1173
}
1157
1174
1158
1175
// 剩余空间
@@ -1180,13 +1197,13 @@ open class DslTabLayout(
1180
1197
measureChild(childView)
1181
1198
1182
1199
// 上面已经处理了分割线和margin的距离了
1183
- _childAllWidthSum + = childView.measuredHeight
1200
+ _childAllSizeSum + = childView.measuredHeight
1184
1201
}
1185
1202
}
1186
1203
// ...end
1187
1204
1188
1205
if (heightMode != MeasureSpec .EXACTLY ) {
1189
- heightSize = min(_childAllWidthSum + paddingTop + paddingBottom, heightSize)
1206
+ heightSize = min(_childAllSizeSum + paddingTop + paddingBottom, heightSize)
1190
1207
}
1191
1208
1192
1209
if (visibleChildList.isEmpty()) {
@@ -1197,7 +1214,7 @@ open class DslTabLayout(
1197
1214
}
1198
1215
}
1199
1216
1200
- setMeasuredDimension(widthSize + _maxConvexHeight , heightSize)
1217
+ setMeasuredDimension(widthSize + _maxConvexSize , heightSize)
1201
1218
}
1202
1219
1203
1220
override fun onLayout (changed : Boolean , l : Int , t : Int , r : Int , b : Int ) {
@@ -1280,7 +1297,7 @@ open class DslTabLayout(
1280
1297
1281
1298
childBottom = when (verticalGravity) {
1282
1299
Gravity .CENTER_VERTICAL -> measuredHeight - paddingBottom -
1283
- ((measuredHeight - paddingTop - paddingBottom - _maxConvexHeight ) / 2 -
1300
+ ((measuredHeight - paddingTop - paddingBottom - _maxConvexSize ) / 2 -
1284
1301
childView.measuredHeight / 2 )
1285
1302
1286
1303
Gravity .BOTTOM -> measuredHeight - paddingBottom
@@ -1332,7 +1349,7 @@ open class DslTabLayout(
1332
1349
}
1333
1350
1334
1351
childLeft = when (horizontalGravity) {
1335
- Gravity .CENTER_HORIZONTAL -> paddingStart + ((measuredWidth - paddingStart - paddingEnd - _maxConvexHeight ) / 2 -
1352
+ Gravity .CENTER_HORIZONTAL -> paddingStart + ((measuredWidth - paddingStart - paddingEnd - _maxConvexSize ) / 2 -
1336
1353
childView.measuredWidth / 2 )
1337
1354
1338
1355
Gravity .RIGHT -> measuredWidth - paddingRight - childView.measuredWidth - lp.rightMargin
@@ -1626,10 +1643,10 @@ open class DslTabLayout(
1626
1643
1627
1644
/* *view最大的宽度*/
1628
1645
val maxWidth: Int
1629
- get() = _childAllWidthSum + paddingStart + paddingEnd
1646
+ get() = _childAllSizeSum + paddingStart + paddingEnd
1630
1647
1631
1648
val maxHeight: Int
1632
- get() = _childAllWidthSum + paddingTop + paddingBottom
1649
+ get() = _childAllSizeSum + paddingTop + paddingBottom
1633
1650
1634
1651
open fun onFlingChange (velocity : Float /* 瞬时值*/ ) {
1635
1652
if (needScroll) {
@@ -1709,12 +1726,14 @@ open class DslTabLayout(
1709
1726
postInvalidate()
1710
1727
}
1711
1728
1712
- fun startScroll (dv : Int ) {
1729
+ /* *[dv] 主方向滚动的距离
1730
+ * [cdv] 交叉方向滚动的距离*/
1731
+ fun startScroll (dv : Int , cdv : Int = 0) {
1713
1732
_overScroller .abortAnimation()
1714
1733
if (isHorizontal()) {
1715
- _overScroller .startScroll(scrollX, scrollY, dv, 0 , scrollAnimDuration)
1734
+ _overScroller .startScroll(scrollX, scrollY, dv, cdv , scrollAnimDuration)
1716
1735
} else {
1717
- _overScroller .startScroll(scrollX, scrollY, 0 , dv, scrollAnimDuration)
1736
+ _overScroller .startScroll(scrollX, scrollY, cdv , dv, scrollAnimDuration)
1718
1737
}
1719
1738
ViewCompat .postInvalidateOnAnimation(this )
1720
1739
}
@@ -1795,6 +1814,7 @@ open class DslTabLayout(
1795
1814
/* *将[index]位置显示在TabLayout的中心*/
1796
1815
fun _scrollToTarget (index : Int , scrollAnim : Boolean ) {
1797
1816
if (! needScroll) {
1817
+ _scrollToDefault (scrollAnim)
1798
1818
return
1799
1819
}
1800
1820
@@ -1873,6 +1893,26 @@ open class DslTabLayout(
1873
1893
}
1874
1894
}
1875
1895
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
+
1876
1916
// </editor-fold desc="滚动相关">
1877
1917
1878
1918
// <editor-fold desc="动画相关">
0 commit comments