Recyclerview 在 CoordinatorLayout 中不是很明显

Recyclerview isn't quitely visible in CoordinatorLayout

我正在开发一个使用 recyclerview 来显示用户费用的应用程序,但我遇到了一个问题,即在 bottomnavigationview 下方滚动后看不到内容。 这张图片解释了我的问题:

这是我的布局:

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:background="@color/transparent"
    android:backgroundTint="@color/transparent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.AppUiActivity">

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottom_bar"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:layout_gravity="bottom"
            android:gravity="center"
            app:backgroundTint="@color/blue_900"
            android:baselineAligned="false"
            app:fabCradleMargin="3dp"
            app:fabCradleRoundedCornerRadius="40dp"
            app:contentInsetEnd="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetStart="0dp"
            app:hideOnScroll="false">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:baselineAligned="false">

                <LinearLayout
                    android:id="@+id/categories"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:layout_weight="3"
                    android:clickable="true"
                    android:background="?android:selectableItemBackground"
                    android:gravity="center"
                    android:focusable="true">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:padding="4dp"
                        android:text="@string/categories"
                        android:textSize="11.5sp"
                        android:fontFamily="@font/crafty_girls"
                        android:textStyle="bold"
                        android:textColor="@color/white"
                        app:drawableTopCompat="@drawable/ic_expense" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/details"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:layout_weight="3"
                    android:clickable="true"
                    android:focusable="true"
                    android:background="?android:selectableItemBackground"
                    android:gravity="center">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:padding="4dp"
                        android:text="@string/details"
                        android:textSize="12sp"
                        android:textStyle="bold"
                        android:fontFamily="@font/crafty_girls"
                        android:textColor="@color/white"
                        app:drawableTopCompat="@drawable/ic_details" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:layout_weight="3"
                    android:enabled="false"
                    android:background="?android:selectableItemBackground"
                    android:gravity="center" />

                <LinearLayout
                    android:id="@+id/calculate"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:clickable="true"
                    android:focusable="true"
                    android:layout_weight="3"
                    android:background="?android:selectableItemBackground"
                    android:gravity="center">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="@string/calculate"
                        android:textStyle="bold"
                        android:textSize="12sp"
                        android:fontFamily="@font/crafty_girls"
                        android:textColor="@color/white"
                        app:drawableTopCompat="@drawable/ic_calculator" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/total"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:background="?android:selectableItemBackground"
                    android:layout_gravity="center"
                    android:clickable="true"
                    android:focusable="true"
                    android:layout_weight="3"
                    android:gravity="center">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:padding="4dp"
                        android:text="@string/total"
                        android:textSize="12sp"
                        android:textStyle="bold"
                        android:fontFamily="@font/crafty_girls"
                        android:textColor="@color/white"
                        app:drawableTopCompat="@drawable/ic_money" />
                </LinearLayout>
            </LinearLayout>
        </com.google.android.material.bottomappbar.BottomAppBar>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="30dp"
            android:clickable="true"
            android:contentDescription="@string/add_expenses"
            android:elevation="20dp"
            android:focusable="true"
            app:fabSize="normal"
            app:layout_anchor="@id/bottom_bar"
            app:layout_anchorGravity="center"
            app:srcCompat="@drawable/ic_add"
            tools:ignore="ImageContrastCheck" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

所以我需要一个有用的答案,因为我已经处理这个问题一个星期了,但还没有解决方案。

为回收站视图添加边距底部,如下所示,这将给出 BottomAppBar

上方的列表

预览在这里:https://i.stack.imgur.com/Kf809.png

<androidx.coordinatorlayout.widget.CoordinatorLayout

    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="match_parent"
    android:background="@color/transparent"
    android:backgroundTint="@color/transparent">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_gravity="bottom"
        android:baselineAligned="false"
        android:gravity="center"
        app:backgroundTint="@color/black"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp"
        app:fabCradleMargin="3dp"
        app:fabCradleRoundedCornerRadius="40dp"
        app:hideOnScroll="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/categories"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="3"
                android:background="?android:selectableItemBackground"
                android:clickable="true"
                android:focusable="true"
                android:gravity="center">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fontFamily="@font/crafty_girls"
                    android:gravity="center"
                    android:padding="4dp"
                    android:text="@string/categories"
                    android:textColor="@color/white"
                    android:textSize="11.5sp"
                    android:textStyle="bold"
                    app:drawableTopCompat="@drawable/ic_expense" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/details"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="3"
                android:background="?android:selectableItemBackground"
                android:clickable="true"
                android:focusable="true"
                android:gravity="center">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fontFamily="@font/crafty_girls"
                    android:gravity="center"
                    android:padding="4dp"
                    android:text="@string/details"
                    android:textColor="@color/white"
                    android:textSize="12sp"
                    android:textStyle="bold"
                    app:drawableTopCompat="@drawable/ic_details" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="3"
                android:background="?android:selectableItemBackground"
                android:enabled="false"
                android:gravity="center" />

            <LinearLayout
                android:id="@+id/calculate"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="3"
                android:background="?android:selectableItemBackground"
                android:clickable="true"
                android:focusable="true"
                android:gravity="center">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fontFamily="@font/crafty_girls"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="@string/calculate"
                    android:textColor="@color/white"
                    android:textSize="12sp"
                    android:textStyle="bold"
                    app:drawableTopCompat="@drawable/ic_calculator" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/total"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="3"
                android:background="?android:selectableItemBackground"
                android:clickable="true"
                android:focusable="true"
                android:gravity="center">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fontFamily="@font/crafty_girls"
                    android:gravity="center"
                    android:padding="4dp"
                    android:text="@string/total"
                    android:textColor="@color/white"
                    android:textSize="12sp"
                    android:textStyle="bold"
                    app:drawableTopCompat="@drawable/ic_money" />
            </LinearLayout>
        </LinearLayout>
    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:clickable="true"
        android:contentDescription="@string/add_expenses"
        android:elevation="20dp"
        android:focusable="true"
        app:fabSize="normal"
        app:layout_anchor="@id/bottom_bar"
        app:layout_anchorGravity="center"
        app:srcCompat="@drawable/ic_add"
        tools:ignore="ImageContrastCheck" />

    

> *<androidx.recyclerview.widget.RecyclerView
>         android:layout_width="match_parent"
>         android:layout_height="match_parent"
>         android:id="@+id/rvList"
>         android:layout_marginBottom="55dp">
>     </androidx.recyclerview.widget.RecyclerView>*

</androidx.coordinatorlayout.widget.CoordinatorLayout>

只需将 2 个属性添加到您的 recyclerview

android:clipToPadding="false"
android:paddingBottom="50dp" // adjust this as per requirement

当您位于列表底部时,只有这些 属性 生效,它会添加 50 dp 的额外填充,以便 recyclerview 在底部有更多区域可以滚动。

编码愉快!!!

而不是在 marginBottom 上猜测 XML 执行此操作的最佳方法是在计算 BottomBar 的高度后在运行时添加底部填充。

在Java

bottom_bar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
            bottom_bar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            recyclerView.setPadding(recyclerView.getPaddingLeft(), 
            recyclerView.getPaddingTop(), recyclerView.getPaddingRight(), 
            recyclerView.getPaddingBottom()+bottom_bar.getHeight());
 }

在科特林中

bottom_bar.viewTreeObserver.addOnGlobalLayoutListener {
bottom_bar.viewTreeObserver.removeOnGlobalLayoutListener(this)
recyclerView.setPadding(
                recyclerView.paddingLeft,
                recyclerView.paddingTop,
                recyclerView.paddingRight,
                recyclerView.paddingBottom +bottom_bar.height
            )
}

您还需要将以下属性添加到您的 RecyclerView,以便看到您的 Recyclerview 元素在您的底部应用栏后面滚动:

android:clipToPadding="false"