ScrollView 不在片段中滚动

ScrollView does not scroll in fragment

我用谷歌搜索了我的问题并在 Whosebug 中阅读了类似的问题,但我没有解决任何问题。

我有一个带有 coordinatorLayout (main.xml) 的布局和一个带有 relativeLayout (fragment.xml) 的片段

main.xml

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:paddingRight="16dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_weight="1">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:text="FRAGMENT TITLE"
                    android:textSize="20sp"
                    android:textColor="@color/colorWhite"
                    android:id="@+id/main_toolbar_title"
                    android:gravity="center_vertical" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:background="@color/colorWhite"
        android:layout_width="match_parent"
        android:layout_height="60dp" />
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#f3f3f3">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:contentPadding="1dp"
            card_view:cardCornerRadius="2dp">

            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="66dp"
                    android:textAllCaps="false"
                    android:text="Button Title"
                    android:textSize="18sp"
                    android:textColor="#404040"/>
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="66dp"
                    android:textAllCaps="false"
                    android:text="Button Title"
                    android:textSize="18sp"
                    android:textColor="#404040"/>
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="66dp"
                    android:textAllCaps="false"
                    android:text="Button Title"
                    android:textSize="18sp"
                    android:textColor="#404040"/>
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="66dp"
                    android:textAllCaps="false"
                    android:text="Button Title"
                    android:textSize="18sp"
                    android:textColor="#404040"/>
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="66dp"
                    android:textAllCaps="false"
                    android:text="Button Title"
                    android:textSize="18sp"
                    android:textColor="#404040"/>
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="66dp"
                    android:textAllCaps="false"
                    android:text="Button Title"
                    android:textSize="18sp"
                    android:textColor="#404040"/>
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="66dp"
                    android:textAllCaps="false"
                    android:text="Button Title"
                    android:textSize="18sp"
                    android:textColor="#404040"/>
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="66dp"
                    android:textAllCaps="false"
                    android:text="Button Title"
                    android:textSize="18sp"
                    android:textColor="#404040"/>

            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
</ScrollView>
</RelativeLayout>

如您所见,我在 scrollView 中有更多按钮但没有滚动。 view pager 中的 ui 元素,我开始以为是因为 view pager 但我测试了它 test_fragment.xml,它完美地工作。

scrollView 在我按住离开视图寻呼机时滚动(例如我的第一个标签) 并移动手指 up/down

test_fragment.xml

<android.support.design.widget.CoordinatorLayout
android:id="@+id/quickreturn_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

问题是什么?

提前致谢。

由于您使用的是 AppBarLayout,它不适用于简单的 ScrollView。您应该改用 NestedScrollview 。并且您已将 ScrollView 放入 RelativeLayout 中,这是没有必要的。再次尝试使用 NestedScrollView.

删除 RelativeLayout

我想这会有所帮助。