当滚动底部导航栏并不掩饰 - BottomNavigationBehavior

问题描述 投票:0回答:2

我想隐藏底部导航栏,所以我使用底部的导航行为,它工作时,我有其他的代码,但在这个似乎不工作我试过,但底部导航栏不会隐藏下面是代码

 package com.blipclap.creativegraphy.Helper;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;

import static java.lang.Float.parseFloat;

public class BottomNavigationBehaviour extends CoordinatorLayout.Behavior {

    public BottomNavigationBehaviour() {
    }

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

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

    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
        child.setTranslationY(Math.max(0f,
                Math.min(Float.parseFloat(String.valueOf(child.getHeight())),child.getTranslationY()+dyConsumed)));
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        if (dependency instanceof Snackbar.SnackbarLayout)
            updateSnackbar(child,dependency);
        return super.layoutDependsOn(parent, child, dependency);
    }

    private void updateSnackbar(View child, View dependency) {
        if (dependency.getLayoutParams()instanceof CoordinatorLayout.LayoutParams) {
            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) dependency.getLayoutParams();

            params.setAnchorId(child.getId());
            params.anchorGravity= Gravity.TOP;
            params.gravity=Gravity.TOP;
            dependency.setLayoutParams(params);

        }
    }
}

这是我的布局内容布局我试图改变相对布局,但不幸的是应用程序停止,所以我不能改变它

<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.blipclap.creativegraphy.HomeActivity"
    tools:showIn="@layout/app_bar_home">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.design.widget.TabLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_above="@+id/navigation"
            android:layout_below="@+id/tabLayout"></android.support.v4.view.ViewPager>

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_gravity="bottom"
            app:layout_behavior=".Helper.BottomNavigationBehavior"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimary"
            app:itemIconTint="@android:color/background_dark"
            app:itemTextColor="@android:color/background_dark"
            app:menu="@menu/bottom_navigation_menu"></android.support.design.widget.BottomNavigationView>
    </RelativeLayout>

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

这是我的3个片段,我使用我时滚动底部导航栏不隐藏类别

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.blipclap.creativegraphy.Fragment.CategoryFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_category"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

每日热门

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.blipclap.creativegraphy.Fragment.CategoryFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_trending"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

最近

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.blipclap.creativegraphy.Fragment.CategoryFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_recent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

这个代码是不是为我工作我应该怎么做任何解决方案

参考

bottom navigation behavior reference 1

bottom navigation behavior reference 2

bottom navigation behavior reference 3

我跟着最后一个,但它不工作

android behavior bottomnavigationview
2个回答
2
投票

有两件事情:

1)你不应该重写onNestedScroll,它应该是onNestedPreScroll,删除onNestedScroll并与更换:

@Override
public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
    super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    child.setTranslationY(Math.max(0f,
            Math.min(Float.parseFloat(String.valueOf(child.getHeight())),child.getTranslationY()+dy)));
}

2)在你的XML文件,你把

app:layout_behavior=".Helper.BottomNavigationBehavior"

而你的类名是

BottomNavigationBehaviour

正如你可以看到一个是行为,而另一种是行为,通常应该抛出一个运行时错误,应用程序应该能够运行,它可能是你一个错字,但我只是在情况下提到。

但要注意,这个代码有缺陷,如果你尝试所有的方式滚动向上或向下,则RecyclerView项目将无法点击了几秒钟,我有一个similar bug。现在,我的选择是使用动画来隐藏BottomNavigationViewthis后解释。

编辑:

这有可能是该行为不适用,因为BottomNavigationView不是CoordinatorLayout的直接孩子,所以你可以完全删除RelativeLayout或采取BottomNavigationView出来:

<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.blipclap.creativegraphy.HomeActivity"
    tools:showIn="@layout/app_bar_home">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.design.widget.TabLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_above="@+id/navigation"
            android:layout_below="@+id/tabLayout"></android.support.v4.view.ViewPager>


    </RelativeLayout>


    <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_gravity="bottom"
            app:layout_behavior=".Helper.BottomNavigationBehavior"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimary"
            app:itemIconTint="@android:color/background_dark"
            app:itemTextColor="@android:color/background_dark"
            app:menu="@menu/bottom_navigation_menu">

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

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

1
投票

bottom layout behavior.Java

package com.utility.behaviour;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;

public class BottomLayoutBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {

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

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

    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V child,
                                  @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        child.setTranslationY(Math.max(0f, Math.min(child.getHeight(), child.getTranslationY() + dy)));
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    }
}

activity_main.xml中

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <include layout="@layout/app_bar" />
    </com.google.android.material.appbar.AppBarLayout>

    <com.utility.viewpager.SwipeOnOffViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bnvHome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/white"
        app:itemIconTint="@color/home_tab_bg"
        app:itemTextColor="@color/home_tab_bg"
        app:labelVisibilityMode="labeled"
        app:layout_anchorGravity="center"
        app:layout_behavior="com.utility.behaviour.BottomLayoutBehavior"
        app:menu="@menu/home_bottom_navigation_items" />
    <!--app:itemBackground="@drawable/home_bottom_bar_gap"-->
</androidx.coordinatorlayout.widget.CoordinatorLayout>

注:android:layout_gravity="bottom"app:layout_behavior="com.utility.behaviour.BottomLayoutBehavior"是强制性的。

© www.soinside.com 2019 - 2024. All rights reserved.