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

狂风中的少年 提交于 2020-01-13 10:15:10

问题


Using Design Support Library 22.2.1, with the following View hierarchy:

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

I run into this problem where the RecyclerView's height is larger than the visible area in which it should be contained, so it looks like the RecyclerView is drawing offscreen, and it is impossible to scroll the last item in the RecyclerView into full view. There is also no movement regarding the Toolbar nor TabLayout (although a layout_behaviour is applied to the FrameLayout).

I had reported this as a bug, but ol' Banesy has stated this is working as intended. If that is the case, how can I avoid this intended behviour in favour of a RecyclerView that respects layout_height="match_parent" and draws its items within the visible screen? https://code.google.com/p/android/issues/detail?id=182391

UPDATE: now with Design Support v23, it's not looking good at all. I've narrowed this down to the Design Support lib itself (i.e. updating RecyclerView, appcompat, whatever else to v23 while leaving Design Support v22.2.1 yields the same problem as described above). So the new look, the CustomDashboardLayout and RecyclerView have gone AWOL, hopefully this isn't working as intended either:


回答1:


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.




回答2:


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()
            }


来源:https://stackoverflow.com/questions/31978217/recyclerview-drawing-offscreen-cant-scroll-bottom-item-into-view

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