Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomSheetCallback from Modal BottomSheetFragment

Tags:

How to add BottomSheetBehavior if I use Modal BottomSheetFragment ?

var bottomSheetBehaviorCallback = 
    object : BottomSheetBehavior.BottomSheetCallback() {

        override fun onSlide(bottomSheet: View, slideOffset: Float) {
            Log.d(Constants.LOG_INFO, "on slide")
        }

        override fun onStateChanged(bottomSheet: View, newState: Int) {
            Log.d(Constants.LOG_INFO, "onStateChanged")
        }
    }

val detailView = StationDetailFragment.newInstance(selectedStationID, "")
detailView.show(fragmentManager, "detailView")

The view is not created at this point

I changed my code and added override setupDialog . But I still don't get callback to work

override fun setupDialog(dialog: Dialog?, style: Int) {

    val contentView = View.inflate(context, R.layout.fragment_station_detail, null)
    dialog!!.setContentView(contentView)

    val layoutParams = (contentView.parent as View).layoutParams as CoordinatorLayout.LayoutParams

    val behavior = layoutParams.behavior

    if (behavior != null && behavior is BottomSheetBehavior<*>) {
      behavior.setBottomSheetCallback(mBottomSheetBehaviorCallback)
    }
}
like image 821
martomstom Avatar asked Dec 20 '17 15:12

martomstom


2 Answers

Thank you all! The best solution for me was to add my BottomSheet as persistent Bottom Sheet to activity_main.xml

The following explanation helped me Android working with Bottom Sheet

Finally I found the solution for Modal Bottomsheet with kotlin

  override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {


    val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog

    dialog.setOnShowListener { dialog ->
        val d = dialog as BottomSheetDialog

        val bottomSheet = d.findViewById<View>(android.support.design.R.id.design_bottom_sheet) as FrameLayout?
        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet!!)
        bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
    }

    return dialog

}

My Problem was that I tried to use my Coordinator Layout. But for Modal BottomSheet you have to use android.support.design.R.id.design_bottom_sheet

like image 146
martomstom Avatar answered Sep 22 '22 13:09

martomstom


You can set your Behaviour in setUpDialog method.

 @Override
        public void setupDialog(Dialog dialog, int style) {
            View contentView = View.inflate(getContext(), R.layout.custom_filter_bottom_sheet, null);
            dialog.setContentView(contentView);
            CoordinatorLayout.LayoutParams layoutParams =
                    (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
            CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
            if (behavior != null && behavior instanceof BottomSheetBehavior) {
                ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
            }

        }

and defining the callbacks

 private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        }
    };
like image 25
Abubakker Moallim Avatar answered Sep 19 '22 13:09

Abubakker Moallim