Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.app.Dialog;
  3. import android.content.DialogInterface;
  4. import android.os.Bundle;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import android.support.design.widget.BottomSheetBehavior;
  8. import android.support.design.widget.CoordinatorLayout;
  9. import android.support.v4.app.DialogFragment;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.view.WindowManager;
  14. import android.widget.FrameLayout;
  15.  
  16. import butterknife.ButterKnife;
  17.  
  18. /**
  19. * Full screen {@link BaseFragment} with {@link BottomSheetBehavior} attached to its main content view.
  20. *
  21. * @author Leo
  22. */
  23. public abstract class FullScreenBottomSheetDialogFragment extends DialogFragment {
  24.  
  25. /** {@link BottomSheetBehavior} object that associates with this Fragment **/
  26. private BottomSheetBehavior mBottomSheetBehavior;
  27.  
  28. @Override
  29. public void onCreate(@Nullable Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetDialog);
  32. }
  33.  
  34. @Nullable
  35. @Override
  36. public final View onCreateView(LayoutInflater inflater,
  37. @Nullable ViewGroup container,
  38. @Nullable Bundle savedInstanceState) {
  39. CoordinatorLayout coordinator = (CoordinatorLayout) inflater.inflate(
  40. R.layout.full_screen_bottom_sheet, container, false);
  41. FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.bottom_sheet);
  42.  
  43. // create and add content view
  44. FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
  45. FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  46. View contentView = onCreateBottomSheetContentView(inflater, bottomSheet, savedInstanceState);
  47. ButterKnife.inject(this, contentView);
  48. bottomSheet.addView(contentView, layoutParams);
  49.  
  50. // behavior
  51. mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
  52. mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
  53. @Override
  54. public void onStateChanged(@NonNull View bottomSheet, int newState) {
  55. switch (newState) {
  56. case BottomSheetBehavior.STATE_DRAGGING:
  57. case BottomSheetBehavior.STATE_SETTLING:
  58. case BottomSheetBehavior.STATE_EXPANDED:
  59. break;
  60. case BottomSheetBehavior.STATE_COLLAPSED:
  61. case BottomSheetBehavior.STATE_HIDDEN:
  62. if (isAdded()) {
  63. dismissAllowingStateLoss();
  64. }
  65. break;
  66. }
  67. }
  68.  
  69. @Override
  70. public void onSlide(@NonNull View bottomSheet, float slideOffset) {
  71. // do nothing
  72. // be careful when overriding this method since it hurts performance on old devices
  73. }
  74. });
  75.  
  76. return coordinator;
  77. }
  78.  
  79. @Override
  80. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  81. super.onViewCreated(view, savedInstanceState);
  82. if (savedInstanceState == null) {
  83. // We need to use post here so we can prevent animation of setting STATE_EXPANDED from being swallowed in case
  84. // (probably) this method is run before layout pass is done.
  85. view.post(() -> mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED));
  86. } else {
  87. // if this fragment is restored from saved state, so don't bother to play the fancy animation
  88. mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
  89. }
  90. }
  91.  
  92. @Override
  93. public void onStart() {
  94. super.onStart();
  95. // make this dialog fullscreen - these lines have to be called here in onStart()
  96. Dialog dialog = getDialog();
  97. if (dialog.getWindow() != null) {
  98. dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
  99. }
  100. }
  101.  
  102. /** Get {@link BottomSheetBehavior} object that associates with this Fragment **/
  103. protected BottomSheetBehavior getBottomSheetBehavior() {
  104. return mBottomSheetBehavior;
  105. }
  106.  
  107. @Override
  108. public void onDismiss(DialogInterface dialog) {
  109. super.onDismiss(dialog);
  110. // finish activity if necessary
  111. Activity activity;
  112. if (shouldFinishActivityOnDismiss()
  113. && (activity = getActivity()) != null
  114. && !activity.isFinishing()) {
  115. activity.finish();
  116. }
  117. }
  118.  
  119. /**
  120. * Default returning {@code true}.<br/>
  121. * Override this method to return true to tell this Fragment not to finish its containing Activity on dismiss.
  122. */
  123. protected boolean shouldFinishActivityOnDismiss() {
  124. return true;
  125. }
  126.  
  127. /** Provide content view for this fragment **/
  128. @NonNull
  129. protected abstract View onCreateBottomSheetContentView(LayoutInflater inflater,
  130. @Nullable ViewGroup container,
  131. @Nullable Bundle savedInstanceState);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement