1

我有一个带有 FrameLayout 和 BottomNavigation 的 MainActivity。

它看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/layoutActivityMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        tools:context=".view.main.MainActivity">

    <RelativeLayout
            android:id="@+id/splash"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="100dp">

        <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/imgBgSplash"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/bg_start" />

        <RelativeLayout
                android:id="@+id/box"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_above="@id/bottomBox"
                android:layout_centerHorizontal="true">

            <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/imgIconStart"
                    android:layout_width="98dp"
                    android:layout_height="98dp"
                    android:layout_centerInParent="true"
                    android:alpha="0.9"
                    android:paddingBottom="18dp"
                    android:src="@drawable/ic_icon_start"
                    android:tint="@android:color/white" />

        </RelativeLayout>

        <RelativeLayout
                android:id="@+id/bottomBox"
                android:layout_width="match_parent"
                android:layout_height="190dp"
                android:layout_alignParentBottom="true">

            <com.example.custom.views.CustomProgressBar
                    android:id="@+id/progress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="100dp"
                    android:indeterminateTint="@android:color/white"
                    android:progressTint="@android:color/white" />

            <Button
                    android:id="@+id/btnRetry"
                    style="@style/buttonStyleWhite"
                    android:layout_width="256dp"
                    android:layout_height="54dp"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="100dp"
                    android:text="@string/x_001_start_splash_btn_retry"
                    android:visibility="gone" />
        </RelativeLayout>
    </RelativeLayout>

    <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/navigationBottom" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/navigationBottom"
            style="Widget.BottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:animateLayoutChanges="false"
            android:background="@drawable/bottom_navigation_with_border"
            app:itemIconSize="30dp"
            app:labelVisibilityMode="labeled"
            app:menu="@menu/bottom_navigation" />

</RelativeLayout>

我的问题让我头疼:如果我隐藏然后通过 GONE 显示 BottomNavigationView 然后 VISIBLE ... Framelayout 内的 Fragment 失去它的滚动状态。此外,所有图像视图(带有滑动)都会重新绘制,这会导致一些可见的副作用(倾斜)。

你有什么想法我可以做些什么来不丢失滚动状态并摆脱上述副作用?

这就是我在 MainActivity 中隐藏和显示 BottomNavigationView 的方式:

fun hideBottomNavBar() {
        if (navigationBottom.isVisible) {
            navigationBottom.animate()
                .yBy(navigationBottom.height.toFloat())
                .withEndAction { navigationBottom.visibility = View.GONE }
                .start()
        }
    }

    fun revealBottomNavBar() {
        if (!navigationBottom.isVisible) {
            navigationBottom.animate()
                .yBy(-navigationBottom.height.toFloat())
                .withStartAction { navigationBottom.visibility = View.VISIBLE }
                .start()
        }
    }
4

1 回答 1

0

RelativeLayout尝试使用FrameLayout更改父级,然后我想您不会遇到这个问题,因为 framelayout 会将视图排列在堆栈视图中,最后一个位于顶部。为您的内部 FrameLayout 提供边距底部,您将在其中附加等于 bottomNavigation 高度的片段。试试这个

    <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            andorid:margin_bottom   />

    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/navigationBottom"
            style="Widget.BottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:animateLayoutChanges="false"
            android:background="@drawable/bottom_navigation_with_border"
            app:itemIconSize="30dp"
            app:labelVisibilityMode="labeled"
            app:menu="@menu/bottom_navigation" />

</FrameLayout>

希望这应该有效。

于 2020-01-24T10:42:05.973 回答