web-dev-qa-db-ja.com

BottomSheetBehaviorはCoordinatorLayoutの子ではありません

これは、songlistという名前のXMLレイアウトです。

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>

これはこのレイアウトを含む私のフラグメントです:

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

これをどのように修正できますか?すべてがうまくいくようですが、そのエラーを与えます...誰かが助けてください

11
Erf

BottomSheetBehavior

CoordinatorLayoutの子ビューをボトムシートとして機能させるための対話動作プラグイン。

現時点では、ボトムシートNestedScrollViewLinearLayoutの子です。そのため、最も外側の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を使用しないでください。次に、独自のスクロールを実装しているため、スクロールビュー内でリストビューを使用しないでください。リストビューをボトムシートとして使用するだけで、これを簡素化できる場合があります。

11
tynn

CoordinatorLayoutを使用せず、代わりにBottomSheetDialogFragmentを使用する場合(これにより作成されます)、現在のバージョンのNavコンポーネントライブラリ(2.1.0- alpha05)そして、それを新しいフラグメントダイアログとしてインスタンス化する必要があります。そうしないと、これを使用する代わりに、このエラーが発生します。

navController().navigate(MerchantHistoryFragmentDirections.showDateSelection())

これを使用する必要があります:

fragmentManager?.let {
            val dateSelection = DateSelectionFragment.newInstance()
            dateSelection.setTargetFragment(this, RC_DATE_SELECTION)
            dateSelection.show(it)
        }

それは一種の鈍いエラーなので、うまくいけばこれが誰かの助けになります。

0
Daniel Wilson

私の場合ではなく

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

<Android.support.constraint.ConstraintLayoutを使用しました(レイアウトとBottomSheetを含むフラグメントまたはアクティビティで)。

0
CoolMind