我需要创建一个类似于 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
。 - CzarMattViewPager
和一个NestedScrollView
作为唯一的布局 - 看起来运行良好。我还成功地将其放入了一个DrawerLayout
中。也许你的代码中还有其他方面阻止了你想要的功能。能否分享更多源代码? - CzarMatt