简体   繁体   English

LinearLayoutManager#scrollToPositionWithOffset()有时无法正常工作

[英]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.

相关问题 RecyclerView 上 LinearLayoutManager 的 scrollToPositionWithOffset 不起作用 - scrollToPositionWithOffset from LinearLayoutManager on RecyclerView not working 如果RecyclerView太短,则Android LinearLayoutManager scrollToPositionWithOffset无法正常工作 - android LinearLayoutManager scrollToPositionWithOffset not work if RecyclerView is too short 关于LinearLayoutManager.scrollToPositionWithOffset(int,int) - About LinearLayoutManager.scrollToPositionWithOffset(int,int) scrollToPositionWithOffset不适用于android WearableRecyclerView - scrollToPositionWithOffset not working with android WearableRecyclerView 电话轮换后,LinearLayoutManager setOrientation无法正常工作 - LinearLayoutManager setOrientation after phone rotation not working Android linearLayoutManager.setStackFromEnd()不适用于聊天节目的最后一个元素 - Android linearLayoutManager.setStackFromEnd() not working for chat show last element mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false); 不工作 - mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false); not working setSelectionFromTop有时工作,有时不行 - setSelectionFromTop sometimes working and sometimes not RecyclerView scrollToPositionWithOffset与动画 - RecyclerView scrollToPositionWithOffset with animation Kotlin/RecyclerView:scrollToPositionWithOffset 未显示 - Kotlin/RecyclerView: scrollToPositionWithOffset Not Showing Up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM