]> git.ktnx.net Git - mobile-ledger.git/commitdiff
measure FAB
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 3 Mar 2021 13:29:54 +0000 (15:29 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 3 Mar 2021 13:29:54 +0000 (15:29 +0200)
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

app/src/main/java/net/ktnx/mobileledger/ui/FabManager.java

index 76f7e049be00cb4b935503078125fed13153c50b..a693a25a1f18befad94acd84c6adf0a26b8b4406 100644 (file)
@@ -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();