Android-该视图不是CoordinatorLayout(BottomSheet)的子级

时间:2019-11-03 04:31:56

标签: android android-layout android-fragments bottom-sheet

我有一个布局,其中用户拍摄照片,并将片段的背景更改为照片。此版式的底部有一个,其中有一个标签,其中包括我的“底页版式”。我试图将此底页附加到我想拥有的片段上,该片段应该使的布局膨胀,但是每次我创建此新片段并尝试用新片段替换该区域时,都会出现错误“视图为而不是CoordinatorLayout的子级”,即使在我看来是这样。即使在布局中,它也是协调器布局的子元素。文件在下面。我正在尝试使用以下代码将底部工作表片段添加到我的主要片段中:

public void showBottomSheetDialogFragment() {
        imageAnalysisBottomSheet = new ImageAnalysisBottomSheet();
        getChildFragmentManager().beginTransaction().replace(R.id.bottom_sheet, imageAnalysisBottomSheet).commit();
        coordinator.setVisibility(View.VISIBLE);
    }

有人对如何解决此问题有任何想法吗?如果不调用showBottomSheetDialogFragment方法,我可以看到底部的表格,但是没有附加逻辑(即,当用户键入数字时,我可以执行某些操作)。我将如何解决这个问题?

布局与底页:

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

    <com.example.cyfi.picture_tab.DrawImageView
        android:id="@+id/image_preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:contentDescription="ap_picture" />

    <TextView
        android:id="@+id/image_instructions"
        android:paddingStart="20dp"
        android:paddingEnd="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:fontFamily="sans-serif-black"
        android:textSize="28sp"
        android:text="Click the camera icon below to take a picture of the access point. Ensure that you are taking an extremely clear picture with direct line of sight. "
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/take_picture_button"
        android:visibility="visible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:src="@drawable/ic_photo_camera_black_24dp"
        app:backgroundTint="@color/silver"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        >
        <View
            android:id="@+id/shadow"
            android:layout_width="match_parent"
            android:layout_height="16dp"
            android:background="@drawable/shadow_gradient"
            app:layout_anchor="@id/bottom_sheet" />
        <include
            android:id="@+id/bottom_sheet"
            android:visibility="visible"
            layout="@layout/image_analysis"/>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

图像分析布局(标记中的布局):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:behavior_hideable="false"
    app:behavior_peekHeight="60dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    android:background="@drawable/rounded_bottom_sheet"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <TextView
        android:id="@+id/heading"
        android:paddingStart="4dp"
        android:paddingTop="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Image Processing"
        android:textStyle="bold"
        android:textSize="32sp"
        android:textColor="@color/black"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>
    <View
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/heading"
        app:layout_constraintBottom_toTopOf="@id/object_size"
        style="@style/Divider.Horizontal"/>
    <TextView
        android:id="@+id/object_size"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingStart="4dp"
        android:paddingTop="8dp"
        app:layout_constraintTop_toBottomOf="@id/heading"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/length"
        android:text="Actual Object Parameters"
        android:textStyle="bold"
        android:textSize="24sp"
        android:textColor="@color/black"/>
    <LinearLayout
        android:id="@+id/length"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/object_size"
        app:layout_constraintStart_toStartOf="parent"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="4dp"
            android:paddingEnd="16dp"
            android:paddingTop="8dp"
            android:text="Object Length"
            android:textSize="18sp"
            android:textColor="@color/black"/>
        <EditText
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:maxLines="1"
            android:maxLength="2"
            android:hint="5" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="4dp"
            android:paddingEnd="16dp"
            android:paddingTop="8dp"
            android:text="Inches"
            android:textSize="18sp"
            android:textColor="@color/black"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/width"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/length"
        app:layout_constraintStart_toStartOf="parent"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/object_width"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="4dp"
            android:paddingEnd="16dp"
            android:paddingTop="8dp"
            android:text="Object Width"
            android:textSize="18sp"
            android:textColor="@color/black"/>
        <EditText
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:maxLines="1"
            android:maxLength="2"
            android:hint="8"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="4dp"
            android:paddingEnd="16dp"
            android:paddingTop="8dp"
            android:text="Inches"
            android:textSize="18sp"
            android:textColor="@color/black"/>
    </LinearLayout>
    <Button
        android:id="@+id/save_button"
        app:layout_constraintTop_toBottomOf="@id/width"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="12dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"/>
    <View
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/save_button"
        app:layout_constraintBottom_toTopOf="@id/object_recognition"
        style="@style/Divider.Horizontal"/>
    <TextView
        android:id="@+id/object_recognition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingStart="4dp"
        android:paddingTop="8dp"

        app:layout_constraintTop_toBottomOf="@id/save_button"
        app:layout_constraintStart_toStartOf="parent"
        android:text="Image Metrics"
        android:textStyle="bold"
        android:textSize="24sp"
        android:textColor="@color/black"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/object_recognition"
        app:layout_constraintEnd_toEndOf="parent">
        <TextView
            android:id="@+id/top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="top"
            />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

ImageAnalysisBottomSheet()

package com.example.cyfi.picture_tab;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import com.example.cyfi.R;
import com.google.android.material.bottomsheet.BottomSheetBehavior;

public class ImageAnalysisBottomSheet extends Fragment {
    private ApImageViewModel apImageViewModel;
    private BottomSheetBehavior sheetBehavior;
    private TextView top;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        apImageViewModel = ViewModelProviders.of(getActivity()).get(ApImageViewModel.class);
        apImageViewModel.getBottom().observe(this, this::bottomEdge);
        apImageViewModel.getTop().observe(this, this::topEdge);
        apImageViewModel.getLeft().observe(this, this::leftEdge);
        apImageViewModel.getRight().observe(this, this::rightEdge);
    }

    private void rightEdge(Float right) {
    }

    private void leftEdge(Float left) {
    }

    private void topEdge(Float top) {
        this.top.setText(top.toString());
    }

    private void bottomEdge(Float bottom) {
        //do something with it
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.image_analysis, container, false);
        top = view.findViewById(R.id.top);
        sheetBehavior = BottomSheetBehavior.from(view);
        sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                switch (newState) {
                    case BottomSheetBehavior.STATE_HIDDEN:
                        break;
                    case BottomSheetBehavior.STATE_EXPANDED: {

                    }
                    break;
                    case BottomSheetBehavior.STATE_COLLAPSED: {

                    }
                    break;
                    case BottomSheetBehavior.STATE_DRAGGING:
                        break;
                    case BottomSheetBehavior.STATE_SETTLING:
                        break;
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });
        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将协调器布局添加为父级

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

    <com.example.cyfi.picture_tab.DrawImageView
        android:id="@+id/image_preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:contentDescription="ap_picture" />

    <TextView
        android:id="@+id/image_instructions"
        android:paddingStart="20dp"
        android:paddingEnd="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:fontFamily="sans-serif-black"
        android:textSize="28sp"
        android:text="Click the camera icon below to take a picture of the access point. Ensure that you are taking an extremely clear picture with direct line of sight. "
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/take_picture_button"
        android:visibility="visible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:src="@drawable/ic_photo_camera_black_24dp"
        app:backgroundTint="@color/silver"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

        <View
            android:id="@+id/shadow"
            android:layout_width="match_parent"
            android:layout_height="16dp"
            android:background="@drawable/shadow_gradient"
            app:layout_anchor="@id/bottom_sheet" />
        <include
            android:id="@+id/bottom_sheet"
            android:visibility="visible"
            layout="@layout/image_analysis"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>