Android:将TabLayout固定到Scrollview的顶部 [英] Android: pin TabLayout to top of Scrollview

查看:87
本文介绍了Android:将TabLayout固定到Scrollview的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看手机上的twitter应用程序.

I was looking at the twitter app on my phone.

您可以看到,当用户向上滚动时,tabLayout实际上只是将自身很好地固定在工具栏的底部,并且根本不会移动.

You can see that when a user scrolls up, the tabLayout actually just pins itself onto the bottom of the toolbar nicely and does not move at all.

我认为也许他们只是通过将应用程序的所有顶部(配置文件图片,自行车在草地上的配置文件壁纸,文本)放入CollapsingToolBarLayout和AppBarLayout中来实现的,但实际上,仅配置文件自行车在草地上的墙纸是CollapsingToolBarLayout和AppBarLayout的一部分,因为这是实际折叠的唯一部分.文本部分只是向上滚动,而tabLayout只是固定在工具栏下方的顶部.

I thought maybe they did it by just putting all of the top part of the app (the profile picture, the profile wallpaper of the bicycle on the grass, the text), into a CollapsingToolBarLayout and AppBarLayout but actually, only the profile wallpaper with the bicycle on the grass is part of the CollapsingToolBarLayout and AppBarLayout as that is the only part that actually collapses. The text part just scrolls up and the tabLayout just pins to the top below the toolbar.

我在twitter应用程序上阅读了鸣谢,看来他们使用了SlidingTabLayout达到了效果. SlidingTabLayout与tabLayout相似,后者在支持库中受支持.

I read the credits on the twitter app and it appears that they used the SlidingTabLayout to achieve their effect. SlidingTabLayout is similar to tabLayout with the latter being supported in the support library.

这看起来也是当今主流应用所使用的一种相当普遍的模式-

It looks like a fairly common pattern that is used by mainstream apps nowadays as well -

Google+应用在该应用的个人资料视图中使用它:

Google+ app uses it in their profile view on the app:

Facebook Moments 在其应用中使用它:

任何想法他们都是如何做到的吗?

Any idea how they did all managed to do this?

我正在寻找与所有这些应用类似的功能.

I'm looking to do something similar to all these apps.

我的要求是:

  1. 有一个collapsingToolBarLayout,当您向上滚动时会折叠
  2. 在collapsingToolBarLayout下具有一个textview,当它完全折叠时,该文本视图将滚动并隐藏"在ToolBar下.
  3. 在textview下方有一个tabLayout,当您在collapsingToolBarLayout下滚动textview时,它将粘贴"到tabLayout.
  4. 在tabLayout下方具有一个recyclerview,以便当您单击tabLayout中的每个选项卡时,recyclerview将在推文",照片",收藏夹"等列表之间切换.

我查看了SO上发布的另外两个问题:

I looked at two other questions that were posted on SO:

滚动时能否将TabLayout的选项卡选择指示器固定在屏幕顶部?.答案似乎是在更改tabLayout的行为,但我怀疑更改行为实际上会产生Twitter应用程序的作用.如前所述,tabLayout似乎位于CollapsingToolBarLayout和AppBarLayout的外部,并且该行为仅当位于CollapsingToolBarLayout和AppBarLayout内时才有效.

Can the tab selection indicator of TabLayout be pinned to the top of the screen while scrolling?. The answer here appears to be changing the behavior of the tabLayout but I doubt changing the behavior would actually generate what the twitter app does. As I mentioned, the tabLayout appears to sit outside the CollapsingToolBarLayout and AppBarLayout and the behavior should only be effective if it is sitting within the CollapsingToolBarLayout and AppBarLayout.

如何将TabLaout固定在ScrollView的顶部? .这个问题的要求与我的要求类似,但没有提供足够的细节,也没有答案.

How to pin TabLaout at top of ScrollView?. This question asks something similar to what I asked, but does not give enough detail and has no answers.

推荐答案

对于前三个问题,请 https://github.com/slidenerd/Android-Design-Support-Library-Demo

for those first 3 questions look here (link seems dead) so this wayback machine link. it points to a github demo repo at https://github.com/slidenerd/Android-Design-Support-Library-Demo

对于第4种情况,您需要为每个选项卡创建片段并在选择它们时以一种简单的方法加载它们,或者您可以创建一个片段并与之通信以在选择选项卡时显示特定内容.

As for the 4th you need to create fragments for each tab and load them when they are selected for a simple approach, or you can create one fragment and communicate with it to show specific content when a tab is selected..

编辑 找不到更新的链接,所以这里是答案

EDIT couldn't find an updated link so here are the answers

  1. 使用CoordinaterLayout.
  2. 您想用工具栏折叠(或隐藏)的任何东西都放在collapsingToolBarLayout内部.
  3. 折叠后的ToolBar剩下的任何东西都会在CollapsingToolbarLayout之后进入AppBarLayout内.

ex-

<android.support.design.widget.CoordinatorLayout
    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:background="@android:color/background_light"
    android:fitsSystemWindows="true"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        >

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

            <ImageView
                android:id="@+id/main.backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                android:src="@drawable/material_flat"
                app:layout_collapseMode="parallax"
                />

            <android.support.v7.widget.Toolbar
                android:id="@+id/main.toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"
                />
         <!-- ADD ANY THING THAT GETS SCROLLED ALL THE WAY UP WITH TOOLBAR -->
        </android.support.design.widget.CollapsingToolbarLayout>

     <!--- ADD TAB_LAYOUT HERE---> 

    </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"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:lineSpacingExtra="8dp"
            android:text="@string/lorem"
            android:padding="@dimen/activity_horizontal_margin"
            />
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:src="@drawable/ic_comment_24dp"
        app:layout_anchor="@id/main.appbar"
        app:layout_anchorGravity="bottom|right|end"
        />
</android.support.design.widget.CoordinatorLayout>

这篇关于Android:将TabLayout固定到Scrollview的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆