Search code examples
androidandroid-recyclerviewandroid-appbarlayout

Android recyclerview newly added item was blocked by appbarlayout


I was doing this app on android and noticed that when I add a new item to recyclerview at the top, the item will be blocked by appbarlayout.app screenshot here

For example if I add a new item, it will show as the first item on on top of "444", but after adding I have to scroll down to see it, otherwise it is blocked by the appbarlayout.

Is there a solution to this? Thanks.

layout file:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_scrollFlags="scroll|enterAlways" />

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

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

<android.support.v7.widget.RecyclerView
    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:id="@+id/crime_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.largerexer.aaronc.criminalintent.CrimeListActivity"
    tools:showIn="@layout/activity_fragment"/>

Solution

  • After adding the new item on top, call mRecyclerView.smoothScrollToPosition(0);