-
Notifications
You must be signed in to change notification settings - Fork 1
/
TabLayoutExt.kt
207 lines (188 loc) · 6.62 KB
/
TabLayoutExt.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.grock
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Typeface
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.animation.doOnEnd
import androidx.core.animation.doOnStart
import androidx.core.view.doOnDetach
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.grock.tc.R
interface CustomTabViewConfig<T : View> {
fun getCustomView(context: Context): T
fun onTabInit(tabView: T, position: Int)
}
class FixedWidthTextTabView(context: Context) : FrameLayout(context) {
/**
* 用于测量时能获得最大尺寸,
*/
val boundSizeTextView: TextView = TextView(context).apply {
visibility = View.INVISIBLE
}
val dynamicSizeTextView: ScaleTexViewTabView = ScaleTexViewTabView(context).apply {
gravity = Gravity.CENTER
}
init {
addView(boundSizeTextView)
addView(dynamicSizeTextView)
}
}
abstract class FixedWidthTextTabViewConfig(val scale: TextScaleConfig) :
CustomTabViewConfig<FixedWidthTextTabView> {
abstract fun getText(position: Int): String
abstract fun onVisibleTextViewInit(tv: TextView)
final override fun onTabInit(tabView: FixedWidthTextTabView, position: Int) {
val text = getText(position)
tabView.boundSizeTextView.text = text
tabView.boundSizeTextView.textSizePx = scale.onSelectTextSize
if(scale.switchBold){
tabView.boundSizeTextView.typeface = Typeface.DEFAULT_BOLD
}
tabView.dynamicSizeTextView.text = text
tabView.dynamicSizeTextView.textSizePx = scale.onUnSelectTextSize
onVisibleTextViewInit(tabView.dynamicSizeTextView)
}
final override fun getCustomView(context: Context): FixedWidthTextTabView {
return FixedWidthTextTabView(context)
}
}
data class TextScaleConfig(
val onSelectTextSize: Int,
val onUnSelectTextSize: Int,
val switchBold:Boolean = true
)
private val defaultScaleConfig = TextScaleConfig(18.dp, 16.dp)
abstract class TextScaleTabViewConfig(scale: TextScaleConfig = defaultScaleConfig) :
FixedWidthTextTabViewConfig(
scale
)
private fun TabLayout.addScaleAnim(config: TextScaleConfig = defaultScaleConfig) {
val key = R.id.tag_tabLayout_scale_ext
val lastListener = getTag(key)
if (lastListener != null) return
val duration = 50L
val animCacheMap = HashMap<TextView, ValueAnimator>()
fun cancelLastAnim(tv: TextView) {
animCacheMap[tv]?.cancel()
}
doOnDetach {
animCacheMap.forEach {
it.value.cancel()
}
}
val listener = object : TabLayout.OnTabSelectedListener {
private fun scaleTextSize(stv: ScaleTexViewTabView, size: Int, isBold: Boolean) {
cancelLastAnim(stv)
val animator =
ValueAnimator.ofInt(stv.textSize.toInt(), size)
animator.duration = duration
animator.addUpdateListener { anim ->
val textSize = anim.animatedValue as? Int
textSize?.let { ts ->
stv.textSizePx = size
}
}
animator.doOnStart {
stv.skipRequestLayout = true
}
animator.doOnEnd {
/*最后做一下修正*/
stv.textSizePx = size
if(config.switchBold){
stv.boldText = isBold
}
stv.skipRequestLayout = false
animCacheMap.remove(stv)
}
animCacheMap[stv] = animator
animator.start()
}
override fun onTabSelected(tab: TabLayout.Tab) {
val cusV = tab.customView
if (cusV is FixedWidthTextTabView) {
val onSelectTextSize = config.onSelectTextSize
val onUnSelectTextSize = config.onUnSelectTextSize
if (onSelectTextSize != onUnSelectTextSize) {
scaleTextSize(cusV.dynamicSizeTextView, onSelectTextSize, true)
}
}
}
override fun onTabUnselected(tab: TabLayout.Tab) {
val cusV = tab.customView
if (cusV is FixedWidthTextTabView) {
val onSelectTextSize = config.onSelectTextSize
val onUnSelectTextSize = config.onUnSelectTextSize
if (onSelectTextSize != onUnSelectTextSize) {
scaleTextSize(cusV.dynamicSizeTextView, onUnSelectTextSize, false)
}
}
}
override fun onTabReselected(tab: TabLayout.Tab) {
}
}
addOnTabSelectedListener(listener)
setTag(key, listener)
}
class ScaleTexViewTabView(context: Context) : AppCompatTextView(context) {
var skipRequestLayout = false
override fun requestLayout() {
if (!skipRequestLayout) {
super.requestLayout()
}
}
}
fun TabLayout.createMediator(
vp: ViewPager2,
onInit: (tab: TabLayout.Tab, position: Int) -> Unit
): TabLayoutMediator {
return TabLayoutMediator(this, vp) { tab, pos ->
onInit(tab, pos)
}
}
fun <T : View> TabLayout.createMediatorByCustomTabView(
vp: ViewPager2,
config: CustomTabViewConfig<T>
): TabLayoutMediator {
return createMediator(vp){tab,pos->
val tabView = config.getCustomView(tab.view.context)
tab.customView = tabView
config.onTabInit(tabView, pos)
}
}
fun TabLayout.createTextScaleMediatorByTextView(
vp: ViewPager2,
config: TextScaleTabViewConfig
): TabLayoutMediator {
addScaleAnim(config.scale)
return createMediatorByCustomTabView(vp, config)
}
fun TabLayout.addScaleTabByTextView(
textList: List<String>,
textColor: Int,
scale: TextScaleConfig = defaultScaleConfig
) {
removeAllTabs()
addScaleAnim(scale)
textList.forEach { text ->
val tab = newTab().apply {
val tabView = FixedWidthTextTabView(view.context)
customView = tabView
tabView.boundSizeTextView.text = text
tabView.boundSizeTextView.textSizePx = scale.onSelectTextSize
if(scale.switchBold){
//避免设置不必要的字体样式
tabView.boundSizeTextView.boldText = true
}
tabView.dynamicSizeTextView.text = text
tabView.dynamicSizeTextView.textSizePx = scale.onUnSelectTextSize
tabView.dynamicSizeTextView.setTextColor(textColor)
}
addTab(tab)
}
}