Bug 1396076 - Part 3 - Switch to a LayoutChangeListener for setting up the delegates. r?jwu draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Thu, 07 Sep 2017 22:26:57 +0200
changeset 660900 042b85437a41b5e293531e23df5194d197680558
parent 660899 cdbee38d6078ab4c6195a1605b752f49265cad67
child 660901 1f46ce8e17acb4c03be9b80569a623729c25951f
child 661534 6a8ad05d79623e12f30e99c55140767b1241b258
push id78601
push usermozilla@buttercookie.de
push dateThu, 07 Sep 2017 20:40:44 +0000
reviewersjwu
bugs1396076
milestone57.0a1
Bug 1396076 - Part 3 - Switch to a LayoutChangeListener for setting up the delegates. r?jwu When the device is rotated between portrait and landscape mode (or vice versa), the View's final size is not yet available during the first OnPreDrawListener call for some of the TabsLayoutItemViews. It would be possible to skip the listener removal, so this is no longer used as a one-shot listener, however onPreDraw is frequently called even when the View's dimensions haven't changed. Therefore, we switch this to an OnLayoutChangeListener, which is more appropriate for what we need. MozReview-Commit-ID: 6JAzXdEBxbL
mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayoutItemView.java
--- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayoutItemView.java
+++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayoutItemView.java
@@ -16,17 +16,16 @@ import android.content.Context;
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
 import android.support.v4.widget.TextViewCompat;
 import android.support.v7.widget.ViewUtils;
 import android.util.AttributeSet;
 import android.util.TypedValue;
 import android.view.MotionEvent;
 import android.view.View;
-import android.view.ViewTreeObserver;
 import android.widget.Checkable;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
 public class TabsLayoutItemView extends LinearLayout
                                 implements Checkable {
     private static final String LOGTAG = "Gecko" + TabsLayoutItemView.class.getSimpleName();
@@ -99,31 +98,27 @@ public class TabsLayoutItemView extends 
         mThumbnail = (TabsPanelThumbnailView) findViewById(R.id.thumbnail);
         mCloseButton = (ImageView) findViewById(R.id.close);
         mThumbnailWrapper = (TabThumbnailWrapper) findViewById(R.id.wrapper);
 
         growCloseButtonHitArea();
     }
 
     private void growCloseButtonHitArea() {
-        getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
+        addOnLayoutChangeListener(new OnLayoutChangeListener() {
             @Override
-            public boolean onPreDraw() {
-                getViewTreeObserver().removeOnPreDrawListener(this);
-
+            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                 // Ideally we want the close button hit area to be 40x40dp but we are constrained by the height of the parent, so
                 // we make it as tall as the parent view and 40dp across.
-                final int targetHitArea = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());;
+                final int targetHitArea = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
 
                 final Rect hitRect = getHitRectRelatively(targetHitArea);
 
                 setTouchDelegate(new TouchDelegateWithReset(hitRect, mCloseButton));
                 setHoverDelegate(new HoverDelegateWithReset(hitRect, mCloseButton));
-
-                return true;
             }
         });
     }
 
     private Rect getHitRectRelatively(int targetHitArea) {
         final boolean isRtl = ViewUtils.isLayoutRtl(this);
         final Rect hitRect = new Rect();
         hitRect.top = 0;