当页面之一包含折叠的工具栏且fitSystemWindows为true时,Android BottomNavigationBar将移动

时间:2019-07-11 17:53:29

标签: android bottomnavigationview fragmenttransaction

我有一个BottomNavigationView活动。在导航中,我显示/隐藏两个片段,其中一个是空片段,另一个包含设置了android:fitsSystemWindows="true"的折叠式工具栏。

问题是,当我打开折叠的片段选项卡,然后导航回空片段时,底部导航视图向下移动(从设备屏幕移出)。

当我没有将fitSystemWindows设置为true或每次用户更改选项卡都创建一个新片段时,都不会出现问题。但是我需要两个都保留。

屏幕截图-https://photos.app.goo.gl/mnBvh4j9fDrMEDLk6

是否有解决方案来避免这种情况?

这是示例代码,其行为如此

活动

class BottomNavigationActivity : AppCompatActivity() {
private var currentFragment: Fragment? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_bottom_navigation)
    val bottomNavigationView = findViewById(R.id.activity_navigation__bottom_bar) as BottomNavigationView
    bottomNavigationView.setOnNavigationItemSelectedListener {
        when (it.itemId) {
            R.id.action_blank -> openFragment(findFragment("blank")?:BlankFragment(), "blank")
            R.id.action_collapsed -> openFragment(findFragment("collapsing")?:CollapsingFragment(), "collapsing")
        }
        return@setOnNavigationItemSelectedListener true
    }
}

private fun openFragment(fragment: Fragment, tag: String) {
    val fragmentTransaction = supportFragmentManager.beginTransaction()
    currentFragment?.let {
        fragmentTransaction.hide(it)
    }

    if (!fragment.isAdded) {
        fragmentTransaction.add(R.id.container, fragment, tag);
    }
    if (!fragment.isVisible) {
        fragmentTransaction.show(fragment)
    }
    currentFragment = fragment
    fragmentTransaction.commitAllowingStateLoss()
}

private fun findFragment(tag: String): Fragment? {
    return supportFragmentManager.findFragmentByTag(tag)
}
}

布局xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

<FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignBottom="@id/activity_navigation__bottom_bar"/>

<android.support.design.widget.BottomNavigationView
        android:id="@+id/activity_navigation__bottom_bar"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        app:menu="@menu/menu"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

空片段

class BlankFragment : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_blank, container, false)
}
}

布局xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".BlankFragment">

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="blank fragment"/>

</FrameLayout>

正在崩溃的片段

class CollapsingFragment : Fragment() {
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_collapsing, container, false)
}
}

布局xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
            android:id="@+id/fragment_collapsing_toolbar__appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/fragment_collapsing_toolbar__container"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="?android:attr/windowBackground"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                android:fitsSystemWindows="true">


            <android.support.v7.widget.Toolbar
                    android:id="@+id/fragment_collapsing_toolbar__toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:title="collapsing fragment"
                    app:titleMarginStart="72dp"
                    app:titleMarginEnd="72dp"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
            android:id="@+id/fragment_collapsing_toolbar__content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</android.support.design.widget.CoordinatorLayout>

这是菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
<item
        android:id="@+id/action_blank"
        android:enabled="true"
        android:title="blank"
        app:showAsAction="ifRoom" />
<item
        android:id="@+id/action_collapsed"
        android:enabled="true"
        android:title="collapsing"
        app:showAsAction="ifRoom" />
</menu>

谢谢。

0 个答案:

没有答案