|
| 1 | +/* |
| 2 | + * Copyright 2024 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.material.catalog.bottomsheet; |
| 18 | + |
| 19 | +import io.material.catalog.R; |
| 20 | + |
| 21 | +import android.app.Dialog; |
| 22 | +import android.os.Bundle; |
| 23 | +import android.view.LayoutInflater; |
| 24 | +import android.view.View; |
| 25 | +import android.view.ViewGroup; |
| 26 | +import androidx.annotation.LayoutRes; |
| 27 | +import androidx.annotation.NonNull; |
| 28 | +import androidx.annotation.Nullable; |
| 29 | +import androidx.core.view.WindowInsetsCompat; |
| 30 | +import com.google.android.material.bottomsheet.BottomSheetBehavior; |
| 31 | +import com.google.android.material.bottomsheet.BottomSheetDialog; |
| 32 | +import com.google.android.material.bottomsheet.BottomSheetDialogFragment; |
| 33 | +import com.google.android.material.internal.ViewUtils; |
| 34 | +import io.material.catalog.feature.DemoFragment; |
| 35 | +import io.material.catalog.windowpreferences.WindowPreferencesManager; |
| 36 | + |
| 37 | +/** |
| 38 | + * A fragment that displays the a BottomSheet demo with unscrollable content and text input for the |
| 39 | + * Catalog app. |
| 40 | + */ |
| 41 | +public class BottomSheetUnscrollableContentDemoFragment extends DemoFragment { |
| 42 | + @NonNull |
| 43 | + @Override |
| 44 | + public View onCreateDemoView( |
| 45 | + @NonNull LayoutInflater layoutInflater, |
| 46 | + @Nullable ViewGroup viewGroup, |
| 47 | + @Nullable Bundle bundle) { |
| 48 | + View view = layoutInflater.inflate(getDemoContent(), viewGroup, false /* attachToRoot */); |
| 49 | + View button = view.findViewById(R.id.bottomsheet_button); |
| 50 | + button.setOnClickListener(v -> new BottomSheet().show(getParentFragmentManager(), "")); |
| 51 | + return view; |
| 52 | + } |
| 53 | + |
| 54 | + @LayoutRes |
| 55 | + protected int getDemoContent() { |
| 56 | + return R.layout.cat_bottomsheet_additional_demo_fragment; |
| 57 | + } |
| 58 | + |
| 59 | + /** A custom bottom sheet dialog fragment. */ |
| 60 | + @SuppressWarnings("RestrictTo") |
| 61 | + public static class BottomSheet extends BottomSheetDialogFragment { |
| 62 | + @NonNull |
| 63 | + @Override |
| 64 | + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { |
| 65 | + // Set up BottomSheetDialog |
| 66 | + BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getContext()); |
| 67 | + new WindowPreferencesManager(requireContext()).applyEdgeToEdgePreference(bottomSheetDialog.getWindow()); |
| 68 | + bottomSheetDialog.setContentView(R.layout.cat_bottomsheet_unscrollable_content); |
| 69 | + View bottomSheetInternal = bottomSheetDialog.findViewById(R.id.design_bottom_sheet); |
| 70 | + BottomSheetBehavior.from(bottomSheetInternal).setPeekHeight(400); |
| 71 | + |
| 72 | + View bottomSheetContent = bottomSheetInternal.findViewById(R.id.bottom_drawer_3); |
| 73 | + ViewUtils.doOnApplyWindowInsets(bottomSheetContent, (v, insets, initialPadding) -> { |
| 74 | + // Add the inset in the inner NestedScrollView instead to make the edge-to-edge behavior |
| 75 | + // consistent - i.e., the extra padding will only show at the bottom of all content, i.e., |
| 76 | + // only when you can no longer scroll down to show more content. |
| 77 | + bottomSheetContent.setPaddingRelative( |
| 78 | + initialPadding.start, |
| 79 | + initialPadding.top, |
| 80 | + initialPadding.end, |
| 81 | + initialPadding.bottom + insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom); |
| 82 | + return insets; |
| 83 | + }); |
| 84 | + return bottomSheetDialog; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments