无法让 ScrollView 或 LinearLayout 滚动 children 超出屏幕

Can't get ScrollView or LinearLayout to scroll on children exceeding screen

我浏览了很多 SO 和其他论坛,但无法得到答案,为什么我的 LinearLayout 不滚动,而 children 碰巧超出了可用的屏幕空间。

None 提供的答案确实有效。有趣的是,我还有其他几个活动,(我认为)布局基本相同,并且滚动这些活动从来没有问题。

这是我的布局代码:

<?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:background="@android:color/holo_blue_bright"
    android:backgroundTint="@android:color/holo_blue_light"
    android:backgroundTintMode="src_over"
    android:fitsSystemWindows="true"
    app:statusBarBackground="@color/LightGreen1"
    tools:context="com.sbm.bc.smartbooksmobile.ActivityTeacherHwkAddTask">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/side_nav_bar"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/holo_blue_dark"
        android:elevation="0dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:background="@android:color/holo_blue_bright"
        android:clickable="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="10dp"
            android:orientation="horizontal"
            android:paddingTop="7dip">

            <TableRow
                android:id="@+id/beforeSearchTabRow"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:elevation="10dp">

                <EditText
                    android:id="@+id/SeekTopicBox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="?attr/actionModeSplitBackground"
                    android:clickable="true"
                    android:elevation="15dp"
                    android:focusable="true"
                    android:inputType="text"
                    android:onClick="onSeekTopicBoxClick"
                    android:selectAllOnFocus="true"
                    android:text="@string/seekTopic"
                    android:textIsSelectable="true"
                    android:textSize="18sp" />

                <ImageButton
                    android:id="@+id/searchButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="-9dp"
                    android:elevation="17dp"
                    android:onClick="onSearchButtonClick"
                    app:srcCompat="@android:drawable/ic_search_category_default" />

            </TableRow>

        </LinearLayout>

        <CheckBox
            android:id="@+id/allTopics"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_marginTop="-35dp"
            android:text="@string/everything"
            android:textAlignment="viewStart"
            android:onClick="onAllTopicsClick"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/SharpGreen1"
            android:elevation="7dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/GradeColumnLabel"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="5"
                android:ems="10"
                android:text="@string/year_of_study"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/TopicColumnLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:gravity="start"
                android:text="@string/topic"
                android:textAlignment="center"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/HomeworkColumnLabel"
                android:layout_width="33dp"
                android:layout_height="wrap_content"
                android:elevation="1dp"
                android:ems="10"
                android:gravity="center"
                android:text="@string/homework"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/TestColumnLabel"
                android:layout_width="33dp"
                android:layout_height="wrap_content"
                android:elevation="1dp"
                android:ems="10"
                android:gravity="center"
                android:text="@string/test_short"
                android:textStyle="bold" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ListOfTopics"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_bright"
            android:elevation="7dp"
            android:isScrollContainer="true"
            android:orientation="vertical"
            android:overScrollMode="ifContentScrolls" />

        <TextView
            android:id="@+id/NoTasksMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:background="@android:color/holo_green_dark"
            android:textAlignment="center"
            android:visibility="visible"
            tools:text="Nenašli sa žiadne úlohy .." />

    </LinearLayout>

    <!-- This FrameLayout insets its children based on system windows using
     android:fitsSystemWindows. -->

    <LinearLayout
        android:id="@+id/fullscreen_content_controls"
        style="?metaButtonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/black_overlay"
        android:elevation="7dp"
        android:orientation="horizontal"
        tools:ignore="UselessParent">

        <Button
            android:id="@+id/addHomeworkBtn"
            style="?metaButtonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:alpha="50"
            android:elevation="3dp"
            android:onClick="onSendButtonClick"
            android:text="@string/next" />

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

布局如下所示:

重要的part/object是:

ListOfTopics << , into which I expand another items: LinearLayouts with children.

这就是我expand/inject:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal">

        <TextView
            android:id="@+id/schoolGrade"
            android:layout_width="33dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.14"
            android:text="Rocnik" />

        <TextView
            android:id="@+id/TopicName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Tema" />

        <CheckBox
            android:id="@+id/SelectTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:foregroundTint="@color/SharpGreen1"
            android:shadowColor="@android:color/holo_green_dark"
            android:textColor="@color/colorPrimary"
            android:textColorLink="@color/SharpGreen1" />

        <CheckBox
            android:id="@+id/SelectHwk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:shadowColor="@color/SharpGreen1"
            android:textColor="@android:color/holo_blue_bright"
            android:textColorLink="@android:color/holo_green_dark" />

</LinearLayout>

是的,我曾尝试将名为 "ListOfTopics" 的顶级 LinearLayout 容器放入 ScrollView,但没有帮助。如果需要,LinearLayout 本身就具有滚动的内在能力。

我在这个问题上花费了很多时间。谁能指出我做错了什么吗?

你想在scrollview中添加listview的地方,你可以试试下面的代码

<ScrollView 
android:layout_width="match_parent"
android:layout_height="match_parent">

    <com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView
        android:id="@+id/expandable_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
      </com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView></ScrollView>

这对我有用。 对于此列表视图 github link: https://github.com/paolorotolo/ExpandableHeightListView

信不信由你,修复它的 "magic" 是更改顶层 CoordinatorLayout that I had, for something else. I tried RelativeLayout,它在我已有的其他活动中滚动效果很好。

CoordinatorLayout 实现了 NestedScrollingParent2 接口,据我所知,该接口假定处理带有委托的嵌套容器的滚动事件。

有趣的是,即使使用 CoordinatorLayout,我也能够在嵌套视图上捕获所有 touch/scroll 事件并手动处理它们(当然这不是我喜欢的)。