Home > database >  Scrolling appbar leaving blank space on top of recyclerview
Scrolling appbar leaving blank space on top of recyclerview

Time:10-06

enter image description here

enter image description here

enter image description here

Hi, I have tried to implement the appbar scrolling behavior and it works fine, however, if I scroll when I'm right on the top it seems like there is extra space for one recyclerview row.

activity_main:

<androidx.coordinatorlayout.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="match_parent"
    tools:context=".MainActivity"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@ id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|snap|enterAlways" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/nav_host" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

fragment_recyclerview:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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="match_parent"
    tools:context=".fragments.recyclerviewFragment.RecyclerviewFragment"
    >

    <androidx.recyclerview.widget.RecyclerView
        android:paddingTop="?attr/actionBarSize"
        android:clipToPadding="false"
        android:scrollbars="none"
        android:id="@ id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_gravity="bottom|right"
        app:layout_anchorGravity="bottom|end|right"
        app:layout_anchor="@id/recyclerView"
        android:id="@ id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="24dp"
        android:backgroundTint="@color/green_300_org"
        android:contentDescription="@string/add_a_task"
        android:src="@drawable/ic_baseline_add_24"
        app:tint="@color/black" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

CodePudding user response:

Instead of adding this attribute to the RecyclerView of fragment_recyclerview.xml:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

add it in your activity_main.xml file, and it will work fine.

It will be like:

<include
        layout="@layout/nav_host"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Then you can remove the top padding from your RecyclerView, and you don't need to use CoordinatorLayout in the fragment_recyclerview.xml file as well.

  • Related