ViewPager 内片段中的 ScrollView 不滚动

ScrollView in a fragment inside ViewPager is not scrolling

我在 activity 中有一个 tablayout 和一个 viewpager,在 viewpager 里面我显示了一个带有滚动视图的片段,因为它是根布局,但由于某种原因它没有滚动。我一直试图让它工作几个小时,但到目前为止没有任何效果,请帮忙。

这是我的activity:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PreferencesActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_toolbar_preferences"
            android:textAlignment="center"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <TextView
                android:id="@+id/toolbar_title"
                style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/prefs_title"
                android:textColor="@android:color/white"
                android:textStyle="bold" />
        </com.google.android.material.appbar.MaterialToolbar>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:elevation="5dp" />
        </androidx.viewpager.widget.ViewPager>

    </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是我的带有滚动视图的片段:

<?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="match_parent"
    tools:context=".fragmentsPrefsActivity.FragmentAppPrefs">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="32dp"
        android:paddingRight="32dp">

        <androidx.cardview.widget.CardView
            android:id="@+id/cardView_alerts"
            style="@style/app_prefs_cardView">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/textView"
                    style="@style/app_prefs_text_header"
                    android:text="Alerts"
                    android:textSize="18sp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <com.suke.widget.SwitchButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="16dp"
                    app:layout_constraintBottom_toBottomOf="@+id/textView3"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/textView3"
                    app:sb_checked="true"
                    app:sb_checked_color="@color/colorPrefsDark"
                    app:sb_show_indicator="false" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    android:text="Sound"
                    android:textColor="@color/textColor"
                    android:textSize="18sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="@+id/textView"
                    app:layout_constraintTop_toBottomOf="@+id/textView" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.cardview.widget.CardView>

        <TextView
            android:id="@+id/text_men"
            style="@style/app_prefs_description_text"
            android:text="@string/prefs_alerts_description" />

        <androidx.cardview.widget.CardView
            android:id="@+id/cardView_matchMakerOnlyMode"
            style="@style/app_prefs_cardView">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.suke.widget.SwitchButton
                    android:id="@+id/switchButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="16dp"
                    app:layout_constraintBottom_toBottomOf="@+id/textView21"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/textView21"
                    app:sb_checked_color="@color/colorPrefsDark"
                    app:sb_show_indicator="false" />

                <TextView
                    android:id="@+id/textView10"
                    style="@style/app_prefs_text_header"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:text="Matchmaker-Only Mode"
                    android:textSize="18sp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/textView21"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    android:text="Hide My Profile"
                    android:textColor="@color/textColor"
                    android:textSize="18sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="@+id/textView10"
                    app:layout_constraintTop_toBottomOf="@+id/textView10" />

            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>

        <TextView
            android:id="@+id/textView11"
            style="@style/app_prefs_description_text"
            android:text="@string/prefs_match_maker_only_mode_description" />

        <androidx.cardview.widget.CardView
            android:id="@+id/cardView3"
            style="@style/app_prefs_cardView">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/cardView_selectLanguage"
                    style="@style/app_prefs_text_header"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:text="Select Language"
                    android:textSize="18sp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <Spinner
                    android:id="@+id/language_spinner"
                    style="@style/app_prefs_text"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="16dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/cardView_selectLanguage" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>

        <TextView
            android:id="@+id/textView17"
            style="@style/app_prefs_description_text"
            android:text="@string/prefs_select_language_description" />

        <androidx.cardview.widget.CardView
            android:id="@+id/cardView_enableNotifications"
            style="@style/app_prefs_cardView">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/textView18"
                    style="@style/app_prefs_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:text="Enable notifications from GoGaga"
                    android:textColor="#151515"
                    android:textSize="16sp"
                    app:layout_constraintBottom_toBottomOf="@+id/btn_go"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/btn_go" />

                <Button
                    android:id="@+id/btn_go"
                    style="?attr/borderlessButtonStyle"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="16dp"
                    android:background="@drawable/ripple_btn_go"
                    android:text="Go"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintDimensionRatio=""
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>

        <TextView
            android:id="@+id/textView19"
            style="@style/app_prefs_description_text"
            android:text="@string/prefs_enable_notification_description" />
    </LinearLayout>


</ScrollView>

你能试着把你的 ViewPager 标签放在 AppBarLayout 之外吗 所以你的 activity xml 应该是这样的:

<CoordinatorLayout>
   <AppBarLayout>
      //...
   </AppBarLayout>
   <ViewPager>
      //...
   </ViewPager>
</CoordinatorLayout>

并且不要忘记将此属性添加到您的视图寻呼机中:

app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"