Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomSheetBehavior is not a child of CoordinatorLayout

this is my XML layout with name songlist :

enter image description here

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <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>
    </LinearLayout>
    <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>

and this is my fragment which contain this layout :

public class SongList extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.songlist,container,false);

        textView=(TextView)view.findViewById(R.id.txt);

        View bottomSheet = view.findViewById(R.id.bottom_sheet);
        BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
        bottomSheetBehavior.setPeekHeight(200);
return view;}
}

but when lunch the app give me this error :

java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout

from this line :

  BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

how can fix this ? seems all things work fine but give that error ... if any one can please help

like image 484
Erf Avatar asked Apr 08 '17 11:04

Erf


4 Answers

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.

like image 117
tynn Avatar answered Oct 17 '22 02:10

tynn


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)
like image 44
mksengun Avatar answered Oct 17 '22 02:10

mksengun


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.

like image 2
Daniel Wilson Avatar answered Oct 17 '22 04:10

Daniel Wilson


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
    }
like image 1
Aniket Nath Avatar answered Oct 17 '22 04:10

Aniket Nath