Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView log statement: W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead

Tags:

I am updating an old Android project, and now I am getting this log statement from RecyclerView repeatedly:

W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead

It occurs when the recyclerview is populated and displayed for the first time or when the items in the recyclerview are refreshed.

Logcat:

11-05 14:02:23.290 20209-20209/com.mydomain W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
11-05 14:02:23.290 20209-20209/com.mydomain W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
11-05 14:02:23.607 20209-20209/com.mydomain W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
11-05 14:02:23.607 20209-20209/com.mydomain W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
11-05 14:02:23.629 20209-20209/com.mydomain W/RecyclerView: RecyclerView does not support scrolling to an absolute position. Use scrollToPosition instead
[...]  

The line repeats about 40+ times

However, as far as I can tell, in my code I am not scrolling to an absolute position. (Unless perhaps it is a byproduct of something else)

I have not been able to find much information about this warning.

Can anyone provide insight?

like image 681
tenprint Avatar asked Nov 05 '17 19:11

tenprint


2 Answers

I'm seeing exactly the same thing and in my case it's caused by the XML attribute

android:animateLayoutChanges="true"

on the RecyclerView's parent - or programmatically setting a LayoutTransition on the parent view, that does the same thing. See this answer on a different SO question. (It looks like it used to be an exception rather than a log in older Android versions, we're lucky in that regard.)

Removing the attribute fixes it, obviously. If (like me) you want to keep it, try organizing your view in such a way that the direct parent of the RecyclerView does not have it, like Sniper suggests in the post I linked to. Copying his example here in case it gets deleted:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/top_parent_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
  android:id="@+id/header_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:gravity="center"
  android:clickable="true"
  android:animateLayoutChanges="true">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  <ImageView
    android:id="@+id/header_arrow_icon"
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:layout_gravity="end|center_vertical"/>
</FrameLayout>
<android.support.v7.widget.RecyclerView
  android:id="@+id/recycler_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

If (like me) you need the LayoutTransition animation on the RecyclerView, putting a wrapper view between the RecyclerView and the view with the layout animations is enough to get rid of the warnings:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:animateLayoutChanges="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </FrameLayout>
</LinearLayout>

Basically make sure the android:animateLayoutChanges property is not on the RecyclerView's parent.

The actual layout change animation works fine even with the warnings, though. I don't know if the performance impact of adding a layer to your view hierarchy is worth it. A better solution might be to subclass the RecyclerView and override the scrollTo (x, y) method to not print the log, like another answer on the same SO question.

like image 156
orange Avatar answered Sep 21 '22 12:09

orange


Put this line in Adapter constructor, maybe it'll work for you:

this.setHasStableIds(true);
like image 23
Ali Azaz Alam Avatar answered Sep 21 '22 12:09

Ali Azaz Alam