【问题标题】:Android BottomNavigationView partially hides last item in RecyclerViewAndroid BottomNavigationView 部分隐藏 RecyclerView 中的最后一项
【发布时间】:2020-07-20 10:46:50
【问题描述】:

我当前的 Android 应用程序采用了

com.google.android.material.bottomnavigation.BottomNavigationView

app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"

视图内容是一个androidx.recyclerview.widget.RecyclerView

当列表中显示的项目数量不足以启用滚动时,我遇到了问题。

在这种情况下,BottomNavigationView 没有隐藏,因此列表中的最后一项被部分隐藏。

我通过在 RecyclerViewandroid:layout_marginBottom="?attr/actionBarSize" 中添加底部边距解决了这个问题

当我的BottomNavigationView 由于滚动和用户隐藏时,此修复现在会导致问题 已滚动到列表中的最后一项。

由于底部边距,用户会在屏幕底部看到一个空白栏。

有什么办法可以解决这两种情况吗?

我的主要 xml 类似于这个

    <?xml version="1.0" encoding="utf-8"?>
    <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:id="@+id/suggest_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MyActivity">
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/toolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                style="@style/Widget.MaterialComponents.Toolbar.Primary"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary">
    
                <View
                    android:id="@+id/paddingView"
                    style="?android:attr/progressBarStyle"
                    android:layout_width="@dimen/size_menu_progressbar"
                    android:layout_height="@dimen/size_menu_progressbar"
                    android:layout_gravity="end" />
    
                <ProgressBar
                    android:id="@+id/myProgressBar"
                    style="?android:attr/progressBarStyle"
                    android:layout_width="@dimen/size_menu_progressbar"
                    android:layout_height="@dimen/size_menu_progressbar"
                    android:layout_gravity="end"
                    android:visibility="invisible" />
    
            </androidx.appcompat.widget.Toolbar>
    
        </com.google.android.material.appbar.AppBarLayout>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/main_area"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <androidx.fragment.app.FragmentContainerView
                android:id="@+id/navHostFragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:defaultNavHost="true"
android:layout_marginBottom="?attr/actionBarSize"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:navGraph="@navigation/my_graph"
                tools:context=".MyActivity" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/home_bottom_navigation_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:background="?android:attr/windowBackground"
            app:itemIconTint="@color/bottom_navigation_bar_color"
            app:itemTextColor="@color/bottom_navigation_bar_color"
            app:labelVisibilityMode="unlabeled"
            app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
            app:menu="@menu/bottom_nav_menu" />
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

我的内容布局类似于此

<?xml version="1.0" encoding="utf-8"?><!-- Use DrawerLayout as root container for activity -->
<layout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="@dimen/margin5"
        tools:context=".MyFragment">

        <androidx.appcompat.widget.SearchView
            android:id="@+id/search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true" />

        <ViewFlipper
            android:id="@+id/recyclerViewFlipper"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/margin6">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="2">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_centerHorizontal="true"
                        android:src="@drawable/ic_search" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="3">

                    <TextView
                        android:id="@+id/find_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="@dimen/margin4"
                        android:textColor="@color/dark_grey"
                        android:textSize="@dimen/text4"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/find_title_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/find_title"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="@dimen/margin4"
                        android:gravity="center_horizontal"
                        android:textColor="@color/dark_grey"
                        android:textSize="@dimen/text3"
                        android:textStyle="normal" />

                </RelativeLayout>

            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <include
                    layout="@layout/pager_loading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true" />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    tools:listitem="@layout/item_my_item" />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/image"
                        android:layout_width="90dp"
                        android:layout_height="80dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/ic_no_found" />

                    <TextView
                        android:id="@+id/text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="@dimen/margin6"
                        android:textColor="@color/dark_grey"
                        android:textSize="@dimen/text4"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/text2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="@dimen/margin3"
                        android:textColor="@color/dark_grey"
                        android:textSize="@dimen/text4" />
                </LinearLayout>

            </RelativeLayout>

        </ViewFlipper>

    </LinearLayout>

</layout>

【问题讨论】:

  • 你能添加你的xml吗?
  • 您可以将底部边距添加到您的项目布局中,也可以将边距添加到可能有帮助的回收站视图中。
  • 你试过我的解决方案了吗@Hector
  • @Hector 你有什么解决办法吗

标签: android android-bottomnavigationview


【解决方案1】:

您可以使用android:paddingBottomandroid:clipToPadding

只需将android:paddingBottom 设置为BottomNavigation 高度和android:clipToPadding tp false

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingTop="8dp"
    android:paddingBottom="?attr/actionBarSize"
    app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/item_layout" />

【讨论】:

  • 我已经尝试过这个解决方案,但它并没有完全解决我的问题。当我滚动到列表底部并且底部导航视图被隐藏时,最后一项和屏幕底部之间存在间隙。
【解决方案2】:

试试这个-->

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

    private boolean hasFixedLastItemNotVisible = false;

    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        if (!hasFixedLastItemNotVisible &&
                !recyclerView.canScrollVertically(10) &&
                newState==RecyclerView.SCROLL_STATE_IDLE) {
            hasFixedLastItemNotVisible = true;
            recyclerView.getAdapter().notifyDataSetChanged();
        }
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    相关资源
    最近更新 更多