Android 滚动listview时,如何使单个标题粘贴到屏幕顶部?

Android 滚动listview时,如何使单个标题粘贴到屏幕顶部?,android,Android,我有一个列表视图,我正在膨胀两个标题。header1和bellow it header2,然后在下面列出项目 listview的高值是匹配父项 我如何使其在滚动列表时,标题2不会从屏幕上消失。因此,当列表项滚动到屏幕顶部时,它的最高位置就是屏幕顶部其他一些人已经为此开发了解决方案。我在谷歌快速搜索中发现了这两个,但肯定还有更多 其他一些公司已经为此开发了解决方案。我在谷歌快速搜索中发现了这两个,但肯定还有更多 下面的代码将对某人有所帮助 1.具有工具栏的代码 2.工具栏下方的视图(V1)(滚

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

listview的高值是匹配父项


我如何使其在滚动列表时,标题2不会从屏幕上消失。因此,当列表项滚动到屏幕顶部时,它的最高位置就是屏幕顶部

其他一些人已经为此开发了解决方案。我在谷歌快速搜索中发现了这两个,但肯定还有更多


其他一些公司已经为此开发了解决方案。我在谷歌快速搜索中发现了这两个,但肯定还有更多


下面的代码将对某人有所帮助

1.具有工具栏的代码

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

3.V1下方另一个视图(V2)(将粘贴到工具栏的顶部和底部)

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

提示:

1.在xml中,必须滚动的视图(V1)应在折叠工具栏布局内部中给出,而视图(V2)必须粘贴在顶部中,并且应在折叠工具栏布局外部和AppbarLayout内部中给出

2.演示使用NestedScrollview和一些虚拟文本。如果您使用的是Recyclerview或类似的东西,请不要忘记放置 app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”

下面给出了xml。

first.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>

sample.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>

下面的代码将对某人有所帮助

1.具有工具栏的代码

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

3.V1下方另一个视图(V2)(将粘贴到工具栏的顶部和底部)

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

提示:

1.在xml中,必须滚动的视图(V1)应在折叠工具栏布局内部中给出,而视图(V2)必须粘贴在顶部中,并且应在折叠工具栏布局外部和AppbarLayout内部中给出

2.演示使用NestedScrollview和一些虚拟文本。如果您使用的是Recyclerview或类似的东西,请不要忘记放置 app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”

下面给出了xml。

first.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>

sample.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>


你找到解决方案了吗?你找到解决方案了吗。这是真的!:)tnx。这是真的!:)