Skip to content

Commit

Permalink
Fix FAB demos in edge to edge
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 264448758
  • Loading branch information
ymarian authored and wcshi committed Aug 26, 2019
1 parent bf933c0 commit 0396311
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="192dp">
android:layout_height="192dp"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
style="?attr/catalogToolbarStyle"
android:layout_width="match_parent"
Expand All @@ -41,7 +42,7 @@
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:title="Collapsing Title"/>
app:title="Collapsing Title" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

Expand All @@ -50,7 +51,7 @@
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<include layout="@layout/cat_topappbar_filler_text_view"/>
<include layout="@layout/cat_topappbar_filler_text_view" />
</androidx.core.widget.NestedScrollView>

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
Expand All @@ -61,6 +62,6 @@
android:text="@string/extended_fab_label"
app:icon="@drawable/ic_add_24px"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|right|end"/>
app:layout_anchorGravity="bottom|right|end" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@
android:layout_margin="8dp"
android:text="@string/shrink_fabs_label"/>
</LinearLayout>

<io.material.catalog.feature.BottomWindowInsetView
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@
android:layout_marginBottom="8dp"
android:text="@string/rotate_fabs_label"/>

<io.material.catalog.feature.BottomWindowInsetView
android:layout_width="wrap_content"
android:layout_height="0dp"
/>

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.material.catalog.feature;


import android.content.Context;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
* A View that measures itself to be as tall as the bottom window inset.
*/
public class BottomWindowInsetView extends View {

private int systemWindowInsetBottom;

public BottomWindowInsetView(Context context) {
this(context, null);
}

public BottomWindowInsetView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public BottomWindowInsetView(Context context, @Nullable AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
super.setVisibility(GONE);
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
ViewGroup parent = (ViewGroup) getParent();
while (parent != null && !ViewCompat.getFitsSystemWindows(parent)) {
parent = (ViewGroup) parent.getParent();
}

if (parent == null) {
return;
}

ViewCompat.setOnApplyWindowInsetsListener(
parent,
(v, insets) -> {
systemWindowInsetBottom = insets.getSystemWindowInsetBottom();
super.setVisibility(systemWindowInsetBottom == 0 ? GONE : VISIBLE);
if (systemWindowInsetBottom > 0) {
requestLayout();
}

return insets;
});
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(width, systemWindowInsetBottom);
}

/**
* Throws {@link UnsupportedOperationException} if called. Let the view handle its own visibility.
*/
@Override
public void setVisibility(int visibility) {
throw new UnsupportedOperationException("don't call setVisibility on BottomWindowInsetView");
}
}
2 changes: 1 addition & 1 deletion catalog/java/io/material/catalog/feature/DemoFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public View onCreateView(
demoContainer.addView(onCreateDemoView(layoutInflater, viewGroup, bundle));

ViewGroup children = (ViewGroup) demoContainer.getChildAt(0);
DemoUtils.addBottomSpaceInsetsIfNeeded(children, demoContainer);
DemoUtils.addBottomSpaceInsetsIfNeeded(children);
return view;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public View onCreateView(
}
additionalDemosSection.setVisibility(additionalDemos.isEmpty() ? View.GONE : View.VISIBLE);

DemoUtils.addBottomSpaceInsetsIfNeeded((ViewGroup) view, viewGroup);
DemoUtils.addBottomSpaceInsetsIfNeeded((ViewGroup) view);
return view;
}

Expand Down
82 changes: 59 additions & 23 deletions catalog/java/io/material/catalog/feature/DemoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
package io.material.catalog.feature;

import android.app.Activity;
import android.content.Context;
import com.google.android.material.snackbar.Snackbar;
import androidx.core.view.ViewCompat;
import androidx.core.widget.NestedScrollView;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLayoutChangeListener;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.Space;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -54,39 +55,74 @@ public static boolean showSnackbar(Activity activity, MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
return false;
}

Snackbar.make(
activity.findViewById(android.R.id.content), menuItem.getTitle(), Snackbar.LENGTH_SHORT)
.show();
return true;
}

public static void addBottomSpaceInsetsIfNeeded(
ViewGroup scrollableViewAncestor, ViewGroup viewGroupFitsSystemWindows) {
List<ScrollView> scrollViews =
public static void addBottomSpaceInsetsIfNeeded(ViewGroup scrollableViewAncestor) {
List<? extends ViewGroup> scrollViews =
DemoUtils.findViewsWithType(scrollableViewAncestor, ScrollView.class);
List<NestedScrollView> nestedScrollViews =
DemoUtils.findViewsWithType(scrollableViewAncestor, NestedScrollView.class);

ViewGroup scrollableContent = null;
if (!scrollViews.isEmpty()) {
scrollableContent = scrollViews.get(0);
} else if (!nestedScrollViews.isEmpty()) {
scrollableContent = nestedScrollViews.get(0);
}

if (scrollableContent != null && scrollableContent.getChildAt(0) instanceof ViewGroup) {
ViewGroup spaceParent = ((ViewGroup) scrollableContent.getChildAt(0));
Space space = new Space(scrollableViewAncestor.getContext());
space.setVisibility(View.GONE);
spaceParent.addView(space);
List<? extends ViewGroup> nestedScrollViews = DemoUtils
.findViewsWithType(scrollableViewAncestor, NestedScrollView.class);

ArrayList<ViewGroup> scrollingViews = new ArrayList<>();
scrollingViews.addAll(scrollViews);
scrollingViews.addAll(nestedScrollViews);
for (ViewGroup scrollView : scrollingViews) {
ViewCompat.setOnApplyWindowInsetsListener(
viewGroupFitsSystemWindows,
(v, insets) -> {
space.setVisibility(View.VISIBLE);
space.getLayoutParams().height = insets.getSystemWindowInsetBottom();
scrollableViewAncestor,
(view, insets) -> {
scrollView.addOnLayoutChangeListener(
new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
scrollView.removeOnLayoutChangeListener(this);
int systemWindowInsetBottom = insets.getSystemWindowInsetBottom();
if (!shouldApplyBottomInset(scrollView, systemWindowInsetBottom)) {
return;
}

int insetBottom = calculateBottomInset(scrollView, systemWindowInsetBottom);
View scrollableContent = scrollView.getChildAt(0);
scrollableContent.setPadding(
scrollableContent.getPaddingLeft(),
scrollableContent.getPaddingTop(),
scrollableContent.getPaddingRight(),
insetBottom);
}
});
return insets;
});
}
);
}
}

private static int calculateBottomInset(ViewGroup scrollView, int systemWindowInsetBottom) {
View scrollableContent = scrollView.getChildAt(0);
int calculatedInset = Math.min(
systemWindowInsetBottom,
scrollableContent.getHeight() + systemWindowInsetBottom - scrollView.getHeight());
return Math.max(calculatedInset, 0);
}

private static boolean shouldApplyBottomInset(ViewGroup scrollView, int systemWindowInsetBottom) {
View scrollableContent = scrollView.getChildAt(0);
int scrollableContentHeight = scrollableContent.getHeight();
int scrollViewHeight = scrollView.getHeight();
int[] scrollViewLocation = new int[2];
scrollView.getLocationOnScreen(scrollViewLocation);
Context context = scrollView.getContext();

return scrollViewHeight + scrollViewLocation[1] >= getContentViewHeight((Activity) context)
&& scrollableContentHeight + systemWindowInsetBottom >= scrollViewHeight;
}

private static int getContentViewHeight(Activity context) {
return context.findViewById(android.R.id.content).getHeight();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
android:layout_height="wrap_content"/>
</com.google.android.material.appbar.AppBarLayout>

<androidx.coordinatorlayout.widget.CoordinatorLayout
<FrameLayout
android:id="@+id/cat_demo_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"/>
android:layout_height="match_parent"/>
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarlayout"
Expand Down

0 comments on commit 0396311

Please sign in to comment.