Android:隐藏/显示底部导航视图会导致滚动状态丢失

时间:2020-01-23 23:46:48

标签: android scroll

我有一个带有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,然后再看到...则Framelayout内的片段丢失了其滚动状态。同样,所有图像视图(带有滑行)也会重新绘制,从而引起一些可见的副作用(有点倾斜)。

您有什么主意,我该怎么做才能不失去滚动状态并摆脱上述副作用?

这就是我在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()
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试使用 FrameLayout 更改父级RelativeLayout,然后我猜您不会遇到此问题,因为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>

希望这应该可行。