底部应用栏在使用片段导航时向上/向下滑动(导航架构组件) [英] Bottom App Bar slide up/down on navigating with fragments (Navigation Architecture Components)

本文介绍了底部应用栏在使用片段导航时向上/向下滑动(导航架构组件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看Bottom App Bar,它有一个很好的功能可以将其隐藏在滚动 app:hideOnScroll ="true" 上.问题是我无法弄清楚如何在片段来回导航和从导航时将其在程序上向上或向下滑动.

I was having a look at the Bottom App Bar and there is a nice feature to hide it on scroll app:hideOnScroll="true". The problem it that I cannot figure it out how to slide it up or down progmatically when the fragments are being navigated to and from.

例如,设置概述:在 MainActivity 上具有导航主机片段,并且它承载 MainFragment DetailFragment .

For example, overview of a setup: on MainActivity has the navigation host fragment and it is hosting a MainFragment and a DetailFragment.

MainFragment 具有一个 RecyclerView ,在滚动时,将隐藏底部的应用栏.单击 RecyclerView 的任何一项时,它将导航到 DetailFragment .但是问题在于,底部应用栏仍处于隐藏状态,我希望显示它.

The MainFragment has a RecyclerView, which on scroll, will hide the Bottom App Bar. On clicking any one of the item of the RecyclerView, it navigates to DetailFragment. But the problem is that the Bottom App Bar is still hidden and I want it to be shown.

再说一次:

  • 启动目标具有 RecyclerView ;底部的应用程序栏可见.
  • 在回收者"视图中单击一个项目时,它导航到另一个片段,在该片段中也有一个回收者"视图.滚动回收器视图,底部的应用栏向下滑动.
  • 按下向上按钮返回到起始位置,您会发现底部的应用程序栏仍处于隐藏状态.
  • the Start Destination has a RecyclerView; the bottom app bar is visible.
  • on clicking one of the item in recycler-view, it navigates to another fragment, where it also has a recycler-view. Scroll the recycler-view and the bottom app bar slides down.
  • Press the up button to get back to start destination and you will find that the bottom app bar is still hidden.

是否没有任何方法可以使底部应用栏与导航组件一起上下滑动?


MainActivity 中,我正在使用以下代码来获取 onCreate 中的向上按钮:

Aren't there any methods for bottom app bar to slide up or down with navigation component?


In MainActivity I'm using the following codes in order to get the Up Button, in onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mBottomAppBar = findViewById(R.id.bottom_appbar);
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController);

    // ...
}

@Override
public boolean onSupportNavigateUp() {
    return Navigation.findNavController(this, R.id.nav_host_fragment).navigateUp();
}

activity_main.xml 中,以隐藏底部的应用栏我已启用 app:hideOnScroll ="true" :

In activity_main.xml, to hide the Bottom App Bar I've enabled app:hideOnScroll="true":

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    //...

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_appbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        app:hideOnScroll="true"
        app:layout_anchor="@+id/nav_host_fragment"
        app:layout_anchorGravity="center|bottom" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/bottom_appbar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha06'
    implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha06'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    // ...

推荐答案

我认为您正在寻找BottomAppBar的 perfomShow()方法,如

I think you are looking for the perfomShow() method of the BottomAppBar as documented here. I'm using v1.1.0 too and the method is there but I have not tested it so far. Will update this when I were able to.

错字

该方法有效:视频

这篇关于底部应用栏在使用片段导航时向上/向下滑动(导航架构组件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆