【发布时间】:2016-06-25 17:01:24
【问题描述】:
你好!上图显示了我在 Android 应用中开发的片段之一。它由一个滑块(“xxxxxxxxxxxx”)、两个按钮(xxxzzz 和 yyyzzz)和一个列表视图组成。我试图在这个片段中添加一个滚动。我想要实现的行为是:当用户滚动这个片段时,首先滑块上升并消失,然后两个按钮固定在片段的顶部,然后用户才能滚动列表视图。有可能吗?
【问题讨论】:
标签: android layout scrollview
你好!上图显示了我在 Android 应用中开发的片段之一。它由一个滑块(“xxxxxxxxxxxx”)、两个按钮(xxxzzz 和 yyyzzz)和一个列表视图组成。我试图在这个片段中添加一个滚动。我想要实现的行为是:当用户滚动这个片段时,首先滑块上升并消失,然后两个按钮固定在片段的顶部,然后用户才能滚动列表视图。有可能吗?
【问题讨论】:
标签: android layout scrollview
我认为您可以通过 CoolapsingToolbarLayout 实现它。
app:layout_scrollFlags="scroll|exitUntilCollapsed" 会变魔术。
要使用它,您需要应用 support:design library。
使用 Android Studio,添加以下内容:
compile 'com.android.support:design:23.1.1'
示例代码
片段布局.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/your_listview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include
android:id="@+id/toolbar"
layout="@layout/layout_toolbar_colliding" />
<FrameLayout
android:id="@+id/your_two_button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
layout_toolbar_colliding.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CollapsingToolbarLayout
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:fitsSystemWindows="true"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="?attr/actionBarSize"
app:layout_collapseMode="parallax">
<android.support.v4.view.ViewPager
android:id="@+id/your_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/your_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_collapseMode="parallax"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.CollapsingToolbarLayout>
【讨论】: