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

时间:2015-08-10 10:28:08

标签: android android-layout sticky android-tablayout

我正在制作一个布局,其中我的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这样的粘贴视图之间的触摸和动作事件。我将在Tabs下面的ViewPager中使用Recycler视图。如何在TabLayout的滚动粘贴上实现这一点?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

通过更多地解决这个问题,我找到了答案。以下是我为我工作的伎俩。

Jackson

答案 1 :(得分:0)

因此,如果我理解你要做的是在顶部有一些静态标签,并在它的底部有一些recyclerview?然后,您不需要使用scrollview封装recyclerview,因为recyclerview带有自己的滚动。你能发布你想要实现的目标吗?