【问题标题】:Scrolling with Collapsing Toolbar and Tabs使用折叠工具栏和选项卡滚动
【发布时间】:2015-12-28 05:33:09
【问题描述】:

我正在尝试使用具有scroll|exitUntilCollapsed 标志的CollapsingToolbarLayout 和具有scroll|enterAlways 滚动标志属性的TabLayout 进行布局。基本上我希望我的工具栏在滚动时被固定并显示和隐藏选项卡。我已经从https://github.com/chrisbanes/cheesesquare 修改了cheesesquare 应用程序。这是我的布局 xml;

<?xml version="1.0" encoding="utf-8"?>

<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="@dimen/detail_backdrop_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            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/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_discuss"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>

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

这是结果;

标签位置不正确。而且他们不关心 enterAlways 属性。

【问题讨论】:

  • 你想让你的 TabLayout 在你的 AppBar 下方的哪个位置?
  • @BhaveshPatadiya 是的。在 AppBar 下方。
  • 再澄清一点,只是为了简化 - 预期的滚动行为是什么?复述是否正确:首先图像以全高可见,然后滚动到工具栏。进一步滚动隐藏标签并保留工具栏?
  • @DmitryArc 是的。确切地。向下滚动会再次显示选项卡。
  • 尝试将您的TabLayout 作为AppBarLayout 的直接子代,而不是在CollapsingToolbarLayout 中。

标签: android android-layout android-toolbar android-coordinatorlayout


【解决方案1】:

this 可以解决你的问题。

只需将android:layout_gravity="bottom" 添加到tablayout 并将android:gravity="top" 添加到工具栏。

【讨论】:

  • 其实是关于工具栏margin_bottom
  • 链接中的示例效果很好,就像在这个 img 中一样
  • 链接已过时
  • @Jimale 你给的链接是垃圾链接,删除这个链接,否则我会投诉你,你在传播垃圾邮件。
  • @Pavel 这不是我的答案,我已经编辑过了
【解决方案2】:
  1. android.support.design.widget.TabLayout 中删除属性app:layout_behavior="@string/appbar_scrolling_view_behavior"app:layout_scrollFlags="scroll|enterAlways" 并添加属性android:layout_gravity="bottom"

  2. 还将android.support.v7.widget.Toolbar 高度设置为104(Toolbar+TabLayout height) 以在collapsed 状态期间同时显示ToolbarTabLayout

这是一个工作示例:

<?xml version="1.0" encoding="utf-8"?>
<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/container"
    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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="256dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false">

            <ImageView
                android:id="@+id/image_header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_toolbar"
                android:layout_width="match_parent"
                android:layout_height="104dp"
                android:minHeight="?attr/actionBarSize"
                android:gravity="top"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:titleMarginTop="13dp" />


            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:tabGravity="fill"
                app:tabMode="scrollable"
                style="@style/MyCustomTabLayout"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:backgroundTint="#f44336"
        android:src="@drawable/ic_maps_my_location" />

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

希望对你有帮助~

【讨论】:

    【解决方案3】:

    我有点不清楚这里要实现什么。

    1. 您不希望 TAB 在滚动时上下移动吗?如果是这样,您想将TabLayout 放在CollapsingToolbarLayout 之外。因为您在CollapsingToolbarLayout 中放入的任何内容都会继续滚动。

    根据您发表的评论,我修改了您的 xml 以模仿 YouTube 屏幕。工具栏被放置在协调器布局之外,因为它被永久固定并且不受滚动的影响。值得一提的是"One note: all views using the scroll flag must be declared before views that do not use the flag. This ensures that all views exit from the top, leaving the fixed elements behind.",这是取自android 博文http://android-developers.blogspot.in/2015/05/android-design-support-library.html。这就是我将工具栏移出 AppBarLayout 的原因。

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>
    
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            android:layout_below="@id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
            </android.support.design.widget.AppBarLayout>
    
            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
        </android.support.design.widget.CoordinatorLayout>
    
    </RelativeLayout>
    

    我已将 TabLayout 设为静态,但您可以使其可滚动。如果这不是您要找的,请用图片说明这里要实现的目标。我很乐意提供帮助。

    【讨论】:

    • 我想要一个类似于 Youtube 应用的布局。您可以在那里检查选项卡的行为。对此还有一个补充;我想要工具栏中的图像。
    • 我正在查看 YouTube 应用的主屏幕。 Toolbar 以 56dp 的恒定高度固定,TabLayout 也以 56dp 的恒定高度固定。滚动视频列表时,它们中的任何一个都没有变化。这里根本没有使用 CollapsingToolbarLayout。再次回到 YouTube 应用程序,工具栏永远不会改变高度(永远不会扩展),因此将图像放在那里是没有意义的。但如果您要在 Collapsing ToolbarLayout 中使用 Image,您可以参考“CheeseDetailActivity.java”及其对应的 xml 布局“activity_detail.xml”。
    • 没有。不是主屏幕。打开某个用户的详细信息屏幕。您可以在那里看到标签(主页、视频、播放列表...)
    • 知道了。我已经更新了我的答案。它模仿您正在谈论的确切 YouTube 屏幕。工具栏被固定,并且 TabLayout 在滚动时上下移动。
    • 正如我上面评论的那样;我需要工具栏中的图像。所以我需要一个折叠的工具栏布局。
    【解决方案4】:

    我在AppbarLayout 之上构建了两个带有视差的样本和一个带有RecyclerView 的样本

    只有视差的是

    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/htab_maincontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
        <!--
        OUTLINE
        <CoordinatorLayout>
            <AppbarLayout>
                   <CollapsingToolbarLayout>
                        <ImageView/>
                     <Toolbar/>
                     <TabLayout/>
                </CollapsingToolbarLayout>
            </ AppbarLayout >
            <NestedScrollView/>
        </CoordinatorLayout>
        -->
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar">
    
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbar"
                android:layout_width="match_parent"
                android:layout_height="256dp"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:contentScrim="?attr/colorPrimary">
    
                <ImageView
                    android:id="@+id/ivHeader"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/header"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    android:layout_gravity="top"
                    android:layout_marginBottom="?attr/actionBarSize" />
    
                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:background="@android:color/transparent"
                    app:tabIconTint="#F57C00"
                    app:tabIndicatorColor="#F57C00"
                    app:tabIndicatorHeight="4dp"
                    app:tabMode="scrollable"
                    app:tabSelectedTextColor="#F5F5F5"
                    app:tabTextColor="#FFE0B2" />
    
            </com.google.android.material.appbar.CollapsingToolbarLayout>
    
        </com.google.android.material.appbar.AppBarLayout>
    
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    而RecyclerView在AppbarLayout上面的那个是

    <?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"
        android:id="@+id/htab_maincontent"
        android:layout_width="match_parent"
        android:background="#EEEEEE"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
        <!--
        OUTLINE
        <CoordinatorLayout>
            <AppbarLayout>
                   <CollapsingToolbarLayout>
                        <ImageView/>
                     <Toolbar/>
                     <TabLayout/>
                </CollapsingToolbarLayout>
            </ AppbarLayout >
            <NestedScrollView/>
        </CoordinatorLayout>
        -->
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar">
    
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbar"
                android:layout_width="match_parent"
                android:layout_height="330dp"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:contentScrim="?attr/colorPrimary">
    
                <ImageView
                    android:id="@+id/ivHeader"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/header"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    android:layout_gravity="top"
                    android:layout_marginBottom="?attr/actionBarSize" />
    
                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:background="@android:color/transparent"
                    app:tabIconTint="#F57C00"
                    app:tabIndicatorColor="#F57C00"
                    android:translationY="-30dp"
                    app:tabIndicatorHeight="4dp"
                    app:tabMode="scrollable"
                    app:tabSelectedTextColor="#F5F5F5"
                    app:tabTextColor="#FFE0B2" />
    
            </com.google.android.material.appbar.CollapsingToolbarLayout>
    
        </com.google.android.material.appbar.AppBarLayout>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/constraintLayout"
            app:behavior_overlapTop="30dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/viewPager2"
                android:layout_width="match_parent"
                android:background="#fff"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:layout_height="match_parent" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    并在滚动时设置TabLayout translationY

    // Check if scrolling up or down
       var initTransitionY = tabLayout.translationY
        tabLayout.post {
            initTransitionY = tabLayout.translationY
        }
    
    appbar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
    
        //Check if the view is collapsed
        if (abs(verticalOffset) >= appbar.totalScrollRange) {
            collapsingToolbar.title = "Collapsed"
        } else {
            collapsingToolbar.title = ""
        }
    
            tabLayout.translationY =
                initTransitionY + initTransitionY * (verticalOffset / appBarLayout.totalScrollRange.toFloat())
    
    })
    

    有关完整示例和其他示例,您可以查看this repo

    【讨论】:

      【解决方案5】:

      将 android:layout_gravity="bottom" 添加到 android.support.design.widget.TabLayout

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-14
        相关资源
        最近更新 更多