简体   繁体   English

如何使用固定的CardView和RecyclerView将工具栏隐藏在片段中

[英]How to hide the Toolbar in a fragment with a fixed CardView and a RecyclerView

So I have a main layout with a Toolbar, TabLayout, ViewPager and a Floating Action Button, and in one of the fragments layout I want to have a fixed CardView on top and a Recycler View next. 所以我有一个主布局,其中有一个工具栏,TabLayout,ViewPager和一个浮动操作按钮,在其中一个片断布局中,我想在顶部有一个固定的CardView,然后是Recycler View。 At the same time, I want to hide the Toolbar and FAB on scroll in any of the fragments. 同时,我想在滚动中隐藏任何片段的工具栏和FAB。 However, in this fragment, I can't hide the Toolbar once I scroll. 但是,在此片段中,滚动后便无法隐藏工具栏。 It only works if I delete the CardView on top (and I really need him there) 仅当我删除最上面的CardView时才有效(并且我真的需要他在那里)

main activity layout: 主要活动布局:

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<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.AppBarLayout
    android:id="@+id/appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorTeal200"
        android:elevation="6dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"
            android:paddingTop="7dp"
            android:src="@drawable/mealpx2" />
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/colorText"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorTeal500" />
</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/gallery_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="160dp"
    android:src="@drawable/ic_photo_library_white_24px"
    android:visibility="invisible"
    app:backgroundTint="@color/colorAccent700"
    app:borderWidth="0dp"
    app:elevation="12dp"
    app:pressedTranslationZ="12dp" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/camera_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="90dp"
    android:src="@drawable/ic_camera_alt_white_24px"
    android:visibility="invisible"
    app:backgroundTint="@color/colorAccent700"
    app:borderWidth="0dp"
    app:elevation="12dp"
    app:pressedTranslationZ="12dp" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/floating_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_margin="16dp"
    android:src="@drawable/ic_add_white_24px"
    app:backgroundTint="@color/colorAccent700"
    app:borderWidth="0dp"
    app:elevation="12dp"
    app:layout_anchor="@id/view_pager"
    app:layout_anchorGravity="bottom|right|end"
    app:layout_behavior="com.draft.meal.testing.ScrollFABAction.ScrollOffBottomBehaviour" />

fragment layout: 片段布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/pt_card"
        layout="@layout/pt_card" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rec_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pt_card"
        android:padding="3dp">

Thanks for the help 谢谢您的帮助

Try this. 尝试这个。

 (AppCompatActivity) getActivity()).getSupportActionBar().hide(); 

Thank you..! 谢谢..!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用CoordinatorLayout隐藏工具栏,但在片段上使用RecyclerView - Hide Toolbar with CoordinatorLayout, but RecyclerView on a fragment 如何从Recyclerview隐藏Cardview - How to hide a cardview from a Recyclerview 如果我向下滚动片段中的recyclerView,如何自动隐藏工具栏? - How to hide toolbar automatically if I scroll down the recyclerView inside a fragment? 如何在片段中隐藏工具栏? - How to hide toolbar in a fragment? 隐藏带有协调器布局和RecyclerView的工具栏 - Hide Toolbar with Coordinator layout and RecyclerView in a fragment 通过在Fragment中滚动recyclerview来隐藏活动中的工具栏? - Hide toolbar in activity by scrolling a recyclerview inside the Fragment? 如何使用CardView分段运行RecyclerView? - How to run my RecyclerView with CardView in fragment? 如何在带有 TabLayout 的 Fragment 中使用 CardView 行实现 RecyclerView - How to implement RecyclerView with CardView rows in a Fragment with TabLayout 如何将 RecyclerView 和 CardView 实现到片段中? - How can I implement a RecyclerView and a CardView into a fragment? 带有 CardView 和 Firebase 的片段中的 RecyclerView - RecyclerView in a Fragment with CardView and Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM