RecyclerView绘制超出屏幕,无法滚动到底部项目视图。

6

使用Design Support Library 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 的最后一项使其完全显示。工具栏和选项卡布局也没有移动(尽管 FrameLayout 上应用了 layout_behaviour)。

annotated screenshot highlighting the issue

我已经将这个问题报告为一个bug,但是Banesy老师说这是按照预期工作的。如果是这样,我该如何避免这种预期行为,而选择一个Respect Layout_height="match_parent"并在可见屏幕内绘制其项目的RecyclerView? https://code.google.com/p/android/issues/detail?id=182391 更新:现在使用Design Support v23,情况看起来不太好。我已经缩小了问题范围,发现是Design Support库本身引起的问题(即将RecyclerView、appcompat和其他内容更新到v23,同时保留Design Support v22.2.1会产生与上述描述相同的问题)。所以新的外观,CustomDashboardLayout和RecyclerView都消失了,希望这也不是按照预期工作的:

enter image description here


真是不可思议,五年后它仍然以同样的方式运作。不幸的是,你发布的两种解决方案对我都不起作用。 - Vucko
2个回答

6
我已经成功解决了v23版本的问题,并为v22.2.1版本找到了一个解决方法。由于版本之间行为差异很大,我认为“按照意图工作”并不是全部的真相。
v23修复:位于ViewPager上方的CustomDashboardLayout引起了问题,如果没有强制将其设置为visibility="gone",ViewPager根本没有被添加到层次结构中。去掉它后,ViewPager被添加了,RecyclerView正确调整了高度。
v22.2.1解决方法:v23修复对v22.2.1没有影响,解决方法是在RecyclerView上设置layout_marginBottom="?attr/actionBarSize"。 总之,很高兴这件事情解决了。

我在RecyclerView上添加了layout_marginBottom="?attr/actionBarSize"。这解决了布局第一次创建时的问题,但是在旋转后,它实际上会将额外的空间添加到屏幕底部,而不仅仅是允许最后一个项目显示。有什么想法吗? - Luke Allison
@LukeAllison,你正在使用v22.2.1吗?你应该转而使用最新版本。同时,更多了解你的具体情况也会很有趣。 - straya
我正在使用23,但运行不了。我正在使用fragments。有趣的是,在Android Studio中提供的Master/Detail-flow模板没有这个问题。我们能通过电子邮件/ Facebook/ Skype等方式聊一下吗? - Luke Allison
@LukeAllison 在这个网站上提出一个新问题,并在这些评论中链接它。 - straya
http://stackoverflow.com/questions/35762827/recyclerview-and-toolbar-cant-scroll-bottom-item-into-view - Luke Allison

0

我今天在使用这个视图层次结构时,即使是在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无法计算正确的高度。我得出这个结论是因为当我滑动ViewPager时,RecyclerView没有正确地展示最后一个条目。因此,在活动中为ViewPager添加了ViewTreeObserver并要求RecyclerView重绘自己。这解决了我的问题。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接