简体   繁体   English

滚动 CoordinatorLayout 时如何防止底部导航隐藏

[英]How to prevent bottom navigation from hiding when scrolling a CoordinatorLayout

I added a ListView with a collapsing toolbar at the top and bottom navigation at the bottom.我在顶部添加了一个带有折叠工具栏的 ListView,在底部添加了底部导航。 My problem is that the bottom navigation keeps hiding when scrolling up.我的问题是向上滚动时底部导航一直隐藏。 I want it to stay visible.我希望它保持可见。 I enabled nested scrolling programmatically.我以编程方式启用了嵌套滚动。 I tried many solutions, none of them worked.我尝试了很多解决方案,但都没有奏效。

This is my problem and my XML at the bottom:这是我的问题,我的 XML 在底部:

在此处输入图像描述

This my main.xml这是我的主要.xml

<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/_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapse"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="#000000"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|enterAlways|snap">
            <LinearLayout
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:gravity="center_horizontal|center_vertical"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/textview1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="8dp"
                    android:text="collapsing toolbar"
                    android:textSize="20sp"
                    android:textColor="#FFFFFF"/>
            </LinearLayout>
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <ListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:choiceMode="none"
            android:layout_weight="1"/>
        <LinearLayout
            android:id="@+id/nav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:background="#1565C0"
            android:gravity="center_horizontal|center_vertical"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/textview2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="bottom navigation"
                android:textSize="20sp"
                android:textColor="#FFFFFF"/>
        </LinearLayout>
    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

use a ConstraintLayout as a Root ViewGroup , and wrap in it the CoordinatorLayout , and the BottomNavigation使用ConstraintLayout作为 Root ViewGroup ,并在其中包装CoordinatorLayoutBottomNavigation

Then you can need to add some padding at the end of the ListView然后您可能需要在ListView的末尾添加一些填充

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/_coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/_app_bar"
            android:layout_width="match_parent"
             android:theme="@style/AppTheme.AppBarOverlay"
            android:layout_height="wrap_content">
           
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapse"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:contentScrim="#000000"
                app:layout_scrollFlags="scroll|enterAlways|snap">

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary" 
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

                <LinearLayout
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="horizontal"
                    android:padding="8dp">

                    <TextView
                        android:id="@+id/textview1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="8dp"
                        android:text="collapsing toolbar"
                        android:textColor="#FFFFFF"
                        android:textSize="20sp" />
                </LinearLayout>


            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>

        <ListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="none"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <LinearLayout
        android:id="@+id/nav"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#1565C0"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="8dp"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/textview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="bottom navigation"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

RelativeLayout version RelativeLayout版本

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/_coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/_app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapse"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:contentScrim="#000000"
                app:layout_scrollFlags="scroll|enterAlways|snap">

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay" />

                <LinearLayout
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal|center_vertical"
                    android:orientation="horizontal"
                    android:padding="8dp">

                    <TextView
                        android:id="@+id/textview1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="8dp"
                        android:text="collapsing toolbar"
                        android:textColor="#FFFFFF"
                        android:textSize="20sp" />
                </LinearLayout>


            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>

        <ListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="none"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <LinearLayout
        android:id="@+id/nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#1565C0"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="8dp"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/textview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="bottom navigation"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />
    </LinearLayout>

</RelativeLayout>

Preview (replace ListView with NestedScrollView for simplicity)预览(为简单起见,将ListView替换为NestedScrollView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止jscrollpane滚动到底部? - how to prevent jscrollpane from scrolling to the bottom? 打开幻灯片菜单时隐藏底部导航视图 - hiding bottom navigation view when open slide menu 如何防止被禁用的JMenuItem在被点击时隐藏菜单? - How to prevent a disabled JMenuItem from hiding the menu when being clicked? 如何实现滚动WebView时隐藏工具栏? - How to implement hiding the toolbar when scrolling WebView? 在 CoordinatorLayout 中禁用 AppBarLayout 时 RecyclerView 滚动故障? - RecyclerView scrolling glitches when disabling AppBarLayout in a CoordinatorLayout? 如何在按下箭头键时阻止JScrollPane滚动 - How to prevent JScrollPane from scrolling when arrow keys are pressed 如何在使用CoordinatorLayout时删除导航抽屉在工具栏上投射的阴影 - How to remove the shadow cast by the navigation drawer over the Toolbar when using a CoordinatorLayout 滚动时如何防止`RecyclerView`复制? - How to prevent the duplication of `RecyclerView` when scrolling? 底部导航切换到导航栏时如何更新所选项目 - How to update selected item when bottom navigation switch to Navigation Rail 如何创建从屏幕底部打开的导航? - How to create a navigation that open from bottom of screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM