[英]LinearLayoutManager#scrollToPositionWithOffset() not working sometimes
@Override
public void onResume() {
super.onResume();
recyclerView.post(new Runnable() {
@Override
public void run() {
((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, viewTopDistance);
}
});
}
position and viewTopDistance are saved and persisted correctly when the user clicks a view which launches another activity. 当用户单击启动另一个活动的视图时,position和viewTopDistance会保存并正确保存。 Upon return to the recycler view about 2/3 of the time the list looks correct, the other 1/3 of the times it just shows the first 4 or 5 times at the top (scrollToPositionWithOffset didn't work) 大约有2/3的时间返回到回收站视图时,列表看起来正确,而其他1/3的时间仅在顶部显示前4或5次(scrollToPositionWithOffset不起作用)
Any ideas on why this fails sometimes? 关于为什么有时失败的任何想法?
I believe your problem is in timing. 我相信您的问题在于时间安排。 Although 'post' delays 'scrollToPositionWithOffset', the view may not be always ready. 尽管“发布”会延迟“ scrollToPositionWithOffset”,但视图可能并不总是准备就绪。 I use the following construct. 我使用以下构造。
mLstView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (mLstView.getChildCount() > 0) {
recyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
llMgr.scrollToPositionWithOffset(position, viewTopDistance);
return true;
}
return false;
}
});
with success (so far). 成功(到目前为止)。 You may also look at the demo code that implements this construct (LstFrag.select). 您还可以查看实现此构造的演示代码 (LstFrag.select)。
Good Luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.