Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RecyclerView below Toolbar

I have a custom RecyclerView and a toolbar which hide when scrolling down and appears when scrolling up. I have a problem about the position of RecyclerView, it is below the Toolbar, I use the behaviour but it seems not working.

My 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"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        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="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>
like image 596
user4789408 Avatar asked Jul 09 '15 02:07

user4789408


5 Answers

Try this:

<?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"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView"
    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.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways" />

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

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

I have removed the ViewPager and added scrolling behavior to RecyclerView

like image 167
Kise Avatar answered Nov 11 '22 06:11

Kise


If Adding a scrolling behavior does not fix your issue

app:layout_behavior="@string/appbar_scrolling_view_behavior"

THEN TRY THIS

I had to give padding to recyclerview which is equivalent to the toolbar/actionbar(app bar) height.

  android:paddingTop="?attr/actionBarSize"  

add the above line to the recyclerview xml file

like image 38
Mightian Avatar answered Nov 11 '22 07:11

Mightian


When I have included RecyclerView from another layout the same problem occurred. I added this following line on recyclerview and the problem no longer persists.

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

Missing this above line will make this problem.

like image 14
Raviraj Subramanian Avatar answered Nov 11 '22 06:11

Raviraj Subramanian


You need to add this attribute to your RecyclerView:

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

i.e.:

<android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
like image 7
ramon Avatar answered Nov 11 '22 07:11

ramon


trying adding this line in the ReyclerView

  android:layout_marginTop="?attr/actionBarSize"
like image 3
sameer Avatar answered Nov 11 '22 08:11

sameer