1

I am trying to add a recycler view on a Drawer Layout. The problem is that the RecyclerView, which is in a separate document content_main.xml, is placed behind the Toolbar found in the main document activity_main.xml.

enter image description here

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

<!--Contents-->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--App Bar declaration-->
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="?android:attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <!--Content-->
    <include
        layout="@layout/content_main" />

    <!--FAB-->
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:tint="@color/colorIcons"
        app:srcCompat="@android:drawable/ic_input_add" />

</FrameLayout>

<!--NavigationDrawer content-->
<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:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.ezloop.yesmom.MainActivity"
tools:showIn="@layout/activity_main">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvTaskList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

I tried adding app:layout_behavior="@string/appbar_scrolling_view_behavior" as found in this answer but it did not work. I also changed the height from match_parent to wrap_content

Adrian Lopez
  • 51
  • 1
  • 7
  • You need to add android:layout_marginTop="?attr/actionBarSize" in parent view of your content_main.xml file. It will solve your problem. – Sachin Rajput Apr 22 '18 at 19:43
  • It's rather pointless to have only a `RecyclerView` inside the `CoordinatorLayout`. Instead, the `CoordinatorLayout` should be the outer content `View`. That is, the `AppBarLayout`, the `RecyclerView`, and the `FloatingActionButton` should all be inside the `CoordinatorLayout`. Then the `appbar_scrolling_view_behavior` will work as expected. The `FrameLayout` isn't really necessary. – Mike M. Apr 22 '18 at 19:59
  • Mike, I thought that the FrameLayout was necesary for the NavigationDrawer to work. Would it work without it? – Adrian Lopez Apr 22 '18 at 20:02
  • Yeah, as long as you have only one main content `View` in the `DrawerLayout`, it can be pretty much anything you want. The `CoordinatorLayout` would work just as well as the `FrameLayout` there. – Mike M. Apr 22 '18 at 20:06

1 Answers1

2

In your RecyclerView xml layout add

android.support.v7.widget.RecyclerView
android:clipToPadding="false"
android:paddingTop="56dp"
android:paddingBottom="56dp

Check the question: Android what does the clipToPadding Attribute do?