Android上的滚动视图

时间:2016-03-10 17:37:14

标签: android layout scrollview

enter image description here 你好!上图显示了我在Android应用程序中开发的一个片段。它由一个滑块(“xxxxxxxxxxxx”),两个按钮(xxxzzz和yyyzzz)和一个列表视图组成。我正在尝试在此片段中添加一个滚动条。我想要实现的行为是:当用户滚动此片段时,首先滑块上升并且dessapear然后两个按钮保持固定在片段的顶部,然后用户才能滚动列表视图。有可能吗?

1 个答案:

答案 0 :(得分:0)

我认为你可以通过CoolapsingToolbarLayout实现它 app:layout_scrollFlags =“scroll | exitUntilCollapsed”将会发挥作用
要使用它,您需要应用支持:设计库。
使用Android Studio,添加:

compile 'com.android.support:design:23.1.1'


示例代码

fragment_layout.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>
相关问题