RecyclerView drawing offscreen, can't scroll bottom item into view

给你一囗甜甜゛ 提交于 2019-12-06 02:00:22

I've managed to fix this on v23 and found a workaround for v22.2.1. The fact between the versions the behaviour is quite different leads me to believe "working as intended" isn't the whole truth.

v23 fix: CustomDashboardLayout above the ViewPager was causing the problem, when it wasn't forced to visibility="gone" the ViewPager wasn't being added to hierarchy at all. With it gone, the ViewPager is added and the RecyclerView sizes its height correctly.

v22.2.1 workaround: the v23 fix has no affect on v22.2.1, the workaround is to set layout_marginBottom="?attr/actionBarSize" on the RecyclerView.

Glad that's over anyway.

I also had the same problem even with the api level 26 today with this view hierarchy -

<android.support.design.widget.CoordinatorLayout>
   <android.support.design.widget.AppBarLayout>
      <android.support.design.widget.CollapsingToolbarLayout>
         <FrameLayout>
            <android.support.v4.view.ViewPager/>
            <TextView/>
         </FrameLayout>
         <android.support.v7.widget.Toolbar>
            <TextView/>
         </android.support.v7.widget.Toolbar>
      </android.support.design.widget.CollapsingToolbarLayout>
   </android.support.design.widget.AppBarLayout>
   <android.support.v7.widget.RecyclerView/>
</android.support.design.widget.CoordinatorLayout>

After spending the whole day I felt that somehow the RecyclerView is not able to calculate the proper height. I arrived on this conclusion as when I swipe the ViewPager the RecyclerView was showing the last item correctly. So I added a ViewTreeObserveron the ViewPager in the activity and requested the RecyclerView to redraw itself. This solved my problem.

 val obs = pager?.viewTreeObserver
            obs?.addOnGlobalLayoutListener {
                recyclerView?.requestLayout()
                recyclerView?.invalidate()
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!