Fragment.hide()不会隐藏我的片段

时间:2019-04-10 22:05:57

标签: android xml android-fragments kotlin

我已经实现了hide()片段的最基本形式,但是它根本不起作用。

我的首字母onCreate()将所有3个片段添加到活动中(它们都同时显示)。然后,当单击导航项时,我在2个片段上调用hide()-但是它们不会从屏幕上消失-不会有任何变化。

我的活动

class TruthOrDare : FragmentActivity(), BottomNavigationView.OnNavigationItemSelectedListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_truth_or_dare)
        bottomNav_truthordare.setOnNavigationItemSelectedListener(this)
        bottomNav_truthordare.selectedItemId = R.id.action_game
        if (savedInstanceState != null) {
            return
        }
        firstFragment.arguments = intent.extras
        val transaction = supportFragmentManager.beginTransaction()
        transaction.add(R.id.fragment_layout, GameFragment())
        transaction.add(R.id.fragment_layout, LeaderboardsFragment())
        transaction.add(R.id.fragment_layout, OptionsFragment())
        transaction.commit()

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.hide(GameFragment())
        transaction.hide(OptionsFragment())
        transaction.commit()

        return true
    }

我的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:id="@+id/layout_truthordare"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    tools:context=".TruthOrDare">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_truthordare"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimaryDark"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@color/colorPrimaryDark"
        app:title="Level 1" />

    <FrameLayout
        android:id="@+id/fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomNav_truthordare" android:layout_below="@id/toolbar_truthordare">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNav_truthordare"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:itemIconTint="@color/bottom_nav_items"
        app:itemTextColor="@color/bottom_nav_items"
        android:layout_alignParentBottom="true"
        app:menu="@menu/menu" />

</RelativeLayout>

有人可以告诉我为什么hide()不起作用吗?

0 个答案:

没有答案