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

时间:2015-08-13 01:48:53

标签: android android-recyclerview androiddesignsupport android-coordinatorlayout

使用Design Support Library 22.2.1,具有以下View层次结构:

<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进入全视图。关于工具栏和TabLayout也没有任何动作(尽管layout_behaviour应用于FrameLayout)。

annotated screenshot highlighting the issue

我曾将此作为一个错误报告,但是Banesy表示这是按预期工作的。如果是这种情况,我怎么能避免这种意图的行为支持一个尊重layout_height =&#34; match_parent&#34;的RecyclerView。并在可见屏幕内绘制项目? https://code.google.com/p/android/issues/detail?id=182391

更新:现在使用Design Support v23,它看起来并不好看。我已将其缩小到设计支持库本身(即更新RecyclerView,appcompat,以及v23的任何其他内容,而离开Design Support v22.2.1会产生与上述相同的问题)。因此新的外观,CustomDashboardLayout和RecyclerView已经失败了,希望这不是按预期工作:

enter image description here

2 个答案:

答案 0 :(得分:4)

我已经设法在v23上解决了这个问题,并为v22.2.1找到了解决方法。行为完全不同的版本之间的事实使我相信“​​按预期工作”并非全部事实。

v23修复:ViewPager上方的CustomDashboardLayout导致了问题,当它没有被强制使用visibility =“off”时,ViewPager根本没有被添加到层次结构中。随着它的消失,ViewPager被添加,RecyclerView正确地调整其高度。

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

无论如何,

很高兴。

答案 1 :(得分:0)

即使使用此视图层次结构的api level 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正确显示最后一项时。因此,我在活动中的ViewTreeObserver上添加了ViewPager,并请求RecyclerView重绘自己。这解决了我的问题。

 val obs = pager?.viewTreeObserver
            obs?.addOnGlobalLayoutListener {
                recyclerView?.requestLayout()
                recyclerView?.invalidate()
            }
相关问题