Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.design.widget.BottomSheetBehavior;
- import android.support.design.widget.CoordinatorLayout;
- import android.support.v4.app.DialogFragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.FrameLayout;
- import butterknife.ButterKnife;
- /**
- * Full screen {@link BaseFragment} with {@link BottomSheetBehavior} attached to its main content view.
- *
- * @author Leo
- */
- public abstract class FullScreenBottomSheetDialogFragment extends DialogFragment {
- /** {@link BottomSheetBehavior} object that associates with this Fragment **/
- private BottomSheetBehavior mBottomSheetBehavior;
- @Override
- public void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetDialog);
- }
- @Nullable
- @Override
- public final View onCreateView(LayoutInflater inflater,
- @Nullable ViewGroup container,
- @Nullable Bundle savedInstanceState) {
- CoordinatorLayout coordinator = (CoordinatorLayout) inflater.inflate(
- R.layout.full_screen_bottom_sheet, container, false);
- FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.bottom_sheet);
- // create and add content view
- FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
- FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
- View contentView = onCreateBottomSheetContentView(inflater, bottomSheet, savedInstanceState);
- ButterKnife.inject(this, contentView);
- bottomSheet.addView(contentView, layoutParams);
- // behavior
- mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
- mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
- @Override
- public void onStateChanged(@NonNull View bottomSheet, int newState) {
- switch (newState) {
- case BottomSheetBehavior.STATE_DRAGGING:
- case BottomSheetBehavior.STATE_SETTLING:
- case BottomSheetBehavior.STATE_EXPANDED:
- break;
- case BottomSheetBehavior.STATE_COLLAPSED:
- case BottomSheetBehavior.STATE_HIDDEN:
- if (isAdded()) {
- dismissAllowingStateLoss();
- }
- break;
- }
- }
- @Override
- public void onSlide(@NonNull View bottomSheet, float slideOffset) {
- // do nothing
- // be careful when overriding this method since it hurts performance on old devices
- }
- });
- return coordinator;
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- if (savedInstanceState == null) {
- // We need to use post here so we can prevent animation of setting STATE_EXPANDED from being swallowed in case
- // (probably) this method is run before layout pass is done.
- view.post(() -> mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED));
- } else {
- // if this fragment is restored from saved state, so don't bother to play the fancy animation
- mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
- }
- }
- @Override
- public void onStart() {
- super.onStart();
- // make this dialog fullscreen - these lines have to be called here in onStart()
- Dialog dialog = getDialog();
- if (dialog.getWindow() != null) {
- dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
- }
- }
- /** Get {@link BottomSheetBehavior} object that associates with this Fragment **/
- protected BottomSheetBehavior getBottomSheetBehavior() {
- return mBottomSheetBehavior;
- }
- @Override
- public void onDismiss(DialogInterface dialog) {
- super.onDismiss(dialog);
- // finish activity if necessary
- Activity activity;
- if (shouldFinishActivityOnDismiss()
- && (activity = getActivity()) != null
- && !activity.isFinishing()) {
- activity.finish();
- }
- }
- /**
- * Default returning {@code true}.<br/>
- * Override this method to return true to tell this Fragment not to finish its containing Activity on dismiss.
- */
- protected boolean shouldFinishActivityOnDismiss() {
- return true;
- }
- /** Provide content view for this fragment **/
- @NonNull
- protected abstract View onCreateBottomSheetContentView(LayoutInflater inflater,
- @Nullable ViewGroup container,
- @Nullable Bundle savedInstanceState);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement