滚动列表视图时,如何使单个标题粘在屏幕顶部?

cod*_*441 1 android

我有一个列表视图,我正在膨胀两个标题。header1 和下面的 header2 然后下面是列表项。

列表视图的最高值是 match_parent

我怎样才能做到这样,当我滚动列表时,header2 不会从屏幕上消失。以便它到达的最高位置是屏幕顶部,而列表项则在其下方滚动

Sac*_*rma 5

下面的代码对某人有帮助。

1.带有工具栏的代码。

2.工具栏下方的视图(V1)(滚动时会消失)

3.V1下面的另一个视图(V2)(它会粘在工具栏的顶部底部)

4.然后最后一个将滚动..(可能是嵌套滚动视图中的视图,Recyclerview ...等)

暗示:

1.In所述的XML视图(V1)具有滚动应给予的CollapsingToolbarLayout内视图(V2)具有是在顶部粘应给予AppbarLayout的CollapsingToolbarLayout的内部和外部

2.演示中使用了 NestedScrollview 和一些虚拟文本..如果您正在使用 Recyclerview 或类似的东西,请不要忘记放置 app:layout_behavior="@string/appbar_scrolling_view_behavior"

下面给出了 xml。

第一个.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tollbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#c65"
            android:elevation="4dp"
            />
    <include
        layout="@layout/sample"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

示例.xml

<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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <ImageView
                android:id="@+id/rest_logo"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:scaleType="fitXY"
                android:src="@mipmap/ic_launcher"
                app:layout_collapseMode="parallax" />


        </android.support.design.widget.CollapsingToolbarLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#cff"
            android:gravity="center"
            app:layout_collapseMode="pin">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Header Sticking in Top"
                android:textColor="#000" />
        </LinearLayout>
    </android.support.design.widget.AppBarLayout>


    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"

            android:orientation="vertical"
            android:textColor="#000">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="1"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="2"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="3"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="4"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="5"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="6"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="7"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="8"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="9"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="10"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="11"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="12"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="13"
                android:textColor="#000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:background="#ccc"
                android:gravity="center"
                android:text="14"
                android:textColor="#000" />


        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>


</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)