BottomSheetBehavior is not a child of the coordinatorLayout

this is my XML layout named 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 snippet that contains 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 gives me this error:

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

      

from this line:

  BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

      

how can this be fixed? Everything seems to work fine but throws this error ... if anyone can help

+7


source to share


2 answers


BottomSheetBehavior

-

Interaction plugin for child CoordinatorLayout so that it works like a bottom sheet.

At the moment, your bottom sheet NestedScrollView

is a child LinearLayout

. So just remove the outer one completely LinearLayout

.



<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 a few more problems with the bottom sheet you are trying to implement. First, you shouldn't be using wrap_content

a scroll list. Second, you shouldn't use a list view inside a scroll, as it implements its own scrolling. You might be able to simplify this just by using the list view as the bottom sheet.

+9


source


In my case, instead of

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

      



I used <android.support.constraint.ConstraintLayout

(in a Fragment or Activity that contains a layout and a BottomSheet).

0


source







All Articles