无法滚动动态添加的片段 (Java)

Unable to scroll a Fragment added dynamically (Java)

我正在开发一个应用程序,我需要在 activity 中使用 2 个片段。根据对第一个片段所做的选择,将显示第二个片段。问题是第二个片段没有向下滚动,被屏幕末端截断了。

这是 activity 的 XML,其中包含“固定”和动态插入的片段 (activity_inspection.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".activities.InspectionActivity"
    android:padding="@dimen/margins">

    <fragment
        android:id="@+id/chooseItemMemorial"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.xxxx.xxxxx.fragments.InspectionMemorial"/>

    <FrameLayout
        android:id="@+id/show_fragment_selected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/chooseItemMemorial"
        android:layout_marginTop="@dimen/top_margins"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我想动态插入到我的 activity 中的片段之一的片段:(fragment_external_access.xml)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
    android:scrollbars="vertical"
    tools:context=".fragments.ExternalAccessFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
/** here goes the rest of the code**/

编辑:我想要实现的是第一个片段,通过 <fragment> 标签添加,保持固定在屏幕上,而第二个片段,通过代码动态添加,如果可以滚动必要的。

我尝试使用 ScrollViewNestedScrollView 但没有成功。但这可能是因为一些愚蠢的错误,比如忘记添加标签。由于我是 android 编程新手,我们将不胜感激。

为了结束这个(因为它可能是相关的),这是我用来添加这个片段的 java 代码:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_external_access, container, false);
    }

发生这种情况是因为您的滚动视图 layout_height 设置为 wrap_content 从而展开以适应其所有内容。您应该将其设置为 match_parent,

编辑:根据新信息,您可以构建如下布局

Activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:padding="@dimen/margins"
    tools:context=".activities.InspectionActivity">

    <fragment
        android:id="@+id/chooseItemMemorial"
        class="com.xxxx.xxxxx.fragments.InspectionMemorial"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_max="100dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView 
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/top_margins"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/chooseItemMemorial" 
        android:scrollbars="vertical">

        <FrameLayout
            android:id="@+id/show_fragment_selected"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

片段

<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
    tools:context=".fragments.ExternalAccessFragment">

/** here goes the rest of the code**/

这样,顶部片段会占用它需要的 space(根据您使用 app:layout_constraintHeight_max 指定的最高 100dp),下面的滚动视图会占用所有剩余的 space,这将有里面的另一个片段有 wrap_content 高度。

问题是最上面的fragment(First Fragment)可以贪心占满整个屏幕,原因是它的高度是wrap_content,所以能占多少就占多少包含,因为它缺少底部约束。

在这种情况下,底部片段可以部分显示甚至隐藏在屏幕下方。

如果底部片段内容较大,同样适用,由于底部片段wrap_content,顶部片段将partially/totally隐藏。

所以你需要限制两个片段的高度不使用wrap_content

这里我将app:layout_constraintHeight_max属性用于顶部片段,但如果它的内容可以大于该值,你必须处理; ScrollView 是可能的选项之一,就像您在底部片段(第二个片段)中所做的那样。

并设置两个片段的高度以匹配约束。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:padding="@dimen/margins"
    tools:context=".activities.InspectionActivity">

    <fragment
        android:id="@+id/chooseItemMemorial"
        class="com.xxxx.xxxxx.fragments.InspectionMemorial"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_max="100dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/show_fragment_selected"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/top_margins"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/chooseItemMemorial" />

</androidx.constraintlayout.widget.ConstraintLayout>