Search code examples
androidandroid-layoutandroid-coordinatorlayoutandroid-appbarlayout

coordinator layout with recycler + button


I am having an issue where I am trying to add a button and a recyclerview to a linearLayout that linear layout is contained in a coordinatorlayout. The issue I am having is when I apply scrolling behavior to the linearLayout the button then scrolls up and down opposite of what the action bar does.Half Actionbar Half Button When I apply scrolling behavior to the recylcerview and the button the top portion of my recylcer view is hidden behind the action bar.

Here is what my layout looks like:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            android:theme="@style/Theme.AppCompat.NoActionBar"
            app:popupTheme="@style/Theme.AppCompat.NoActionBar">

            <android.support.v7.widget.SearchView
                android:id="@+id/recommender_search_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/Theme.AppCompat.NoActionBar"
                app:popupTheme="@style/Theme.AppCompat.NoActionBar"/>

            </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/contacts_recycler_view"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:background="@color/recommender_divider"/>

        <Button
            style="@style/GrapeFullscreenButtonPrimary"
            android:id="@+id/send_button"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Send (0)"/>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

I have messed around with the fitSystemWindow property with no luck and also have tried the scrolling behavior with multiple scenarios as mentioned before. I have been battling this issue for a while now and any help would be highly appreciated.


Solution

  • Ok, you can achieve the same effect using the following technique:


    Change

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/contacts_recycler_view"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:background="@color/recommender_divider"/>
    
        <Button
            style="@style/GrapeFullscreenButtonPrimary"
            android:id="@+id/send_button"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Send (0)"/>
    
    </LinearLayout>'
    

    To

     <android.support.v7.widget.RecyclerView
            android:id="@+id/contacts_recycler_view"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:background="@color/recommender_divider"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:paddingBottom="[your button's height]"/>
    
        <Button
            style="@style/GrapeFullscreenButtonPrimary"
            android:id="@+id/send_button"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_gravity="bottom"
            android:text="Send (0)"/>'
    



    Note:

    appbar_scrolling_view_behavior will move the RecyclerView down as part of its implementation; this is why your Button should have a solid background or you will see part of the RecyclerView beneath it.