【问题标题】:BottomNavigationView is not hiding when scroll滚动时BottomNavigationView不隐藏
【发布时间】:2019-03-20 04:39:12
【问题描述】:

我正在开发一个 android 应用程序,其中定义了以下布局以显示顶部导航视图、抽屉和底部导航视图。但是,当 recyclerview 中的项目滚动时,我无法隐藏我的底部导航视图。

根据我已将app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" 放在BottomNavigationView 中的可用帮助,但由于BottomNavigationViewConstraintLayout 中,它似乎没有按预期工作。

我也尝试使用 CoordinatorLayout 代替 ConstraintLayout,但它不起作用,当我这样做时,我的应用程序挂起。

我也尝试使用嵌套在 ConstraintLayout 内的 CoordinatorLayout,但我的应用程序在这里也挂起。

感谢任何帮助。 content_dashboard.xml 的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".DashboardActivity"
    tools:showIn="@layout/app_bar_dashboard">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        android:foreground="?attr/selectableItemBackground"
        app:itemBackground="@color/bgBottomNavigation"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/menu_bottom_navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>

</android.support.constraint.ConstraintLayout>

app_bar_dashboard.xml的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    tools:context="com.mobiapp.hospmgmt.DashboardActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.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" />

    </android.support.design.widget.AppBarLayout>

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


</android.support.design.widget.CoordinatorLayout>

我已插入以下自定义代码来隐藏 BottomNavigationView,但应用程序挂起。任何建议。

package com.mobiapp.hospmgmt;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

public class BottomNavigationBehaviour extends CoordinatorLayout.Behavior<BottomNavigationView> {

    public BottomNavigationBehaviour() {
        super();
    }

    public BottomNavigationBehaviour(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, BottomNavigationView child, View dependency) {
        boolean dependsOn = dependency instanceof FrameLayout;
        return dependsOn;
    }

    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, BottomNavigationView child, @NonNull View directTargetChild, @NonNull View target, int nestedScrollAxes, int type)
    {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }

    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull BottomNavigationView child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed,
                               @ViewCompat.NestedScrollType int type)
    {
        if (dyConsumed > 0) {
            hideBottomNavigationView(child);
        } else if (dyConsumed < 0) {
            showBottomNavigationView(child);
        }
    }


    private void hideBottomNavigationView(BottomNavigationView view) {
        view.animate().translationY(view.getHeight());
    }

    private void showBottomNavigationView(BottomNavigationView view) {
        view.animate().translationY(0);
    }
}

【问题讨论】:

    标签: android android-layout bottomnavigationview


    【解决方案1】:

    我修复了它,删除了 content_dashboard.xml 并将所有内容放在协调器布局中,还用自定义行为替换了 hide_bottom_view_on_scroll_behavior

    【讨论】:

    • 让我试试建议的更改
    • 请参阅上面的代码更改。应用程序挂起,它不工作,任何建议。我得到的错误是致命异常:主进程:com.mobiapp.hospmgmt,PID:14429 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.mobiapp.hospmgmt/com.mobiapp.hospmgmt.DashboardActivity}:java.lang。 ClassCastException:android.support.constraint.ConstraintLayout$LayoutParams 无法转换为 android.support.design.widget.CoordinatorLayout$LayoutParams
    • 如果我从合并的 .xml 文件中删除 CoordinatorLayout,系统会挂起。任何进一步的建议。
    • 你必须从 BottomNavigationView 中移除 belove 参数。解决命运异常。 app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"
    【解决方案2】:

    要使其工作,您必须将 BottomNavigationView 放置为 CoordinatorLayout 布局的直接子级。修改您的布局,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.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.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.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" />
    
        </android.support.design.widget.AppBarLayout>
    
        <include layout="@layout/content_dashboard" />
    
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="?android:attr/windowBackground"
            android:foreground="?attr/selectableItemBackground"
            app:itemBackground="@color/bgBottomNavigation"
            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white"
            app:menu="@menu/menu_bottom_navigation"
            app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
    </android.support.design.widget.CoordinatorLayout>
    

    ConstraintLayout为根在你的@layout/content_dashboard中,并将RecyclerView和其他视图放在那里:

    <?xml version="1.0" encoding="utf-8"?>
    <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="match_parent"
        tools:showIn="@layout/app_bar_dashboard">
    
        <android.support.v7.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/recycler"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
            />
            ....
            ....
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案3】:

      在合并以上两个 .xml 文件后,我通过将 BottomNavigationView 移出 ConstraintLayout 解决了这个问题。 感谢@Vishrut Mavani 的支持

      【讨论】:

        猜你喜欢
        • 2017-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多