平滑滚动不适用于Android RecyclerView的初始滚动
我正在开发一个Android应用程序,它只在一个设备运行KitKat上运行 我在其他物理平板电脑上使用的RecylerView的平滑滚动功能,而genymotion不幸停止了它需要使用的一个设备 它没有滚动到某个位置,而是经过目标位置,一直滚动到底部,看起来非常糟糕 我能够在RecyclerView类中追踪到抽象SmoothScroller的错误平滑滚动不适用于Android RecyclerView的初始滚动,android,android-recyclerview,android-scroll,linearlayoutmanager,Android,Android Recyclerview,Android Scroll,Linearlayoutmanager,我正在开发一个Android应用程序,它只在一个设备运行KitKat上运行 我在其他物理平板电脑上使用的RecylerView的平滑滚动功能,而genymotion不幸停止了它需要使用的一个设备 它没有滚动到某个位置,而是经过目标位置,一直滚动到底部,看起来非常糟糕 我能够在RecyclerView类中追踪到抽象SmoothScroller的错误 if (getChildPosition(mTargetView) == mTargetPosition) {
if (getChildPosition(mTargetView) == mTargetPosition) {
onTargetFound(mTargetView, recyclerView.mState, mRecyclingAction);
mRecyclingAction.runIfNecessary(recyclerView);
stop();
} else {
Log.e(TAG, "Passed over target position while smooth scrolling.");
mTargetView = null;
}
我使用的是我在网上找到的SnappingLinearLayoutManager,但与Android上的普通LinearLayoutManager进行了交换,仍然存在同样的问题
列表有7个项目(用户一次可以看到4个),我滚动到第5个项目(位置4)
当我滚动到第三个时,我没有收到此错误
同样,在我上下滚动列表一次后,错误停止发生
编辑:
我能够使用layoutManager.scrollToPositionWithOffset();但我正在尝试使用平滑滚动动画来实现这一点
以下是我的一些代码和详细信息:
private void setupMainRecyclerViewWithAdapter() {
mainLayoutManager = new SnappingLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mainListRecyclerView.setLayoutManager(mainLayoutManager);
settingsMainListAdapter = new SettingsListAdapter(SettingsActivity.this,
settingsPresenter.getSettingsItems(),
settingsPresenter);
mainListRecyclerView.setAdapter(settingsMainListAdapter);
mainListRecyclerView.addItemDecoration(new BottomOffsetDecoration(EXTRA_VERTICAL_SCROLLING_SPACE));
}
@Override
public void scrollMainList(boolean listAtTop) {
if(listAtTop) {
mainListRecyclerView.smoothScrollToPosition(4);
moveMainMoreButtonAboveList();
} else {
mainListRecyclerView.smoothScrollToPosition(0);
moveMainMoreButtonBelowList();
}
}
嗯,我意识到已经太晚了,但是我尝试了一些不同的解决方案,找到了一个 在自定义LinearSmoothScroller中,我覆盖了InterimTarget的updateActionForInterimTarget
@覆盖
受保护的void updateActionForInterimTarget(操作){
动作。跳到(位置);
}
它看起来不是很平滑,但与scrollToPositionWithOffset相比不是即时的 只需添加一行即可平滑滚动
recyclerView.setNestedScrollingEnabled(false);
如果调用
recyclerView.smoothScrollToPosition(pos),它将正常工作将立即在UI线程上调用,如果recyclerView
的适配器太忙,无法生成视图项,则调用smoothScrollToPosition
将被错过,因为recyclerView
没有要平滑滚动的数据。因此,最好在后台线程中通过recyclerView.post()
执行此操作。通过调用它,它进入主线程
队列,并在其他挂起任务完成后执行
因此,你应该这样做,这对我的案子很有效:
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollToPosition(pos);
}
});
尝试使用layoutManager.scrollToPosition(索引)代码>相反,很抱歉,我应该说我能够使用layoutManager.scrollToPositionWithOffset();但我正在尝试使用平滑滚动动画来实现这一点。好球@BNK。我将把这个编辑添加到我的问题中。也许他们可以帮助我,是的,我试着从mcochin的博客文章中实现这个解决方案,但是遇到了相同的“平滑滚动时越过目标位置”。错误。我会调查另一个。谢谢你的帮助!为我工作,但有人能解释为什么这样做吗?这只适用于带有嵌套滚动区域的回收器视图