From d179e74026c8d7979e88ee17a2e2cf39dfd0d4bd Mon Sep 17 00:00:00 2001 From: Constantine Date: Sun, 10 Feb 2019 23:09:59 +0300 Subject: [PATCH] Fix problem with jumping of backLayout --- .../backdrop/BackdropBehavior.kt | 47 +++++++++++++------ .../semper_viventem/backdrop/BackdropUtils.kt | 2 +- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/backdrop/src/main/kotlin/ru/semper_viventem/backdrop/BackdropBehavior.kt b/backdrop/src/main/kotlin/ru/semper_viventem/backdrop/BackdropBehavior.kt index 93d24b8..08b3f88 100644 --- a/backdrop/src/main/kotlin/ru/semper_viventem/backdrop/BackdropBehavior.kt +++ b/backdrop/src/main/kotlin/ru/semper_viventem/backdrop/BackdropBehavior.kt @@ -45,6 +45,8 @@ class BackdropBehavior : CoordinatorLayout.Behavior { private var dropState: DropState = DEFAULT_DROP_STATE + private var needToInitializing = true + private var dropListeners = mutableListOf() constructor() : super() @@ -91,7 +93,7 @@ class BackdropBehavior : CoordinatorLayout.Behavior { } } - if (toolbar != null && frontLayout != null && backLayout != null) { + if (toolbar != null && frontLayout != null && backLayout != null && needToInitializing) { initViews(parent, frontLayout!!, toolbar!!, backLayout!!) } @@ -142,7 +144,7 @@ class BackdropBehavior : CoordinatorLayout.Behavior { } else { dropState = DropState.OPEN if (backLayout != null && toolbar != null && frontLayout != null) { - drawDropState(backLayout!!, toolbar!!, frontLayout!!, withAnimation) + drawDropState(frontLayout!!, toolbar!!, backLayout!!, withAnimation) } else { throw IllegalArgumentException("Toolbar and backContainer must be initialized") } @@ -155,7 +157,7 @@ class BackdropBehavior : CoordinatorLayout.Behavior { } else { dropState = DropState.CLOSE if (backLayout != null && toolbar != null && frontLayout != null) { - drawDropState(backLayout!!, toolbar!!, frontLayout!!, withAnimation) + drawDropState(frontLayout!!, toolbar!!, backLayout!!, withAnimation) } else { throw IllegalArgumentException("Toolbar and backContainer must be initialized") } @@ -165,12 +167,14 @@ class BackdropBehavior : CoordinatorLayout.Behavior { private fun initViews(parent: CoordinatorLayout, frontLayout: View, toolbar: Toolbar, backLayout: View) { - // TODO (next release): remove this conditional + // TODO (next release): remove this block if (toolbarId != null) { backLayout.y = toolbar.y + toolbar.height + frontLayout.layoutParams.height = parent.height - toolbar.height + } else { + frontLayout.layoutParams.height = parent.height - (backLayout.y.toInt() + toolbar.y.toInt() + toolbar.height) } - frontLayout.layoutParams.height = parent.height - (toolbar.y.toInt() + toolbar.height) drawDropState(frontLayout, toolbar, backLayout, false) with(toolbar) { @@ -183,33 +187,48 @@ class BackdropBehavior : CoordinatorLayout.Behavior { notifyListeners(true) } } + + needToInitializing = false } - private fun drawDropState(child: View, toolbar: Toolbar, backContainer: View, withAnimation: Boolean = true) { + private fun drawDropState(frontLayout: View, toolbar: Toolbar, backContainer: View, withAnimation: Boolean = true) { when (dropState) { DropState.CLOSE -> { - drawClosedState(child, backContainer, withAnimation) + drawClosedState(frontLayout, backContainer, toolbar, withAnimation) toolbar.setNavigationIcon(closedIconId) } DropState.OPEN -> { - drawOpenedState(child, backContainer, withAnimation) + drawOpenedState(frontLayout, backContainer, withAnimation) toolbar.setNavigationIcon(openedIconRes) } } } - private fun drawClosedState(child: View, backContainer: View, withAnimation: Boolean = true) { - val position = backContainer.y + private fun drawClosedState(frontLayout: View, backLayout: View, toolbar: Toolbar, withAnimation: Boolean = true) { + val position = calculateTopPosition(backLayout, toolbar) val duration = if (withAnimation) DEFAULT_DURATION else WITHOUT_DURATION - child.animate().y(position).setDuration(duration).start() + frontLayout.animate().y(position).setDuration(duration).start() } - private fun drawOpenedState(child: View, backContainer: View, withAnimation: Boolean = true) { - val position = backContainer.y + backContainer.height + private fun drawOpenedState(frontLayout: View, backLayout: View, withAnimation: Boolean = true) { + val position = calculateBottomPosition(backLayout) val duration = if (withAnimation) DEFAULT_DURATION else WITHOUT_DURATION - child.animate().y(position).setDuration(duration).start() + frontLayout.animate().y(position).setDuration(duration).start() + } + + private fun calculateTopPosition(backLayout: View, toolbar: Toolbar): Float { + // TODO (next release): remove this block + return if (toolbarId != null) { + backLayout.y + } else { + (backLayout.y + toolbar.y + toolbar.height) + } + } + + private fun calculateBottomPosition(backLayout: View): Float { + return backLayout.y + backLayout.height } private fun notifyListeners(fromUser: Boolean) { diff --git a/backdrop/src/main/kotlin/ru/semper_viventem/backdrop/BackdropUtils.kt b/backdrop/src/main/kotlin/ru/semper_viventem/backdrop/BackdropUtils.kt index 94f008d..f69d5f5 100644 --- a/backdrop/src/main/kotlin/ru/semper_viventem/backdrop/BackdropUtils.kt +++ b/backdrop/src/main/kotlin/ru/semper_viventem/backdrop/BackdropUtils.kt @@ -3,7 +3,7 @@ package ru.semper_viventem.backdrop import android.view.ViewGroup import androidx.appcompat.widget.Toolbar -class BackdropUtils { +internal class BackdropUtils { fun findToolbar(viewGroup: ViewGroup): Toolbar? { for (chileId in 0..viewGroup.childCount) {