【发布时间】:2015-10-15 07:21:02
【问题描述】:
我有一个带有片段的 ViewPager,其中包含一个 RecyclerViews。 CollapsingTollbarLayout 位于 ViewPager 上方。除了在 RecyclerView 上以编程方式滚动之外,一切工作正常。然后 AppBarLayout 或 CollapsingToolbarLayout 没有响应。
这是我的基本布局 xml:
<?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:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_behavior="de.wackernagel.playball.ui.FlingBehaviour">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/collapsingToolbarLayout"
app:contentScrim="?attr/colorPrimary"
app:titleEnabled="false"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:contentDescription="@null"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/showdown"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/doubleActionBarSize"
android:minHeight="?attr/actionBarSize"
android:gravity="top"
app:titleMarginTop="14dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
android:layout_gravity="bottom"
app:tabTextColor="#BEFFFFFF"
app:tabSelectedTextColor="#FFFFFFFF"
app:tabContentStart="@dimen/keyline_2"
app:tabMode="scrollable" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
我的目标是在像普通触摸滚动一样以编程方式滚动时折叠 CollapsingToolbarLayout。
我是这样做的
Activity get Fragment at current viewpager position
Fragment get LayoutManager from RecyclerView
LayoutManager scrollToPosition x
滚动行为在 RecyclerView 上有效,但 CollapsingToolbarLayout 没有响应。
编辑
这是我的片段的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<de.wackernagel.playball.views.EmptyAwareRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"/>
【问题讨论】:
-
我写了一篇关于在检测到滚动时触发折叠的帖子,需要使用 dpad(使用程序滚动)novoda.com/blog/fixing-hiding-appbarlayout-android-tv
-
@ataulm 谢谢你的提示。它为我指明了正确的方向。
-
@ataulm 顺便说一句,您检测程序滚动( !recyclerView.isInTouchMode() )的技术不起作用。由于缺少触摸屏,这可能仅适用于电视。
-
不,由于其他原因它不起作用:P
isInTouchMode如果最后一次用户交互是通过触摸而不是键盘、dpad、远程等,则返回 true。
标签: android android-recyclerview androiddesignsupport android-collapsingtoolbarlayout