Skip to content

Commit

Permalink
[Catalog][BottomSheet] Use BottomSheetDialogFragment
Browse files Browse the repository at this point in the history
Resolves #3601
Resolves #3600

GIT_ORIGIN_REV_ID=2c857675e53ee226eb46e6fcb5a77c68bb5ea4cc
PiperOrigin-RevId: 573848332
  • Loading branch information
manabu-nakamura authored and drchen committed Oct 17, 2023
1 parent 26c3129 commit 2c53952
Showing 1 changed file with 35 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,49 +40,48 @@
* 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;
}

@LayoutRes
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;
}
}
}

0 comments on commit 2c53952

Please sign in to comment.