代码之家  ›  专栏  ›  技术社区  ›  Mahendra Chhimwal

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

  •  1
  • Mahendra Chhimwal  · 技术社区  · 9 年前

    我有一个布局,其中我的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视图。如何在TabLayout的滚动粘贴上实现这一点?如有任何帮助,将不胜感激。

    2 回复  |  直到 9 年前
        1
  •  1
  •   Mahendra Chhimwal    9 年前

    我通过进一步研究找到了答案。以下是我的诀窍。

    <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>
    
        2
  •  0
  •   CommonSenseCode    9 年前

    所以如果我明白你想做的是在顶部有一些静态标签,在底部有一些回收标签?然后,您不需要用滚动视图封装recyclerview,因为recyclerview自带滚动功能。你能发布你想要实现的目标的图片吗?