简体   繁体   中英

Collapse layout in a fragment under toolbar

The following is my layout in a Fragment .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragments.TAClaimFragment"
    android:background="@drawable/main_gradient"
    android:orientation="vertical">
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       app:layout_anchor="@id/parent"
       app:layout_anchorGravity="top">
       <android.support.v7.widget.SearchView
           android:layout_width="0dp"
           android:layout_weight="2"
           android:layout_height="?actionBarSize"
           android:layoutDirection="rtl"
           android:id="@+id/ta_search"
           android:animateLayoutChanges="true"
           app:searchIcon="@drawable/ic_search"
           app:closeIcon="@drawable/ic_close"
           android:tooltipText="tooltip"
           app:searchHintIcon="@drawable/ic_search"
           app:queryHint="@string/search_hint"
           />
       <ImageView
           android:layout_width="0dp"
           android:id="@+id/ta_menu"
           android:padding="16dp"
           android:layout_weight="0.3"
           android:layout_height="50dp" />

   </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/ta_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

The RecyclerView has a SearchView and an ImageView above it in a linear layout I want the linear layout to disappear under the toolbar which the Activity containing the fragment has. I am not familiar enough with coordinator layout or collapsing toolbar layout, however I guess the solution lies in the use of the two.

How do I go about it?

I want the linear layout to disappear under the toolbar which the Activity containing the fragment has.

If you are showing the Fragment by a FrameLayout (Or whatever), show the Fragment like following layout: ( FrameLayout inside NestedScrollView for being the layout scrollable)

<CoordinatorLayout>

    <AppBarLayout>
      <Toolbar/>
    </AppBarLayout>

    <NestedScrollView>

    <FrameLayout />

    </NestedScrollView>
</CoordinatorLayout>

And then your Fragment codes (like your current layout codes) in another layout called myfragmentlayout.xml .

So, here, if you are trying to hide the LinearLayout , it will be disappear when you scroll the RecyclerView . Or, you can create a CollpasingToolbarLayout which contains a picture then if you scroll the content, it will be disappear.

Example for CollapsingToolbarLayout with an ImageView inside. (And so much more on SO by CollapsingToolbarLayout tag!):

How to add an ImageView with the title in collapsingtoolbarlayout in Android

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