RecyclerView 在屏幕外绘制,无法将底部项目滚动到视图中

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

使用设计支持库 22.2.1,具有以下视图层次结构:

<DrawerLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true">
    <CoordinatorLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent">
        <AppBarLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
            <Toolbar
             android:layout_width="match_parent"
             android:layout_height="?attr/actionBarSize" />
            <TabLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:clipToPadding="false"
             app:tabGravity="fill"
             app:tabMode="scrollable" />
        </AppBarLayout>
        <FrameLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

            <!-- Fragments replaced here -->
            <LinearLayout
             android:orientation="vertical"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
                <CustomDashboardView
                 android:layout_width="match_parent"
                 android:layout_height="120dp" />
                <ViewPager
                 android:layout_width="match_parent"
                 android:layout_height="match_parent">

                    <!-- Tab Fragments go here -->
                    <LinearLayout
                     android:orientation="vertical"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent">
                        <CustomEmptyErrorLayout
                         android:layout_width="match_parent"
                         android:layout_height="match_parent"
                         android:visibility="gone" />
                        <android.support.v7.widget.RecyclerView
                         android:layout_width="match_parent"
                         android:layout_height="match_parent" />
                    </LinearLayout>

                </ViewPager>
            </LinearLayout>

        </FrameLayout>
    </CoordinatorLayout>
    <NavigationView
     android:layout_height="match_parent"
     android:layout_width="wrap_content"
     android:fitsSystemWindows="true" />
</DrawerLayout>

我运行遇到这个问题,RecyclerView的高度大于它应该包含的可见区域,所以看起来RecyclerView在屏幕外绘制,无法滚动最后一个项目在 RecyclerView 中进入全视图。 Toolbar 和 TabLayout 也没有移动(尽管 layout_behaviour 应用于 FrameLayout)。

我曾将此作为错误报告,但 ol' Banesy 表示这是按预期工作的。如果是这种情况,我如何才能避免这种有利于尊重 layout_height="match_parent" 并在可见屏幕内绘制其项目的 RecyclerView 的预期行为? https://code.google.com/p/android/issues/detail?id=182391

更新:现在有了 Design Support v23,它看起来一点也不好。我已将其缩小到设计支持库本身(即,将 RecyclerView、appcompat 等更新到 v23,同时保留设计支持 v22.2.1 会产生与上述相同的问题)。所以新的外观,CustomDashboardLayout 和 RecyclerView 已经擅离职守了,希望这也没有按预期工作:

我已经在 v23 上解决了这个问题,并找到了 v22.2.1 的解决方法。两个版本之间的行为完全不同的事实让我相信 "working as intended" 不是全部事实。

v23 修复:ViewPager 上方的 CustomDashboardLayout 导致了问题,当它没有被强制设置为 visibility="gone" 时,ViewPager 根本没有被添加到层次结构中。随着它的消失,添加了 ViewPager 并且 RecyclerView 正确调整了它的高度。

v22.2.1 解决方法:v23 修复对 v22.2.1 没有影响,解决方法是在 RecyclerView 上设置 layout_marginBottom="?attr/actionBarSize"。

很高兴无论如何都结束了。

即使今天使用此视图层次结构 api 级别 26,我也遇到了同样的问题 -

<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>

花了一整天,我觉得RecyclerView不知何故无法计算出正确的高度。我得出这个结论是因为当我滑动 ViewPagerRecyclerView 正确显示了最后一项。所以我在 activity 中的 ViewPager 上添加了一个 ViewTreeObserver 并请求 RecyclerView 重绘自己。这解决了我的问题。

 val obs = pager?.viewTreeObserver
            obs?.addOnGlobalLayoutListener {
                recyclerView?.requestLayout()
                recyclerView?.invalidate()
            }