【发布时间】:2015-06-01 18:48:03
【问题描述】:
我需要创建一个类似于 Google Newsstand 的界面,它是一种在折叠标题(垂直滚动)上的 ViewPager(水平滚动)。我的要求之一是使用 Google IO 2015 上展示的新设计支持库。 (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 功能的好处和对单个面板的关注(我可以在 2 个面板之间停止水平)。
现在我没有更多的想法,如果有人可以引导我找到解决方案......
谢谢
这是我最新的布局文件:
<?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。 -
是的,我试过了,但结果还是一样。我对许多组件进行了许多测试,但无论层次结构如何,ViewPager 都不会在 NestedScrollView 中呈现
-
我刚刚创建了一个测试应用程序,其中
ViewPager在NestedScrollView内作为唯一的布局 - 似乎工作正常。我也能够成功进入DrawerLayout。也许您的代码的另一个方面阻止了您想要的功能。愿意分享更多您的资源吗? -
哇,你可以向左/向右和向上/向下滑动?我只是使用 FragmentPagerAdapter 在寻呼机中显示片段。你可以分享你的示例代码吗?
-
我在这里粘贴了一个简单的示例布局:pastebin.com/jXPw6mtf 编辑:您可以在
ViewPager中加载您想要的任何片段。我可以上下滚动视图,也可以左右滚动ViewPager。
标签: android android-layout android-viewpager android-support-library