在NestedScrollView中使用ViewPager

123

我需要创建一个类似于 Google Newsstand 的界面,它是在可折叠的标题栏(纵向滚动)上使用ViewPager(横向滚动)。我的一个要求是使用在2015年Google IO上推出的新设计支持库Design Support Library。 (http://android-developers.blogspot.ca/2015/05/android-design-support-library.html)

在Chris Banes的示例基础上(https://github.com/chrisbanes/cheesesquare),我已经实现了可折叠效果,但是只能使用基本的LinearLayout而没有水平滚动。

我试图用ViewPager替换LinearLayout,但是只得到一个空白屏幕。我尝试调整宽度、权重和各种视图组,但是仍然是空白屏幕。看起来ViewPager 和NestedScrollView不兼容。

我试图通过使用HorizontalScrollView进行一些变通:它可以工作,但是我失去了PagerTitleStrip的功能和对单个面板的聚焦(我无法在两个面板之间停止水平滚动)。

现在我没有更多的想法,如果有人能帮助我找到解决方案...

谢谢

这是我的最新布局文件:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/header_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <include
                layout="@layout/part_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="parallax"/>

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

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

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

        <android.support.v4.view.ViewPager
            android:id="@+id/activity_main_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFA0"/>


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

你尝试过将 ViewPager 包裹在 LinearLayout 中吗?只是好奇这样做是否能正确渲染 ViewPager - CzarMatt
是的,我尝试过了,但结果仍然相同。我使用了许多组件进行了许多测试,但无论层次结构如何,ViewPager都不会在NestedScrollView中呈现。 - Kyso84
我刚刚创建了一个测试应用程序,其中包含一个ViewPager和一个NestedScrollView作为唯一的布局 - 看起来运行良好。我还成功地将其放入了一个DrawerLayout中。也许你的代码中还有其他方面阻止了你想要的功能。能否分享更多源代码? - CzarMatt
哇,你能左右滑动和上下滑动吗? 我只是使用了FragmentPagerAdapter来在我的分页器中显示片段。你能分享一下你的示例代码吗? - Kyso84
我在这里粘贴了一个简单的布局示例:http://pastebin.com/jXPw6mtf 编辑:您可以在“ViewPager”中加载任何片段。我能够上下滚动视图,以及左右滚动“ViewPager”。 - CzarMatt
显示剩余3条评论
21个回答

-1
实际上,NestedScrollView在CoordinatorLayout中不需要直接成为CollapsingToolbarLayout的兄弟元素。我已经实现了一些非常奇怪的东西。appbar_scrolling_view_behavior在2个嵌套的片段中都可以工作。
我的活动布局:
<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>

            <android.support.design.widget.CollapsingToolbarLayout>

                <include
                    layout="@layout/toolbar"/>

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

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

    <FrameLayout
        android:id="@+id/container"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

在“container” frameLayout 中,我已经填充了这个片段:
<android.support.constraint.ConstraintLayout>

    ... some content ...

    <android.support.v4.view.ViewPager/>

    ...

</android.support.constraint.ConstraintLayout>

而ViewPager内部的片段看起来像这样:

<RelativeLayout>

    <android.support.v7.widget.RecyclerView/>

    ....

</RelativeLayout>

NestedScrollView 可以在 CoordinatorLayout 的子层次结构中嵌套很深,而滚动行为仍然有效,这让我感到惊讶。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接