Skip to content

Commit

Permalink
Add method for selectItem at runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
iammert committed Mar 20, 2019
1 parent 8098c5d commit ca43916
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package com.iammert.readablebottombar

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.iammert.library.readablebottombar.ConfigurationXmlParser

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.AttributeSet
import android.view.View
import android.view.ViewTreeObserver
import android.widget.LinearLayout
import java.lang.IllegalArgumentException

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

bottomBarItemList = bottomBarItemConfigList.map { config ->
BottomBarItem(
config.index,
config.text,
textSize,
textColor,
config.drawable,
activeItemType
config.index,
config.text,
textSize,
textColor,
config.drawable,
activeItemType
)
}

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

fun selectItem(index: Int) {
if (index < 0 || index > bottomBarItemList.size) {
throw IllegalArgumentException("Index should be in range of 0-${bottomBarItemList.size}")
}

val item = bottomBarItemList[index]
for (i in 0 until childCount) {
if (TAG_CONTAINER == getChildAt(i).tag) {
val selectedItemView = ((getChildAt(i) as LinearLayout).getChildAt(index) as BottomBarItemView)
if (selectedItemView != currentSelectedView) {
onSelected(item.index, selectedItemView)
}
}
}
}

private fun drawBottomBarItems() {
val itemContainerLayout = LinearLayout(context).apply {
layoutParams = LinearLayout.LayoutParams(layoutWidth.toInt(), layoutHeight.toInt())
orientation = HORIZONTAL
tag = TAG_CONTAINER
}

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

animateIndicator(item.index)

currentSelectedView?.deselect()
currentSelectedView = this
currentSelectedView?.select()
itemSelectListener?.onItemSelected(item.index)

onSelected(item.index, this)
}
}

Expand Down Expand Up @@ -172,16 +184,28 @@ class ReadableBottomBar @JvmOverloads constructor(context: Context, attrs: Attri
}

private fun animateIndicator(currentItemIndex: Int) {
val previousMargin: Float = (indicatorView?.layoutParams as? LinearLayout.LayoutParams)?.leftMargin?.toFloat() ?: 0F
val previousMargin: Float = (indicatorView?.layoutParams as? LinearLayout.LayoutParams)?.leftMargin?.toFloat()
?: 0F
val currentMargin: Float = currentItemIndex * itemWidth
indicatorAnimator?.setFloatValues(previousMargin, currentMargin)
indicatorAnimator?.start()
}

private fun onSelected(index: Int, bottomBarItemView: BottomBarItemView) {
animateIndicator(index)

currentSelectedView?.deselect()
currentSelectedView = bottomBarItemView
currentSelectedView?.select()
itemSelectListener?.onItemSelected(index)
}

companion object {

const val ANIMATION_DURATION = 300L

const val TAG_CONTAINER = "TAG_CONTAINER"

}

}

0 comments on commit ca43916

Please sign in to comment.