视图分页器不在scrollView中滚动

时间:2019-04-01 07:25:55

标签: android android-viewpager android-scrollview

我在布局文件中有一个ViewPager和一个RecyclerView。我想使整个布局可滚动。那就是我希望ViewPager向上滚动。因此,我添加了<ScrollView>作为根布局。

问题:

我面临的问题是RecyclerView在滚动中按预期方式运行,但是ViewPager区域是固定的。

下图描述了上述问题:

enter image description here

我的布局文件的xml代码如下所示:

<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:orientation="vertical"
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/home_viewpager_guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.4" />

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/home_view_pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/home_viewpager_guideline"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:id="@+id/home_viewpager_dots_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="16dp"
            android:gravity="center"
            android:orientation="horizontal"
            app:layout_constraintBottom_toTopOf="@+id/home_viewpager_guideline"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0"></LinearLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/linearLayout3"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@color/latest_news_bg"
            app:layout_constraintBottom_toTopOf="@+id/latest_news_guideline"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/home_view_pager">

            <View
                android:id="@+id/divider2"
                android:layout_width="0dp"
                android:layout_height="3dp"
                android:background="@color/gray"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.16"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent"
                app:layout_constraintVertical_bias="1.0" />

            <TextView
                android:id="@+id/latest_news_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginBottom="8dp"
                android:text="Latest News"
                android:textColor="@color/dark_gray"
                android:textSize="22sp"
                app:layout_constraintBottom_toTopOf="@+id/divider2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/latest_news_guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.51" />

        <TextView
            android:id="@+id/home_article_type_txt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:fontFamily="sans-serif-medium"
            android:text="Featured Article"
            android:textColor="@color/pf_white"
            android:textSize="20sp"
            app:layout_constraintBottom_toTopOf="@+id/home_viewpager_dots_layout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.02"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.65" />

        <TextView
            android:id="@+id/home_article_title_txt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="8dp"
            android:text="What better way to put your health and wellbeing first than"
            android:textColor="@color/pf_white"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/home_viewpager_dots_layout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.05"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/home_article_type_txt"
            app:layout_constraintVertical_bias="0.0" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/home_article_list"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout3"
            app:layout_constraintVertical_bias="1.0" />


    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

我尝试了什么? 如本答案所述Android ScrollView not Scrolling ,我尝试将ScrollView方向设置为vertical,并将fillViewPort属性设置为true,但问题仍然存在。

我也尝试使用NestedScrollView作为根,但这也行不通。

如果有人可以建议我该怎么做,这对面临此类问题的任何人都将非常有帮助。

0 个答案:

没有答案