Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

Getting this error on Samsung GT-S7562 Android 4.0.4. I am not calling scrollTo method through code. It is getting called internally. Please help

java.lang.UnsupportedOperationException: RecyclerView does not support scrolling to an absolute position.
            at android.support.v7.widget.RecyclerView.scrollTo(RecyclerView.java:941)
            at android.view.View.setScrollX(View.java:7010)
            at android.animation.PropertyValuesHolder.nCallIntMethod(Native Method)
            at android.animation.PropertyValuesHolder.access$200(PropertyValuesHolder.java:35)
            at android.animation.PropertyValuesHolder$IntPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:815)
            at android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:476)
            at android.animation.ValueAnimator.animationFrame(ValueAnimator.java:1145)
            at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:551)
            at android.animation.LayoutTransition.startChangingAnimations(LayoutTransition.java:813)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1805)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2632)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4517)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
            at dalvik.system.NativeStart.main(Native Method)
like image 900
Abhayjeet Gupta Avatar asked Feb 10 '15 09:02

Abhayjeet Gupta


3 Answers

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.

like image 128
Aalap Avatar answered Nov 20 '22 01:11

Aalap


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>
like image 23
cohnen Avatar answered Nov 20 '22 01:11

cohnen


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);
}
like image 14
Vova K. Avatar answered Nov 20 '22 02:11

Vova K.