简体   繁体   中英

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. At the same time, I want to hide the Toolbar and FAB on scroll in any of the fragments. 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM