将TabLayout固定在屏幕顶部

3
我有一个包含视图树的布局,如下所示:
    - ConstraintLayout
     -- TextView
    - WebView
    - TabLayout (1) (3 tabs)
    - ViewPager (1)
    - TabLayout (2) (4 tabs)
    - ViewPager (2)

当用户滚动到ViewPager(1)时,TabLayout(1)将会固定在顶部,并且能够像火币app中下面的GIF所示一样进行交互。如果用户继续滚动到ViewPager(2),它将推出TabLayout(1)并将TabLayout(2)固定在顶部。
我尝试了一些文章,例如(https://dev59.com/kVwY5IYBdhLWcg3wJk8q#44327350--它创建了一个假视图,不能与标题进行交互),以及一些库,例如(https://github.com/emilsjolander/StickyScrollViewItems--相当长时间没有更新),但我不觉得它好。
这方面有什么好的经验吗?我看到很多应用程序使用它,但不确定Google是否原生支持它,也不确定我错过了什么。
任何帮助都将不胜感激。谢谢。
更新于2021年12月13日
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_color">

        // Header 
        <include
            android:id="@+id/layout_header"
            layout="@layout/layout_header" />

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <WebView
                    android:id="@+id/wvChart"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/webview_height" />

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tl1"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/tablayout_height"
                    style="@style/TabLayoutStyle"
                    app:layout_constraintTop_toBottomOf="@id/wvChart"
                    app:tabTextAppearance="@style/TabLayoutTextStyle"/>

                <androidx.viewpager2.widget.ViewPager2
                    android:id="@+id/vpg1"
                    android:minHeight="@dimen/viewpager_market_info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/background_color"
                    app:layout_constraintTop_toBottomOf="@id/tl1"/>

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tl2"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/tablayout_height"
                    style="@style/TabLayoutStyle"
                    app:layout_constraintTop_toBottomOf="@id/vpg1"
                    app:tabTextAppearance="@style/TabLayoutTextStyle"/>

                <androidx.viewpager2.widget.ViewPager2
                    android:id="@+id/vpg2"
                    android:minHeight="@dimen/viewpager_market_info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/background_color"
                    app:layout_constraintTop_toBottomOf="@id/tl2"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>
</layout>

我目前正在使用这个库 StickyScrollView,它的功能正常,但包含一些小缺陷。我仍然希望找到其他更稳定的方法。谢谢。

enter image description here


我的回答有帮助到你吗? :) - Ole Pannier
1个回答

1
由于我没有你的源代码,所以我开始做一个自己的小项目来实现这个。首先,您需要以 CoordinatorLayout 作为基本框架。在其中使用一个 AppBarLayout 作为父元素,它的下一级是一个 CollapsingToolbarLayout ,然后在里面可以放置内容(例如 TextView)。第二个 Toolbar 需要固定 (app:layout_collapseMode="pin")。 接下来,您将继续使用 NestedScrollView ,以避免出现任何故障等,以获得流畅的用户体验。
以下是您的 activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="#6200EA"
            app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
            app:title="Collapsing Toolbar">

            <TextView
                android:layout_width="250dp"
                android:layout_height="40dp"
                android:layout_gravity="center|end"
                android:layout_marginBottom="15dp"
                android:scaleType="centerCrop"
                android:text="Trade your Bitcoins here:"
                android:textSize="18sp"
                android:textStyle="bold"
                app:layout_collapseMode="parallax" />

            <Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />


        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

折叠式工具栏:

collapsed

扩展工具栏:

extended


我认为现在你应该有了如何进一步设计的好灵感。这是同样的原则。就时间而言,我只做了一个粗略版本。在你展示的GIF中,顶部只是一个额外的工具栏,通常称为themes.xml中的"DarkActionbar"。而RecyclerView(在你的例子中是“订单簿”)将作为NestedScrollView的子项添加。干杯!


1
嗨,谢谢你的想法。看起来不符合我的要求。据我所知,它只支持一个标题。我已经在问题上更新了布局结构。如果我错了,请帮助我更正。谢谢。 - Truong Hieu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接