底部导航遮挡RecyclerView最后项,如何调整ViewPager或布局解决?
嘿,这个问题我之前也碰到过!RecyclerView最后一项被BottomNavigation遮挡,本质就是内容区域和底部导航的布局层级或尺寸没协调好,给你几个实用的解决办法:
方法一:给ViewPager添加底部内边距(最直接)
最简单的方式就是给承载RecyclerView的ViewPager加上底部内边距,数值对应BottomNavigation的高度。
如果你的BottomNavigation用的是Material Design标准高度(56dp),可以直接在布局里硬编码:
<android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="56dp" android:clipToPadding="false" />
⚠️ 一定要加上android:clipToPadding="false",不然RecyclerView的内容会被裁剪,还是看不到最后一项。
如果BottomNavigation高度是自适应的,就用代码动态计算:
// Java示例 bottomNavigationView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { bottomNavigationView.getViewTreeObserver().removeOnGlobalLayoutListener(this); int navHeight = bottomNavigationView.getHeight(); viewPager.setPadding(0, 0, 0, navHeight); viewPager.setClipToPadding(false); } });
方法二:用布局层级限制ViewPager高度(最稳妥)
如果你的主布局用的是LinearLayout,直接给ViewPager设置layout_weight="1",让它自动占据BottomNavigation之外的所有空间:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 你的AppBarLayout放在这里 --> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <!-- ... AppBar的内容 ... --> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <android.support.design.widget.BottomNavigationView android:id="@+id/bottomNav" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
这种方式从根源上避免了内容被覆盖,因为ViewPager的高度被严格限制在底部导航之上。
方法三:给RecyclerView单独加内边距(更精准)
如果每个Tab对应的Fragment里都有独立的RecyclerView,也可以直接给RecyclerView设置底部padding:
<android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="56dp" android:clipToPadding="false" />
这种方式适合每个Tab布局差异较大的场景,只针对需要的RecyclerView生效。
额外提示:用CoordinatorLayout适配系统窗口
如果你的主布局是CoordinatorLayout,可以给ViewPager的父容器加上android:fitsSystemWindows="true",让布局自动适配底部导航的位置:
<android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v4.widget.NestedScrollView> <android.support.design.widget.BottomNavigationView android:id="@+id/bottomNav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" /> </android.support.design.widget.CoordinatorLayout>
内容的提问来源于stack exchange,提问作者Orhan Avan
相关产品推荐
相关产品推荐

