Skip to content

Commit

Permalink
Merge pull request #53 from st235/feature/show_hide
Browse files Browse the repository at this point in the history
implemented show and hide methods.
  • Loading branch information
st235 authored Sep 11, 2020
2 parents 3947d16 + 7f7934d commit 6af52df
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
vers = [
versionCode: 36,
versionName: "1.2.3"
versionCode: 37,
versionName: "1.2.4"
]
info = [
name: 'expandablebottombar',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package github.com.st235.lib_expandablebottombar

import android.animation.Animator
import android.annotation.TargetApi
import android.content.Context
import android.content.res.ColorStateList
Expand All @@ -24,6 +25,8 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.marginBottom
import github.com.st235.lib_expandablebottombar.ExpandableBottomBar.ItemStyle.Companion.toItemStyle
import github.com.st235.lib_expandablebottombar.behavior.ExpandableBottomBarBehavior
import github.com.st235.lib_expandablebottombar.parsers.ExpandableBottomBarParser
Expand Down Expand Up @@ -94,6 +97,8 @@ class ExpandableBottomBar @JvmOverloads constructor(
var onItemSelectedListener: OnItemClickListener? = null
var onItemReselectedListener: OnItemClickListener? = null

private var animator: Animator? = null

init {
initAttrs(context, attrs, defStyleAttr)
}
Expand Down Expand Up @@ -242,6 +247,38 @@ class ExpandableBottomBar @JvmOverloads constructor(
*/
fun getSelected(): ExpandableBottomBarMenuItem = viewControllers.getValue(selectedItemId).menuItem

/**
* Shows the bottom bar
*/
fun show() {
cancelRunningAnimation()

animator = AnimationHelper.translateViewTo(this, 0F)
animator?.start()
}

/**
* Hides the bottom bar
*/
fun hide() {
cancelRunningAnimation()

animator = AnimationHelper.translateViewTo(this, getMaxScrollDistance())
animator?.start()
}

private fun getMaxScrollDistance(): Float {
val childHeight = if (ViewCompat.isLaidOut(this)) height else measuredHeight
return childHeight.toFloat() + marginBottom
}

private fun cancelRunningAnimation() {
if (animator?.isRunning == true) {
animator?.cancel()
animator = null
}
}

private fun madeMenuItemsAccessible(items: List<ExpandableBottomBarMenuItem>) {
for ((i, item) in items.withIndex()) {
val prev = viewControllers[items.getOrNull(i - 1)?.itemId]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package github.com.st235.lib_expandablebottombar.behavior

import android.animation.ValueAnimator
import android.animation.Animator
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.View
import android.view.animation.DecelerateInterpolator
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import androidx.core.view.marginBottom
import github.com.st235.lib_expandablebottombar.utils.AnimationHelper
import github.com.st235.lib_expandablebottombar.utils.clamp
import kotlin.math.abs

Expand All @@ -19,7 +19,7 @@ class ExpandableBottomBarScrollableBehavior<V: View>:
private val handler = Handler(Looper.getMainLooper())
private var lastKnownRunnable: Runnable? = null

private var animator: ValueAnimator? = null
private var animator: Animator? = null
private var lastKnownDirection: Int? = null

constructor(): super()
Expand Down Expand Up @@ -88,14 +88,7 @@ class ExpandableBottomBarScrollableBehavior<V: View>:
}

cancelAnimation()

animator = ValueAnimator.ofFloat(child.translationY, translation)
animator?.interpolator = DecelerateInterpolator()
animator?.addUpdateListener { animator ->
val animatedValue = this.animator?.animatedValue as Float
child.translationY = animatedValue
}

animator = AnimationHelper.translateViewTo(child, translation)
animator?.start()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package github.com.st235.lib_expandablebottombar.utils

import android.animation.Animator
import android.animation.ValueAnimator
import android.view.View
import android.view.animation.DecelerateInterpolator

object AnimationHelper {

fun <T: View> translateViewTo(child: T, translation: Float): Animator {
val animator = ValueAnimator.ofFloat(child.translationY, translation)
animator.interpolator = DecelerateInterpolator()
animator.addUpdateListener { animator ->
val animatedValue = animator?.animatedValue as Float
child.translationY = animatedValue
}

return animator
}

}

0 comments on commit 6af52df

Please sign in to comment.