Skip to content

Commit ca43916

Browse files
committed
Add method for selectItem at runtime.
1 parent 8098c5d commit ca43916

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

app/src/main/java/com/iammert/readablebottombar/MainActivity.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package com.iammert.readablebottombar
22

33
import android.support.v7.app.AppCompatActivity
44
import android.os.Bundle
5-
import com.iammert.library.readablebottombar.ConfigurationXmlParser
65

76
class MainActivity : AppCompatActivity() {
87

98
override fun onCreate(savedInstanceState: Bundle?) {
109
super.onCreate(savedInstanceState)
1110
setContentView(R.layout.activity_main)
12-
1311
}
1412
}

library/src/main/java/com/iammert/library/readablebottombar/ReadableBottomBar.kt

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.util.AttributeSet
77
import android.view.View
88
import android.view.ViewTreeObserver
99
import android.widget.LinearLayout
10+
import java.lang.IllegalArgumentException
1011

1112
class ReadableBottomBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
1213
: LinearLayout(context, attrs, defStyleAttr) {
@@ -75,12 +76,12 @@ class ReadableBottomBar @JvmOverloads constructor(context: Context, attrs: Attri
7576

7677
bottomBarItemList = bottomBarItemConfigList.map { config ->
7778
BottomBarItem(
78-
config.index,
79-
config.text,
80-
textSize,
81-
textColor,
82-
config.drawable,
83-
activeItemType
79+
config.index,
80+
config.text,
81+
textSize,
82+
textColor,
83+
config.drawable,
84+
activeItemType
8485
)
8586
}
8687

@@ -105,10 +106,27 @@ class ReadableBottomBar @JvmOverloads constructor(context: Context, attrs: Attri
105106
this.itemSelectListener = itemSelectListener
106107
}
107108

109+
fun selectItem(index: Int) {
110+
if (index < 0 || index > bottomBarItemList.size) {
111+
throw IllegalArgumentException("Index should be in range of 0-${bottomBarItemList.size}")
112+
}
113+
114+
val item = bottomBarItemList[index]
115+
for (i in 0 until childCount) {
116+
if (TAG_CONTAINER == getChildAt(i).tag) {
117+
val selectedItemView = ((getChildAt(i) as LinearLayout).getChildAt(index) as BottomBarItemView)
118+
if (selectedItemView != currentSelectedView) {
119+
onSelected(item.index, selectedItemView)
120+
}
121+
}
122+
}
123+
}
124+
108125
private fun drawBottomBarItems() {
109126
val itemContainerLayout = LinearLayout(context).apply {
110127
layoutParams = LinearLayout.LayoutParams(layoutWidth.toInt(), layoutHeight.toInt())
111128
orientation = HORIZONTAL
129+
tag = TAG_CONTAINER
112130
}
113131

114132
bottomBarItemList.forEach { item ->
@@ -129,13 +147,7 @@ class ReadableBottomBar @JvmOverloads constructor(context: Context, attrs: Attri
129147
return@setOnClickListener
130148
}
131149

132-
animateIndicator(item.index)
133-
134-
currentSelectedView?.deselect()
135-
currentSelectedView = this
136-
currentSelectedView?.select()
137-
itemSelectListener?.onItemSelected(item.index)
138-
150+
onSelected(item.index, this)
139151
}
140152
}
141153

@@ -172,16 +184,28 @@ class ReadableBottomBar @JvmOverloads constructor(context: Context, attrs: Attri
172184
}
173185

174186
private fun animateIndicator(currentItemIndex: Int) {
175-
val previousMargin: Float = (indicatorView?.layoutParams as? LinearLayout.LayoutParams)?.leftMargin?.toFloat() ?: 0F
187+
val previousMargin: Float = (indicatorView?.layoutParams as? LinearLayout.LayoutParams)?.leftMargin?.toFloat()
188+
?: 0F
176189
val currentMargin: Float = currentItemIndex * itemWidth
177190
indicatorAnimator?.setFloatValues(previousMargin, currentMargin)
178191
indicatorAnimator?.start()
179192
}
180193

194+
private fun onSelected(index: Int, bottomBarItemView: BottomBarItemView) {
195+
animateIndicator(index)
196+
197+
currentSelectedView?.deselect()
198+
currentSelectedView = bottomBarItemView
199+
currentSelectedView?.select()
200+
itemSelectListener?.onItemSelected(index)
201+
}
202+
181203
companion object {
182204

183205
const val ANIMATION_DURATION = 300L
184206

207+
const val TAG_CONTAINER = "TAG_CONTAINER"
208+
185209
}
186210

187211
}

0 commit comments

Comments
 (0)