From 1c96f31e5580b00e4bde95cf4f742c6fcacaed53 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 3 Mar 2021 15:29:54 +0200 Subject: [PATCH] measure FAB requires that it is bottom-aligned to a supported layout fixes a problem when the FAB needs to be hidden before view are measured and laid out --- .../net/ktnx/mobileledger/ui/FabManager.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/FabManager.java b/app/src/main/java/net/ktnx/mobileledger/ui/FabManager.java index 76f7e049..a693a25a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/FabManager.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/FabManager.java @@ -23,8 +23,7 @@ import android.animation.TimeInterpolator; import android.annotation.SuppressLint; import android.content.Context; import android.view.MotionEvent; -import android.view.View; -import android.view.ViewParent; +import android.view.ViewGroup; import android.view.ViewPropertyAnimator; import androidx.annotation.NonNull; @@ -141,16 +140,20 @@ public class FabManager { private void calcVerticalFabOffset() { if (fabVerticalOffset > 0) return;// already calculated - int top = fab.getTop(); - ViewParent parent = fab.getParent(); - while (parent != null && !(parent instanceof View)) - parent = parent.getParent(); - - if (parent != null) { - View parentView = (View) parent; - int parentHeight = parentView.getHeight(); - fabVerticalOffset = parentHeight - top; - } + fab.measure(0, 0); + + int height = fab.getMeasuredHeight(); + + int bottomMargin; + + ViewGroup.LayoutParams layoutParams = fab.getLayoutParams(); + if (layoutParams instanceof ViewGroup.MarginLayoutParams) + bottomMargin = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin; + else + throw new RuntimeException("Unsupported layout params " + layoutParams.getClass() + .getCanonicalName()); + + fabVerticalOffset = height + bottomMargin; } public interface FabHandler { Context getContext(); -- 2.39.5