-
Notifications
You must be signed in to change notification settings - Fork 4
/
BottomBar.kt
233 lines (192 loc) · 6.92 KB
/
BottomBar.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.felixsoares.animatedbottombar.ui
import android.content.Context
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.widget.LinearLayout
import com.felixsoares.animatedbottombar.NavigationListner
import com.felixsoares.animatedbottombar.R
import com.felixsoares.animatedbottombar.model.Item
import com.felixsoares.animatedbottombar.util.Util
import com.transitionseverywhere.TransitionManager
class BottomBar : LinearLayout {
private var mListner: NavigationListner? = null
private var mItens = mutableListOf<Item>()
private var textColor = 0
private var textSize = 0f
private var bgColor = Color.WHITE
private var bgIconColor = Color.WHITE
private var bgIndicatorColor = Color.BLACK
private var tintIconColor = Color.BLACK
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
applyCustomLayout(context, attrs)
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
applyCustomLayout(context, attrs)
}
private fun applyCustomLayout(context: Context, attrs: AttributeSet) {
val typedArray = context.theme.obtainStyledAttributes(
attrs,
R.styleable.BottomBar, 0, 0
)
try {
this.textColor = typedArray.getColor(
R.styleable.BottomBar_text_color,
ContextCompat.getColor(context, android.R.color.black)
)
this.textSize = typedArray.getDimension(R.styleable.BottomBar_text_size, 12f)
this.bgColor = typedArray.getColor(
R.styleable.BottomBar_bg_color,
Color.WHITE
)
bgIconColor = typedArray.getColor(
R.styleable.BottomBar_bg_icon_color,
Color.WHITE
)
bgIndicatorColor = typedArray.getColor(
R.styleable.BottomBar_indicator_color,
Color.BLACK
)
tintIconColor = typedArray.getColor(
R.styleable.BottomBar_tint_icon_color,
Color.BLACK
)
setBackgroundColor(bgColor)
} finally {
typedArray.recycle()
}
}
private fun init() {
this.orientation = VERTICAL
}
fun setupListner(listner: NavigationListner): BottomBar {
this.mListner = listner
return this
}
fun setupItens(itens: MutableList<Item>): BottomBar {
this.mItens = itens
return this
}
fun addItem(item: Item): BottomBar {
this.mItens.add(item)
return this
}
fun build() {
when {
mItens.isEmpty() -> {
throw Exception("Navigation items should not be empty.")
}
this.mItens.size < 3 -> {
throw Exception("Don't use minus than three destinations. You should work with tabs.")
}
this.mItens.size > 5 -> {
throw Exception("Don’t use more than five itens.")
}
else -> {
createNavigationItems()
}
}
}
fun setTextColor(textColor: Int): BottomBar {
this.textColor = textColor
return this
}
fun setTextSize(textSize: Float): BottomBar {
this.textSize = textSize
return this
}
fun setBgColor(bgColor: Int): BottomBar {
this.bgColor = bgColor
return this
}
fun setBgIconColor(bgIconColor: Int): BottomBar {
this.bgIconColor = bgIconColor
return this
}
fun setIconColor(tintIconColor: Int): BottomBar {
this.tintIconColor = tintIconColor
return this
}
fun setIndicatorColor(bgIndicatorColor: Int): BottomBar {
this.bgIndicatorColor = bgIndicatorColor
return this
}
private fun createNavigationItems() {
val indicatorParams = LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, Util.dpToPx(2))
val llIndicator = LinearLayout(context)
llIndicator.orientation = HORIZONTAL
llIndicator.layoutParams = indicatorParams
val navParams = LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
val llNav = LinearLayout(context)
llNav.orientation = HORIZONTAL
llNav.layoutParams = navParams
createIndicator(mItens.size, llIndicator)
for (i in 0..(mItens.size - 1)) {
createItens(mItens[i], llNav, i)
}
addView(llIndicator)
addView(llNav)
}
private fun createItens(item: Item, llNav: LinearLayout, position: Int) {
val navi = NavItem(context).init(
context,
item,
textColor,
textSize,
bgColor,
bgIconColor,
tintIconColor
)
navi.setOnClickListener {
moveIndicator(position, mItens.size)
closeAll()
navi.show()
mListner?.OnClick(position)
}
if (position == 0) {
navi.show()
}
llNav.addView(navi)
}
private fun closeAll() {
val nav = getChildAt(1) as LinearLayout
for (i in 0..(nav.childCount - 1)) {
val item = nav.getChildAt(i) as NavItem
if (item.statusOpened) {
item.close()
}
}
}
private fun createIndicator(size: Int, llIndicator: LinearLayout) {
val lfParams = LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 0f)
val leftFlank = LinearLayout(context)
leftFlank.layoutParams = lfParams
val indicatorParams = LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1f)
val indicator = LinearLayout(context)
indicator.layoutParams = indicatorParams
indicator.setBackgroundColor(bgIndicatorColor)
val lrParams = LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, (size - 1).toFloat())
val rightFlank = LinearLayout(context)
rightFlank.layoutParams = lrParams
llIndicator.addView(leftFlank)
llIndicator.addView(indicator)
llIndicator.addView(rightFlank)
}
private fun moveIndicator(position: Int, size: Int) {
val llIndicator = this.getChildAt(0) as LinearLayout
val leftFlank = llIndicator.getChildAt(0) as LinearLayout
val rightFlank = llIndicator.getChildAt(2) as LinearLayout
val llparams = leftFlank.layoutParams as LinearLayout.LayoutParams
val rlparams = rightFlank.layoutParams as LinearLayout.LayoutParams
llparams.weight = position.toFloat()
rlparams.weight = (size - position - 1).toFloat()
TransitionManager.beginDelayedTransition(llIndicator)
leftFlank.layoutParams = llparams
rightFlank.layoutParams = rlparams
}
}