From 2c539524ec62713028e9521ca7b80c38c6aabf9c Mon Sep 17 00:00:00 2001 From: manabu-nakamura Date: Mon, 16 Oct 2023 10:08:09 -0700 Subject: [PATCH] [Catalog][BottomSheet] Use BottomSheetDialogFragment Resolves https://github.com/material-components/material-components-android/pull/3601 Resolves https://github.com/material-components/material-components-android/issues/3600 GIT_ORIGIN_REV_ID=2c857675e53ee226eb46e6fcb5a77c68bb5ea4cc PiperOrigin-RevId: 573848332 --- ...tomSheetScrollableContentDemoFragment.java | 68 ++++++++++--------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/catalog/java/io/material/catalog/bottomsheet/BottomSheetScrollableContentDemoFragment.java b/catalog/java/io/material/catalog/bottomsheet/BottomSheetScrollableContentDemoFragment.java index ab72efdff78..0edc178ce14 100644 --- a/catalog/java/io/material/catalog/bottomsheet/BottomSheetScrollableContentDemoFragment.java +++ b/catalog/java/io/material/catalog/bottomsheet/BottomSheetScrollableContentDemoFragment.java @@ -18,16 +18,19 @@ import io.material.catalog.R; +import android.app.Dialog; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.LayoutRes; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.android.material.bottomsheet.BottomSheetDialog; +import com.google.android.material.bottomsheet.BottomSheetDialogFragment; import com.google.android.material.internal.ViewUtils; import io.material.catalog.feature.DemoFragment; import io.material.catalog.windowpreferences.WindowPreferencesManager; @@ -37,44 +40,12 @@ * app. */ public class BottomSheetScrollableContentDemoFragment extends DemoFragment { - - private WindowPreferencesManager windowPreferencesManager; - - @Override - public void onCreate(@Nullable Bundle bundle) { - super.onCreate(bundle); - windowPreferencesManager = new WindowPreferencesManager(getContext()); - } - - @SuppressWarnings("RestrictTo") @Override public View onCreateDemoView( LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) { View view = layoutInflater.inflate(getDemoContent(), viewGroup, false /* attachToRoot */); - - // Set up BottomSheetDialog - BottomSheetDialog bottomSheetDialog = - new BottomSheetDialog( - getContext(), R.style.ThemeOverlay_Catalog_BottomSheetDialog_Scrollable); - windowPreferencesManager.applyEdgeToEdgePreference(bottomSheetDialog.getWindow()); - bottomSheetDialog.setContentView(R.layout.cat_bottomsheet_scrollable_content); - View bottomSheetInternal = bottomSheetDialog.findViewById(R.id.design_bottom_sheet); - BottomSheetBehavior.from(bottomSheetInternal).setPeekHeight(400); View button = view.findViewById(R.id.bottomsheet_button); - button.setOnClickListener(v -> bottomSheetDialog.show()); - - View bottomSheetContent = bottomSheetInternal.findViewById(R.id.bottom_drawer_2); - ViewUtils.doOnApplyWindowInsets(bottomSheetContent, (v, insets, initialPadding) -> { - // Add the inset in the inner NestedScrollView instead to make the edge-to-edge behavior - // consistent - i.e., the extra padding will only show at the bottom of all content, i.e., - // only when you can no longer scroll down to show more content. - ViewCompat.setPaddingRelative(bottomSheetContent, - initialPadding.start, - initialPadding.top, - initialPadding.end, - initialPadding.bottom + insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom); - return insets; - }); + button.setOnClickListener(v -> new BottomSheet().show(getParentFragmentManager(), "")); return view; } @@ -82,4 +53,35 @@ public View onCreateDemoView( protected int getDemoContent() { return R.layout.cat_bottomsheet_scrollable_content_fragment; } + + /** A custom bottom sheet dialog fragment. */ + @SuppressWarnings("RestrictTo") + public static class BottomSheet extends BottomSheetDialogFragment { + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + // Set up BottomSheetDialog + BottomSheetDialog bottomSheetDialog = + new BottomSheetDialog( + getContext(), R.style.ThemeOverlay_Catalog_BottomSheetDialog_Scrollable); + new WindowPreferencesManager(requireContext()).applyEdgeToEdgePreference(bottomSheetDialog.getWindow()); + bottomSheetDialog.setContentView(R.layout.cat_bottomsheet_scrollable_content); + View bottomSheetInternal = bottomSheetDialog.findViewById(R.id.design_bottom_sheet); + BottomSheetBehavior.from(bottomSheetInternal).setPeekHeight(400); + + View bottomSheetContent = bottomSheetInternal.findViewById(R.id.bottom_drawer_2); + ViewUtils.doOnApplyWindowInsets(bottomSheetContent, (v, insets, initialPadding) -> { + // Add the inset in the inner NestedScrollView instead to make the edge-to-edge behavior + // consistent - i.e., the extra padding will only show at the bottom of all content, i.e., + // only when you can no longer scroll down to show more content. + ViewCompat.setPaddingRelative(bottomSheetContent, + initialPadding.start, + initialPadding.top, + initialPadding.end, + initialPadding.bottom + insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom); + return insets; + }); + return bottomSheetDialog; + } + } }