Search code examples
androidandroid-layoutstickyandroid-tablayout

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


I have Working on a layout in which my TabLayout is between some content and should be sticked to top ( below Toolbar) on reaching top while scrolling. Following is my layout 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>

I have also tried StickyItemView https://github.com/emilsjolander/StickyScrollViewItems/issues/27 Library as above but its not gonna work for me as Its pretty hard for me to redirects touch and motion Events between stickked views like TabLayout. I Will use Recycler view in ViewPager below Tabs.How do I achieve this on scroll sticking things of TabLayout? Any help would be appreciated.


Solution

  • I have found a answer by working around it a bit more. Following is my trick working for me.

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