java.lang.UnsupportedOperationException: RecyclerView does not support scrolling to an absolute position

I had same problem, for me it was because I had

android:animateLayoutChanges="true"

for my recycler view's parent. I removed that and it worked.


You will get this error on some Samsung, Sony,... Implementation of ICS. Both I've founded that include strange internal view managements. One solution to this is to extend RecyclerView (Or the class that generate the issue) with your own class and use that one. In that class you will basically "eat" all possible render exceptions for these devices. It's ugly, it's awful but it's all what I can do to fix this kind of issues.

For your specific issue with scrollTo....

package com.stackoverflow.customviews;

public class CustomRecyclerView extends RecyclerView{

  private static final String TAG = "CustomRecyclerView";

  public CustomRecyclerView(android.content.Context context) {
    super(context);
  }

  public CustomRecyclerView(android.content.Context context, android.util.AttributeSet attrs) {
      super(context, attrs);
  }

  public CustomRecyclerView(android.content.Context context, android.util.AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
  }

  @Override
  public void scrollTo(int x, int y) {
      Log.e(TAG, "CustomRecyclerView does not support scrolling to an absolute position.");
     // Either don't call super here or call just for some phones, or try catch it. From default implementation we have removed the Runtime Exception trown 
  }
}

Then you can replace in your layout xml this new class:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

<com.stackoverflow.customviews.CustomRecyclerView
    android:id="@+id/my_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:overScrollMode="never"
    android:scrollbars="vertical" />

</FrameLayout>

To make scroll programmatically you need to use LayoutManager. For example LinearLayoutManager has scrollToPositionWithOffset. GridLayoutManager extends LinearLayoutManager - so you also can use this. Code works for me:

RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
   LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
   linearLayoutManager.scrollToPositionWithOffset(0, newTopMargin - defaultMargin);
}

Tags:

Android