垂直ScrollView内的水平ViewPager

时间:2016-10-13 11:15:53

标签: android android-viewpager fragment scrollview

在我的应用程序中,我有一个带有水平ViewPager的片段和位于垂直ScrollView下的水平RecycleViews。我的问题是ViewPager阻止了父垂直滚动。我试图禁用ViewPager垂直触摸事件,但它没有帮助。 这是一个显示问题的视频:

https://drive.google.com/file/d/0B9XMTJsXn0ofQnpOcWo5TW9GLWs/view?usp=sharing

这是我的布局xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_gravity="top"
            android:visibility="gone" />

        <InkPageIndicator
            android:id="@+id/page_indicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginStart="@dimen/activity_horizontal_margin"
            android:paddingBottom="8dp"
            android:visibility="gone" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:minHeight="48dp"
        android:visibility="gone">

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1.0"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:text="@string/software"
                android:textColor="?android:textColorPrimary"
                android:textSize="20sp" />
        </FrameLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginStart="@dimen/activity_horizontal_margin"
            android:text="@string/see_all"
            android:textAppearance="@style/TextAppearance.Button"
            android:textColor="?colorAccent" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/software_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="4dp"
        android:visibility="gone" />
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您正在使用LinearLayout,然后您可以设置

  

机器人:weightSum

解决此问题的属性。

答案 1 :(得分:0)

使用CoordinatorLayout而不是Scrollview,将view-pager放在CollapsingToolbarLayout中,其余布局放在nestedScrollView中。

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
android:fitsSystemWindows="true">

<android.support.v7.widget.Toolbar
    android:id="@+id/main.toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/main.appbar"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main.collapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_gravity="top"
                android:visibility="gone" />

            <InkPageIndicator
                android:id="@+id/page_indicator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="@dimen/activity_horizontal_margin"
                android:layout_marginStart="@dimen/activity_horizontal_margin"
                android:visibility="gone" />
        </RelativeLayout>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

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

    // Put the layouts and views here, those you want to show below the view pager.
</android.support.v4.widget.NestedScrollView>