Skip to content

Commit 5f6930e

Browse files
wcshicketcham
authored andcommitted
Update demo fragment to support edge to edge.
Add convenience method to DemoUtils that will add a space to offset the bottom WindowInsets when needed. Updated DemoLandingFragment to call this utils method as well. PiperOrigin-RevId: 263622027
1 parent 674b462 commit 5f6930e

5 files changed

Lines changed: 42 additions & 21 deletions

File tree

catalog/java/io/material/catalog/feature/DemoFragment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public View onCreateView(
7373
initDemoActionBar();
7474
demoContainer.addView(onCreateDemoView(layoutInflater, viewGroup, bundle));
7575

76+
ViewGroup children = (ViewGroup) demoContainer.getChildAt(0);
77+
DemoUtils.addBottomSpaceInsetsIfNeeded(children, demoContainer);
7678
return view;
7779
}
7880

catalog/java/io/material/catalog/feature/DemoLandingFragment.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import androidx.fragment.app.Fragment;
3333
import androidx.core.view.MarginLayoutParamsCompat;
3434
import androidx.core.view.MenuItemCompat;
35-
import androidx.core.view.ViewCompat;
3635
import androidx.appcompat.app.AppCompatActivity;
3736
import androidx.appcompat.widget.Toolbar;
3837
import android.view.LayoutInflater;
@@ -41,7 +40,6 @@
4140
import android.view.MenuItem;
4241
import android.view.View;
4342
import android.view.ViewGroup;
44-
import android.view.ViewGroup.LayoutParams;
4543
import android.view.ViewGroup.MarginLayoutParams;
4644
import android.widget.TextView;
4745
import dagger.android.support.DaggerFragment;
@@ -102,16 +100,7 @@ public View onCreateView(
102100
}
103101
additionalDemosSection.setVisibility(additionalDemos.isEmpty() ? View.GONE : View.VISIBLE);
104102

105-
View spacer = view.findViewById(R.id.spacer);
106-
ViewCompat.setOnApplyWindowInsetsListener(
107-
viewGroup,
108-
(v, insets) -> {
109-
LayoutParams lp = spacer.getLayoutParams();
110-
lp.height = insets.getSystemWindowInsetBottom();
111-
spacer.setVisibility(View.VISIBLE);
112-
spacer.setLayoutParams(lp);
113-
return ViewCompat.onApplyWindowInsets(v, insets);
114-
});
103+
DemoUtils.addBottomSpaceInsetsIfNeeded((ViewGroup) view, viewGroup);
115104
return view;
116105
}
117106

catalog/java/io/material/catalog/feature/DemoUtils.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818

1919
import android.app.Activity;
2020
import com.google.android.material.snackbar.Snackbar;
21+
import androidx.core.view.ViewCompat;
22+
import androidx.core.widget.NestedScrollView;
2123
import android.view.MenuItem;
2224
import android.view.View;
2325
import android.view.ViewGroup;
26+
import android.widget.ScrollView;
27+
import android.widget.Space;
2428
import java.util.ArrayList;
2529
import java.util.List;
2630

@@ -55,4 +59,34 @@ public static boolean showSnackbar(Activity activity, MenuItem menuItem) {
5559
.show();
5660
return true;
5761
}
62+
63+
public static void addBottomSpaceInsetsIfNeeded(
64+
ViewGroup scrollableViewAncestor, ViewGroup viewGroupFitsSystemWindows) {
65+
List<ScrollView> scrollViews =
66+
DemoUtils.findViewsWithType(scrollableViewAncestor, ScrollView.class);
67+
List<NestedScrollView> nestedScrollViews =
68+
DemoUtils.findViewsWithType(scrollableViewAncestor, NestedScrollView.class);
69+
70+
ViewGroup scrollableContent = null;
71+
if (!scrollViews.isEmpty()) {
72+
scrollableContent = scrollViews.get(0);
73+
} else if (!nestedScrollViews.isEmpty()) {
74+
scrollableContent = nestedScrollViews.get(0);
75+
}
76+
77+
if (scrollableContent != null && scrollableContent.getChildAt(0) instanceof ViewGroup) {
78+
ViewGroup spaceParent = ((ViewGroup) scrollableContent.getChildAt(0));
79+
Space space = new Space(scrollableViewAncestor.getContext());
80+
space.setVisibility(View.GONE);
81+
spaceParent.addView(space);
82+
83+
ViewCompat.setOnApplyWindowInsetsListener(
84+
viewGroupFitsSystemWindows,
85+
(v, insets) -> {
86+
space.setVisibility(View.VISIBLE);
87+
space.getLayoutParams().height = insets.getSystemWindowInsetBottom();
88+
return insets;
89+
});
90+
}
91+
}
5892
}

catalog/java/io/material/catalog/feature/res/layout/cat_demo_fragment.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
<com.google.android.material.appbar.AppBarLayout
2424
android:layout_width="match_parent"
25-
android:layout_height="wrap_content">
25+
android:layout_height="wrap_content"
26+
android:fitsSystemWindows="true">
2627

2728
<androidx.appcompat.widget.Toolbar
2829
android:id="@+id/toolbar"
@@ -31,8 +32,9 @@
3132
android:layout_height="wrap_content"/>
3233
</com.google.android.material.appbar.AppBarLayout>
3334

34-
<FrameLayout
35+
<androidx.coordinatorlayout.widget.CoordinatorLayout
3536
android:id="@+id/cat_demo_fragment_container"
3637
android:layout_width="match_parent"
37-
android:layout_height="match_parent"/>
38+
android:layout_height="match_parent"
39+
android:fitsSystemWindows="true"/>
3840
</LinearLayout>

catalog/java/io/material/catalog/feature/res/layout/cat_demo_landing_fragment.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,6 @@
8888
</LinearLayout>
8989

9090
<include layout="@layout/cat_demo_landing_links_section"/>
91-
92-
<Space
93-
android:id="@+id/spacer"
94-
android:layout_height="0dp"
95-
android:layout_width="match_parent"
96-
android:visibility="gone"/>
9791
</LinearLayout>
9892
</androidx.core.widget.NestedScrollView>
9993

0 commit comments

Comments
 (0)