CollapsingToolbarLayout 中不同子项的不同滚动行为
Different scrolling behavior on different children in CollapsingTollbarLayout
我在带有 RelativeLayout 的 AppBarLayout 中有 CollapsingToolbarLayout
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="360dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<include android:id="@+id/post_meta"
layout="@layout/item_post_meta_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
app:layout_collapseMode="none"
app:layout_scrollFlags="scroll|snap"
/>
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/post_meta"
android:fitsSystemWindows="true"
android:minHeight="292dp"
android:scaleType="centerCrop"
app:layout_collapseMode="pin"
app:layout_collapseParallaxMultiplier="0.7"
/>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
如您所见,在 RelativeLayout 2 子级中,我想在滚动时做出不同的行为:
向上滚动时,我的布局 (id=post_meta) 在图像和固定到工具栏的图像下滚动。可能吗?如果可以,我该怎么做?
所以,我找到了解决方案并采用了这种方式:
<?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:background="@color/white"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="460dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="460dp"
android:fitsSystemWindows="false"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|snap|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
<include android:id="@+id/post_meta"
layout="@layout/item_post_meta_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="340dp"
app:layout_collapseMode="parallax"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed">
<android.support.v4.view.ViewPager
android:id="@+id/post_pager"
layout_collapseParallaxMultiplier="0.7"
android:layout_width="match_parent"
android:layout_height="340dp"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/circlePageInd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="4dp"
app:fillColor="#E0E0E0"
app:pageColor="#FFF"
app:radius="2dp"/>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:orientation="vertical"
android:paddingBottom="50dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways">
<android.support.v7.widget.RecyclerView
android:id="@+id/chat_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clipToPadding="false"
android:fillViewport="true"
android:minHeight="100dp"/>
</RelativeLayout>
<include layout="@layout/send_container"/>
</android.support.design.widget.CoordinatorLayout>
所以,这里 post_meta 向上滚动时使用 ViewPager 在 RelativeLayout 下滚动布局,向下滚动后使用 ViewPager 滚动出 RelativeLayout。
我在带有 RelativeLayout 的 AppBarLayout 中有 CollapsingToolbarLayout
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="360dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<include android:id="@+id/post_meta"
layout="@layout/item_post_meta_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
app:layout_collapseMode="none"
app:layout_scrollFlags="scroll|snap"
/>
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/post_meta"
android:fitsSystemWindows="true"
android:minHeight="292dp"
android:scaleType="centerCrop"
app:layout_collapseMode="pin"
app:layout_collapseParallaxMultiplier="0.7"
/>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
如您所见,在 RelativeLayout 2 子级中,我想在滚动时做出不同的行为: 向上滚动时,我的布局 (id=post_meta) 在图像和固定到工具栏的图像下滚动。可能吗?如果可以,我该怎么做?
所以,我找到了解决方案并采用了这种方式:
<?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:background="@color/white"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="460dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="460dp"
android:fitsSystemWindows="false"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|snap|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
<include android:id="@+id/post_meta"
layout="@layout/item_post_meta_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="340dp"
app:layout_collapseMode="parallax"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed">
<android.support.v4.view.ViewPager
android:id="@+id/post_pager"
layout_collapseParallaxMultiplier="0.7"
android:layout_width="match_parent"
android:layout_height="340dp"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/circlePageInd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="4dp"
app:fillColor="#E0E0E0"
app:pageColor="#FFF"
app:radius="2dp"/>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:orientation="vertical"
android:paddingBottom="50dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways">
<android.support.v7.widget.RecyclerView
android:id="@+id/chat_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clipToPadding="false"
android:fillViewport="true"
android:minHeight="100dp"/>
</RelativeLayout>
<include layout="@layout/send_container"/>
</android.support.design.widget.CoordinatorLayout>
所以,这里 post_meta 向上滚动时使用 ViewPager 在 RelativeLayout 下滚动布局,向下滚动后使用 ViewPager 滚动出 RelativeLayout。