Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.graphics.Rect; |
| 5 | import android.util.AttributeSet; |
| 6 | import android.view.View; |
| 7 | import android.view.ViewGroup; |
| 8 | import android.widget.FrameLayout; |
| 9 | |
| 10 | public class InsettableFrameLayout extends FrameLayout implements |
| 11 | ViewGroup.OnHierarchyChangeListener, Insettable { |
| 12 | |
| 13 | protected Rect mInsets = new Rect(); |
| 14 | |
| 15 | public InsettableFrameLayout(Context context, AttributeSet attrs) { |
| 16 | super(context, attrs); |
| 17 | setOnHierarchyChangeListener(this); |
| 18 | } |
| 19 | |
| 20 | public void setFrameLayoutChildInsets(View child, Rect newInsets, Rect oldInsets) { |
| 21 | final FrameLayout.LayoutParams flp = (FrameLayout.LayoutParams) child.getLayoutParams(); |
| 22 | if (child instanceof Insettable) { |
| 23 | ((Insettable) child).setInsets(newInsets); |
| 24 | } else { |
| 25 | flp.topMargin += (newInsets.top - oldInsets.top); |
| 26 | flp.leftMargin += (newInsets.left - oldInsets.left); |
| 27 | flp.rightMargin += (newInsets.right - oldInsets.right); |
| 28 | flp.bottomMargin += (newInsets.bottom - oldInsets.bottom); |
| 29 | } |
| 30 | child.setLayoutParams(flp); |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public void setInsets(Rect insets) { |
| 35 | final int n = getChildCount(); |
| 36 | for (int i = 0; i < n; i++) { |
| 37 | final View child = getChildAt(i); |
| 38 | setFrameLayoutChildInsets(child, insets, mInsets); |
| 39 | } |
| 40 | mInsets.set(insets); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public void onChildViewAdded(View parent, View child) { |
| 45 | setFrameLayoutChildInsets(child, mInsets, new Rect()); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void onChildViewRemoved(View parent, View child) { |
| 50 | } |
| 51 | |
| 52 | } |