gpt4 book ai didi

android - onNestedScroll 未使用自定义 BottomNavigationViewBehavior 调用

转载 作者:行者123 更新时间:2023-11-29 02:31:23 25 4
gpt4 key购买 nike

我想实现滚动时隐藏 BottomNavigationView。在谷歌上搜索解决方案,因为 android 框架似乎不存在一个可以开箱即用的解决方案,我当前的设置如下:

<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=".MainActivity">

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:gravity="center"/>

<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:menu="@menu/menu_main">
</android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>

主 Activity :

CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigation.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewBehavior());

BottomNavigationViewBehavior:

public class BottomNavigationViewBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {

private int height;

@Override
public boolean onLayoutChild(CoordinatorLayout parent, BottomNavigationView child, int layoutDirection) {
height = child.getHeight();
return super.onLayoutChild(parent, child, layoutDirection);
}

@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
BottomNavigationView 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 BottomNavigationView child,
@NonNull View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed,
@ViewCompat.NestedScrollType int type) {
if (dyConsumed > 0) {
slideDown(child);
} else if (dyConsumed < 0) {
slideUp(child);
}
}

private void slideUp(BottomNavigationView child) {
child.clearAnimation();
child.animate().translationY(0).setDuration(200);
}

private void slideDown(BottomNavigationView child) {
child.clearAnimation();
child.animate().translationY(height).setDuration(200);
}
}

每次都会调用onLayoutChild,父级是CoordinatorLayout,子级是BottomNavigationView。但 onStartNestedScrollonNestedScroll 从未被调用,因此底部导航 View 不会在滚动时隐藏。尝试切换布局 - fragment ,没有 fragment ,尝试通过 xml 布局设置行为,但没有任何效果。我在这里缺少什么?

最佳答案

您的 xml 应如下所示:

<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=".MainActivity">

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:gravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:nestedScrollingEnabled="true"/>

<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:menu="@menu/menu_main"
app:layout_behavior="BottomNavigationViewBehavior ">
</android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>

放弃这部分:

    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigation.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewBehavior());

你有一个类似的例子here

附言您还应该更改 GridView 并将 RecyclerView 与 GridLayoutManager 一起使用,就像您看到的那样 here

关于android - onNestedScroll 未使用自定义 BottomNavigationViewBehavior 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com