如何在 android 中滚动时将标签从内容粘贴到屏幕顶部

How to stick tabs from content to top of screen while scrolling in android

我正在设计一种布局,在该布局中,我的 TabLayout 位于某些内容之间,并且在滚动时到达顶部时应粘在顶部(工具栏下方)。 以下是我的布局xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <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.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </android.support.design.widget.AppBarLayout>

        <com.sample.retail.ui.view.StickyScrollView
            android:id="@+id/sticky_scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:stuckShadowDrawable="@drawable/sticky_shadow_default">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:elevation="@dimen/home_activity_views_elevation"
                android:orientation="vertical">

                <RelativeLayout
                    android:id="@+id/home_top_banner_layout"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/home_banner_layout_height">

                    <com.sample.retail.ui.view.viewPager.ScrollerViewPager
                        android:id="@+id/home_banner_view_pager"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:elevation="@dimen/home_activity_views_elevation" />

                    <com.Sample.retail.ui.view.viewPager.SpringTabIndicator
                        android:id="@+id/home_banner_indicator"
                        android:layout_width="@dimen/home_banner_indicator_width"
                        android:layout_height="@dimen/home_banner_indicator_height"
                        android:layout_alignBottom="@id/home_banner_view_pager"
                        android:layout_centerHorizontal="true"
                        android:layout_marginBottom="8dp"
                        android:elevation="12dp"
                        app:stiIndicatorColor="@color/theme_primary"
                        app:stiIndicatorColors="@array/spring_indicator_colors"
                        app:stiRadiusMax="4dp"/>

                </RelativeLayout>

                <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:layout_marginBottom="4dp"
                    android:layout_marginTop="4dp"
                    android:foreground="?android:attr/selectableItemBackground"
                    card_view:cardCornerRadius="2dp"
                    card_view:cardElevation="@dimen/home_activity_views_elevation">

                    <LinearLayout
                        android:id="@+id/home_category_layout"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/home_category_layout_height"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/cat_textView1"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="0.25"
                            android:background="@drawable/grey_ripple"
                            android:drawableTop="@drawable/ic_action_wallet_giftcard"
                            android:gravity="center"
                            android:text="Fashion" />


                        <TextView
                            android:id="@+id/cat_textView2"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="0.25"
                            android:background="@drawable/grey_ripple"
                            android:drawableTop="@drawable/ic_action_phone"
                            android:gravity="center"
                            android:text="Electronics" />

                        <TextView
                            android:id="@+id/cat_textView3"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="0.25"
                            android:background="@drawable/grey_ripple"
                            android:drawableTop="@drawable/ic_action_phone"
                            android:gravity="center"
                            android:text="Sports" />

                        <TextView
                            android:id="@+id/cat_textView4"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="0.25"
                            android:background="@drawable/grey_ripple"
                            android:drawableTop="@drawable/ic_action_list_2"
                            android:gravity="center"
                            android:text="Categories" />

                    </LinearLayout>
                </android.support.v7.widget.CardView>

                <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:layout_marginBottom="4dp"
                    android:foreground="?android:attr/selectableItemBackground"
                    card_view:cardCornerRadius="2dp"
                    card_view:cardElevation="@dimen/home_activity_views_elevation">

                    <LinearLayout
                        android:id="@+id/home_lower_banner_layout"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/home_lower_banner_height"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@+id/lower_banner_firstImageView"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight=".50"
                            android:background="@drawable/shape_bg_strike_thru"
                            android:scaleType="fitXY"
                            android:src="@drawable/banner3" />

                        <ImageView
                            android:id="@+id/lower_banner_secondImageView"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight=".50"
                            android:background="@drawable/shape_bg_strike_thru"
                            android:scaleType="fitXY"
                            android:src="@drawable/banner4" />
                    </LinearLayout>
                </android.support.v7.widget.CardView>

                <LinearLayout
                    android:id="@+id/recently_viewed_text_layout"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/home_view_headertext_layout_height"
                    android:layout_marginBottom="4dp"
                    android:background="#66000000"
                    android:elevation="@dimen/home_activity_views_elevation"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/recently_viewed_item_textView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/recently_viewed"
                        android:textColor="@color/white"
                        android:textSize="@dimen/home_section_header_textSize"
                        android:textStyle="bold" />
                </LinearLayout>  


                <android.support.design.widget.TabLayout
                    android:id="@+id/categoryTabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/theme_primary"
                    android:tag="sticky"
                    app:tabMode="scrollable"
                    app:theme="@style/AppTabTheme" />

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

            </LinearLayout>
        </com.sample.retail.ui.view.StickyScrollView>
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/menu_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

我也尝试过上面的 StickyItemView https://github.com/emilsjolander/StickyScrollViewItems/issues/27 库,但它对我不起作用,因为我很难在 TabLayout 等粘滞视图之间重定向触摸和运动事件。我将在下面的 ViewPager 中使用 Recycler 视图 Tabs.How 我是否可以在 TabLayout 的滚动条上实现此目的?任何帮助将不胜感激。

所以,如果我理解你想要做的是在顶部有一些静态选项卡,在它的底部有一些 recyclerview?然后你不需要用滚动视图封装 recyclerview,因为 recyclerview 自带滚动。你能post一张你想要达到的目标的图片吗?

我通过进一步解决它找到了答案。以下是我的技巧。

<Coodinatorlayout   
    <Appbarlaout -- with marginTop=height of toobar
        <View> --with android:layout_scrollFlags = "scroll"
        <View> --with android:layout_scrollFlags = "scroll"
        <View> --with android:layout_scrollFlags = "scroll"
        <View> --with android:layout_scrollFlags = "scroll"
        <View> --with android:layout_scrollFlags = "scroll"
        .....
        ....
        <View> --with android:layout_scrollFlags = "scroll"
        <TabLayout>--with android:layout_scrollFlags = "scroll|exitUnitlCollapsed|"
    </Appbarlayout>
    <Viewpager>  --with app:layout_behavior="@string/appbar_scrolling_view_behavior"

 <Toobar>  --with gravity top  //this is the main trick part. Put a separate toolbar in parent coordinate layout instead of putting it in AppbarLayout. and set this as support Action bar
</Coordinatorlayout>