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
--- 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;