BottomDialog

2019. 4. 22. 16:27Android

반응형

o styles.xml

<style name="DefaultBottomDialog" parent="Animation.AppCompat.Dialog">
  <item name="android:windowEnterAnimation">@anim/design_bottom_sheet_slide_in</item>
  <item name="android:windowExitAnimation">@anim/design_bottom_sheet_slide_out</item>
</style>

BottomDialog.java

public class BottomDialog extends BottomSheetDialog {

private Window mWindow;
private Context mContext;

private int mAnimation;
private int mPeekHeight = 0;

public CommonBottomDialog(@NonNull Context context) {
  super(context);
  mContext = context;
  mWindow = this.getWindow();
}

public CommonBottomDialog(@NonNull Context context, int theme) {
  super(context, theme);
  mContext = context;
  mWindow = this.getWindow();
}

/**
* BottomSheetBehavior.STATE_COLLAPSED : height 만큼 보이게 됩니다.
* BottomSheetBehavior.STATE_EXPANDED : 가득 차게 처리합니다.
* BottomSheetBehavior.STATE_HIDDEN : 숨김 처리됩니다.
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setOnShowListener(new OnShowListener() {
   @Override
   public void onShow(DialogInterface dialog) {
      BottomSheetDialog d = (BottomSheetDialog) dialog;
      FrameLayout bottomSheet = d.findViewById(R.id.design_bottom_sheet);
      CoordinatorLayout coordinatorLayout = (CoordinatorLayout) bottomSheet.getParent();
      BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
      if(mPeekHeight == 0){
         bottomSheetBehavior.setHideable(true);
         bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
      }else {
         bottomSheet.getLayoutParams().height = mPeekHeight;
         bottomSheetBehavior.setPeekHeight(mPeekHeight);
         bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

         int defaultAnimation = R.style.DefaultBottomDialog;
         mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
         mWindow.setDimAmount(0.5f);
         if(mAnimation != defaultAnimation)
                  mWindow.getAttributes().windowAnimations = mAnimation;
         else
                  mWindow.getAttributes().windowAnimations = defaultAnimation;
         }
         bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                  @Override
                  public void onStateChanged(@NonNull View view, int i) {
                      
                   }

                  @Override
                  public void onSlide(@NonNull View view, float v) {
                          
                  }
         });
         coordinatorLayout.getParent().requestLayout();
       
    });
}

protected void setAnimation(int animation){
         mAnimation = animation;
}

protected void setPeekHeight(float peekHeight){
         mPeekHeight = Util.applyDimension(mContext, peekHeight);
}
}

 

o CustomBottomDialog.java

public class CustomBottomDialog extends BottomDialog {

private Context mContext;

public CustomBottomDialog(@NonNull Context context) {
  super(context);
  mContext = context;
}

public CustomBottomDialog(@NonNull Context context, int theme) {
  super(context, theme);
  mContext = context;
}

public CustomBottomDialog(@NonNull Context context, Bundle bundle) {
  super(context);
  mContext = context;
  mBundle = bundle;
  setContentView(R.layout.view_dialog_main); // layout of bottomdialog
  setAnimation(R.style.DefaultBottomDialog); // animation of bottomdialog
  setPeekHeight(250.0f); // Height of bottomdialog
}

반응형

'Android' 카테고리의 다른 글

AAC(Android Architecture Components)  (0) 2019.09.13
Letter spacing  (0) 2019.05.23
Observer Pattern  (0) 2019.04.19
안드로이드 파이 대응 관련  (0) 2019.04.18
android-design-support-library  (0) 2019.04.18