底部导航栏所覆盖的最后一项

6

我有一个问题。我的recyclerview的最后一项被底部导航条遮挡了。底部导航栏在activity中,而recyclerview在fragment中。我找不到答案。

这是我的fragment布局,其中包含recyclerview:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".PromoFragment">

    <!-- TODO: Update blank fragment layout -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/promo_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginBottom="110dp">

    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

这是我在RecyclerView中使用的项布局。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5px">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:id="@+id/card_pertama"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

               <include layout="@layout/card_promo_layout"/>

        </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

这是我的代码结果图片:

enter image description here


希望这能帮助你了解代码的效果。
4个回答

4
android:paddingBottom="56dp"添加到包含RecyclerViewFragment或最接近RecyclerView的父级布局。例如:
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="56dp"
    tools:context=".PromoFragment">

    <!-- TODO: Update blank fragment layout -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/promo_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

P.S. 56dp是根据Material Design规范BottomNavigationView的高度。因此,paddingBottom的值必须与BottomNavigationView的高度相同。


实际上,它起作用了。我之前用的是marginBottom,但不起作用。Padding是完美的。谢谢。 - bobby I.

1

我遇到了同样的问题。我使用RecyclerView的ItemDecoration进行修复,并将最后一个项目的高度偏移设置为底部导航栏的高度(通常与操作栏相同)。例如:

 mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
           @Override
           public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
               int position = parent.getChildAdapterPosition(view);
               if (position == parent.getAdapter().getItemCount() - 1) {
                      outRect.bottom = bottomMargin;
                }
             }
   });

1
将您的底部导航更改为以下内容。
<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/navigationView" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    android:layout_alignParentBottom="true"
    app:menu="@menu/your_menu" />

将你的布局更改为RelativeLayout

使用FrameLayout和RelativeLayout有什么区别?我认为它们的思想是相同的。 - bobby I.
请参考此链接以了解RelativeLayout和FrameLayout之间的区别:https://www.quora.com/What-is-the-difference-between-RelativeLayout-and-FrameLayout - Jimale Abdi

1

您可以在RecyclerView中使用android:clipToPadding="false"属性,并添加android:paddingBottom="导航栏的高度"属性。

    <android.support.v7.widget.RecyclerView
            android:id="@+id/promo_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingBottom="height of the navigation bar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginBottom="110dp"/>

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