RecyclerView:UnsupportedOperationException

时间:2015-04-08 17:12:43

标签: android android-support-library android-recyclerview

我在某些设备上遇到RecyclerView的奇怪问题(目前看来只是三星)。我收到以下错误消息:

java.lang.UnsupportedOperationException: RecyclerView does not support scrolling to an absolute position.
   at android.support.v7.widget.RecyclerView.scrollTo(RecyclerView.java:869)
   at android.view.View.setScrollX(View.java:9945)
   at android.animation.PropertyValuesHolder.nCallIntMethod(PropertyValuesHolder.java)
   at android.animation.PropertyValuesHolder.access$200(PropertyValuesHolder.java:35)
   at android.animation.PropertyValuesHolder$IntPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:825)
   at android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:571)
   at android.animation.ValueAnimator.animationFrame(ValueAnimator.java:1175)
   at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1216)
   at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:524)
   at android.animation.LayoutTransition.startChangingAnimations(LayoutTransition.java:1021)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2206)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
   at android.view.Choreographer.doCallbacks(Choreographer.java:614)
   at android.view.Choreographer.doFrame(Choreographer.java:584)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:146)
   at android.app.ActivityThread.main(ActivityThread.java:5602)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
   at dalvik.system.NativeStart.main(NativeStart.java)

RecyclerView.scrollTo(x,y)中的代码是:

@Override
public void scrollTo(int x, int y) {
    throw new UnsupportedOperationException(
            "RecyclerView does not support scrolling to an absolute position.");
}   

很奇怪这就是实施,但那没关系。问题是,我调用该方法。我使用以下(每this个答案):

LinearLayoutManager manager = (LinearLayoutManager) mActivityList.getLayoutManager();
manager.scrollToPositionWithOffset(0, 20);

我找到的唯一解决方法是this,它建议继承RecyclerView并简单地覆盖有意义的scrollTo方法。但对我来说,这似乎不是一个最佳解决方案,因为它可能导致滚动实际上不会发生在那些设备上。有没有其他人有此解决方案?

2 个答案:

答案 0 :(得分:12)

当我设置android:animatelayoutchanges = true时,它发生在我身上。删除解决了这个问题。发生这种情况还有一个例子。它说它只发生在少数设备上。这个帖子java.lang.UnsupportedOperationException: RecyclerView does not support scrolling to an absolute position

解释了这一点

它适当地回答了问题。

答案 1 :(得分:1)

LinearLayoutManager不支持绝对滚动,它只是将其传递给RecyclerView,它也不支持它。 支持的是滚动到数据集中的特定位置(scrollToPosition()),水平或垂直滚动​​一定量(scrollHorizontallyBy()scrollVerticallyBy()),或者到一个位置加一个偏移量(scrollToPositionWithOffset())。