android – BottomSheetBehavior不是CoordinatorLayout的子代

前端之家收集整理的这篇文章主要介绍了android – BottomSheetBehavior不是CoordinatorLayout的子代前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的 XML布局,名称为歌曲列表:

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

这是我的片段,其中包含此布局:

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;}
}

但午餐时,应用程序给我这个错误

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

从这一行:

BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

怎么解决这个问题?似乎所有的事情都运转正常,但给出了这个错误…如果任何人可以请求帮助

解决方法

BottomSheetBehavior

An interaction behavior plugin for a child view of CoordinatorLayout to make it work as a bottom sheet.

目前,您的底层表NestedScrollView是LinearLayout的子项.所以只需完全删除最外面的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>

但是现在你在尝试实现的底页上遇到了一些问题.首先,不应将wrap_content与滚动视图一起使用.其次,您不应在滚动视图中使用列表视图,因为它正在实现自己的滚动.您可以通过仅将列表视图用作底部工作表来简化此操作.

原文链接:https://www.f2er.com/android/308726.html

猜你在找的Android相关文章