BottomSheetBehavior is not a child of CoordinatorLayout
Note, if you don't use a CoordinatorLayout and instead use the BottomSheetDialogFragment (which makes one for you), I noticed that you cannot navigate to that dialog fragment with Fragment directions using the current version of the Nav component library (2.1.0-alpha05) and must instantiate it as a new fragment dialog otherwise you get this error, i.e. instead of using this:
navController().navigate(MerchantHistoryFragmentDirections.showDateSelection())
You must use this:
fragmentManager?.let {
val dateSelection = DateSelectionFragment.newInstance()
dateSelection.setTargetFragment(this, RC_DATE_SELECTION)
dateSelection.show(it)
}
It's a kind of obtuse error so hopefully this helps someone.
In my case I have used the following solution to solve the issue
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.setOnShowListener { dialogInterface ->
val bottomSheetDialog = dialogInterface as BottomSheetDialog
setupFullHeight(bottomSheetDialog)
}
return dialog
}
val bottomSheet = bottomSheetDialog
.findViewById<FrameLayout>(R.id.design_bottom_sheet)
val behavior: BottomSheetBehavior<*>?
if (bottomSheet != null) {
behavior = BottomSheetBehavior.from(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.isDraggable = false
}
The BottomSheetBehavior
is
An interaction behavior plugin for a child view of CoordinatorLayout to make it work as a bottom sheet.
At the moment you bottom sheet NestedScrollView
is a child of LinearLayout
. So just drop the outer-most LinearLayout
all completely.
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/viewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@android:color/holo_purple"
android:orientation="horizontal"/>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="308dp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/personlog"
app:layout_anchor="@id/viewA"
app:layout_anchorGravity="bottom|center" />
</android.support.design.widget.CoordinatorLayout>
But now you have some more issues with the bottom sheet you're trying to implement. Firstly you should not use wrap_content
with a scroll view. Secondly you should not use a list view inside a scroll view, since it's implementing its own scrolling. You might be able to simplify this by only using the list view as a bottom sheet.
If you are using data binding and including the layout to the fragment you have to do like following
val sheetBehavior = BottomSheetBehavior.from(binding.layoutBottomSheet.root)