【问题标题】:How can I make a single header stick to the top of the screen when scrolling a listview?滚动列表视图时,如何使单个标题粘在屏幕顶部?
【发布时间】:2014-03-25 23:23:58
【问题描述】:

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

listview的high是match_parent

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

【问题讨论】:

  • 你找到解决办法了吗??

标签: android


【解决方案1】:

以下代码将对某人有所帮助。

1.带有工具栏的代码。

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

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

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

提示:

1.在 xml 中,view(V1) 必须滚动应该在 CollapsingToolbarLayout 中给出,并且 view(V2) 有be to sticked in top应该在CollapsingToolbarLayout之外和AppbarLayout里面给出。

2. 演示使用 NestedScrollview 和一些虚拟文本..如果您使用 Recyclerview 或类似的东西,请不要忘记放 app:layout_behavior="@string/appbar_scrolling_view_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>

示例.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.这是真的! :)
【解决方案2】:

其他几个人已经为此开发了解决方案。我通过 Google 快速搜索找到了这两个,但肯定还有更多。

https://github.com/emilsjolander/StickyListHeaders

https://github.com/JimiSmith/PinnedHeaderListView

【讨论】:

    猜你喜欢
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2010-11-16
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多