|
22 | 22 |
|
23 | 23 | import android.content.Context; |
24 | 24 | import android.content.res.ColorStateList; |
| 25 | +import android.graphics.Rect; |
25 | 26 | import androidx.appcompat.widget.TintTypedArray; |
26 | 27 | import android.util.AttributeSet; |
27 | 28 | import android.util.Log; |
@@ -59,6 +60,11 @@ public class FloatingToolbarLayout extends FrameLayout { |
59 | 60 | private boolean marginTopSystemWindowInsets; |
60 | 61 | private boolean marginRightSystemWindowInsets; |
61 | 62 | private boolean marginBottomSystemWindowInsets; |
| 63 | + private Rect originalMargins; |
| 64 | + private int bottomMarginWindowInset; |
| 65 | + private int topMarginWindowInset; |
| 66 | + private int leftMarginWindowInset; |
| 67 | + private int rightMarginWindowInset; |
62 | 68 |
|
63 | 69 | public FloatingToolbarLayout(@NonNull Context context) { |
64 | 70 | this(context, null); |
@@ -115,46 +121,73 @@ public FloatingToolbarLayout( |
115 | 121 | @Override |
116 | 122 | public WindowInsetsCompat onApplyWindowInsets( |
117 | 123 | @NonNull View v, @NonNull WindowInsetsCompat insets) { |
118 | | - if (!marginLeftSystemWindowInsets && !marginRightSystemWindowInsets |
119 | | - && !marginTopSystemWindowInsets && !marginBottomSystemWindowInsets) { |
| 124 | + if (!marginLeftSystemWindowInsets |
| 125 | + && !marginRightSystemWindowInsets |
| 126 | + && !marginTopSystemWindowInsets |
| 127 | + && !marginBottomSystemWindowInsets) { |
120 | 128 | return insets; |
121 | 129 | } |
122 | 130 | Insets systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()); |
123 | 131 | Insets cutoutInsets = insets.getInsets(WindowInsetsCompat.Type.displayCutout()); |
124 | | - int bottomInset = systemBarInsets.bottom + cutoutInsets.bottom; |
125 | | - int topInset = systemBarInsets.top + cutoutInsets.top; |
126 | | - int rightInset = systemBarInsets.right + cutoutInsets.right; |
127 | | - int leftInset = systemBarInsets.left + cutoutInsets.left; |
128 | | - |
129 | | - ViewGroup.LayoutParams lp = getLayoutParams(); |
130 | | - if (!(lp instanceof MarginLayoutParams)) { |
131 | | - Log.w(TAG, "Unable to update margins because layout params are not MarginLayoutParams"); |
132 | | - return insets; |
133 | | - } |
134 | | - |
135 | | - MarginLayoutParams marginLp = (MarginLayoutParams) lp; |
136 | | - |
137 | | - if (marginLeftSystemWindowInsets) { |
138 | | - marginLp.leftMargin += leftInset; |
139 | | - } |
140 | | - |
141 | | - if (marginRightSystemWindowInsets) { |
142 | | - marginLp.rightMargin += rightInset; |
143 | | - } |
144 | | - |
145 | | - if (marginTopSystemWindowInsets) { |
146 | | - marginLp.topMargin += topInset; |
147 | | - } |
| 132 | + bottomMarginWindowInset = systemBarInsets.bottom + cutoutInsets.bottom; |
| 133 | + topMarginWindowInset = systemBarInsets.top + cutoutInsets.top; |
| 134 | + rightMarginWindowInset = systemBarInsets.right + cutoutInsets.right; |
| 135 | + leftMarginWindowInset = systemBarInsets.left + cutoutInsets.left; |
148 | 136 |
|
149 | | - if (marginBottomSystemWindowInsets) { |
150 | | - marginLp.bottomMargin += bottomInset; |
151 | | - } |
| 137 | + updateMargins(); |
152 | 138 |
|
153 | | - requestLayout(); |
154 | 139 | return insets; |
155 | 140 | } |
156 | 141 | }); |
157 | 142 |
|
158 | 143 | attributes.recycle(); |
159 | 144 | } |
| 145 | + |
| 146 | + private void updateMargins() { |
| 147 | + ViewGroup.LayoutParams lp = getLayoutParams(); |
| 148 | + if (originalMargins == null) { |
| 149 | + Log.w(TAG, "Unable to update margins because original view margins are not set"); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + int newLeftMargin = |
| 154 | + originalMargins.left + (marginLeftSystemWindowInsets ? leftMarginWindowInset : 0); |
| 155 | + int newRightMargin = |
| 156 | + originalMargins.right + (marginRightSystemWindowInsets ? rightMarginWindowInset : 0); |
| 157 | + int newTopMargin = |
| 158 | + originalMargins.top + (marginTopSystemWindowInsets ? topMarginWindowInset : 0); |
| 159 | + int newBottomMargin = |
| 160 | + originalMargins.bottom + (marginBottomSystemWindowInsets ? bottomMarginWindowInset : 0); |
| 161 | + |
| 162 | + MarginLayoutParams marginLp = (MarginLayoutParams) lp; |
| 163 | + boolean marginChanged = |
| 164 | + marginLp.bottomMargin != newBottomMargin |
| 165 | + || marginLp.leftMargin != newLeftMargin |
| 166 | + || marginLp.rightMargin != newRightMargin |
| 167 | + || marginLp.topMargin != newTopMargin; |
| 168 | + if (marginChanged) { |
| 169 | + marginLp.bottomMargin = newBottomMargin; |
| 170 | + marginLp.leftMargin = newLeftMargin; |
| 171 | + marginLp.rightMargin = newRightMargin; |
| 172 | + marginLp.topMargin = newTopMargin; |
| 173 | + requestLayout(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void setLayoutParams(ViewGroup.LayoutParams params) { |
| 179 | + super.setLayoutParams(params); |
| 180 | + if (params instanceof MarginLayoutParams) { |
| 181 | + MarginLayoutParams marginParams = (MarginLayoutParams) params; |
| 182 | + originalMargins = |
| 183 | + new Rect( |
| 184 | + marginParams.leftMargin, |
| 185 | + marginParams.topMargin, |
| 186 | + marginParams.rightMargin, |
| 187 | + marginParams.bottomMargin); |
| 188 | + updateMargins(); |
| 189 | + } else { |
| 190 | + originalMargins = null; |
| 191 | + } |
| 192 | + } |
160 | 193 | } |
0 commit comments