簡體   English   中英

android 在特定片段中隱藏工具欄不閃爍

[英]android hide toolbar in specific fragment without flicker

我想從導航組件堆棧中特定片段的活動中隱藏工具欄。 問題是隱藏與片段更改不同步,所以當我單擊導航到第二個片段時,我有兩個具有類似問題的選項

  1. 工具欄被隱藏,第一個片段拉伸片刻,然后顯示第二個片段。
  2. 工具欄標題已更改,顯示第二個片段,然后隱藏該工具欄。 這發生得很快,但它是可見的

Stackoverflow 提供了 2 種解決方案

  1. 此代碼出現第一個問題:
navController.addOnDestinationChangedListener { _, destination, _ ->
    if(destination.getId() == R.id.full_screen_destination) {
        toolbar.setVisibility(View.GONE)
        bottomNavigationView.setVisibility(View.GONE);
    } else {
        toolbar.setVisibility(View.VISIBLE)
        bottomNavigationView.setVisibility(View.VISIBLE);
    }
}
  1. 此代碼出現第二個問題:
@Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
@Override
public void onStop() {
    super.onStop();
    ((AppCompatActivity)getActivity()).getSupportActionBar().show();
}

這是我當前的活動布局

<androidx.constraintlayout.widget.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:context=".domain.MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:theme="@style/ToolbarTheme">
    </androidx.appcompat.widget.Toolbar>

    <fragment
        android:id="@+id/nav_host"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:navGraph="@navigation/nav_graph_main"
        tools:ignore="FragmentTagUsage" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation_menu"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Navigation Controler 是否有一些解決方案來同步所有內容?

我認為您有兩種解決方法:

a) 將 Toolbar 放入每個片段的布局中,並將其從 Activity 的布局中移除。

這樣做的缺點是,當使用工具欄在兩個片段之間導航時,片段轉換 animation 也將應用於工具欄。 例如,您會看到它隨着片段內容淡出和淡入。 如果您不使用 animation 可能沒問題。

b) 像您一樣在 Activity 中擁有工具欄,但將要繪制的 NavHostFragment 放置在它下面 這意味着更換

app:layout_constraintTop_toBottomOf="@+id/toolbar"

app:layout_constraintTop_toTopOf="parent"

然后將頂部填充應用於將顯示工具欄的每個片段的布局。 例如在fragment_test.xml中:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"  << adding this line
tools:context=".TestFragment">

這感覺是更好的解決方案,但缺點是您可能必須將填充放置到許多片段中。 當您擁有可以具有動態高度的工具欄時,它可能會很復雜(對於此類片段,您可能希望將其與變體 (a) 結合使用 - 在其自己的布局中使用不同的工具欄)。

Nice 可以使用 animation 來隱藏/顯示工具欄以使過渡更柔和。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM