Fragment in FrameLayout not showing in CoordinatorLayout

荒凉一梦 提交于 2019-12-05 10:25:52

You need to set a layout behaviour on the FrameLayout to make it work in a CoordinatorLayout.

app:layout_behavior="@string/appbar_scrolling_view_behavior"

In my case in was the android.support.v4.widget.NestedScrollView that was preventing fragment replacement transaction. I replaced NestedScrollView with LinearLayout which had app:layout_behavior="@string/appbar_scrolling_view_behavior" and fragment started working.

 <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:background="@color/activityBackground">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="title"/>

            <fragment
                android:name="com.stxnext.stxinsider.DetailsActivity$EmptyFragment"
                android:id="@+id/activity_details_content_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

  </LinearLayout>

Activity:

    val detailsContentFragment = DetailsListFragment()

    val transaction = fragmentManager.beginTransaction()
    transaction.replace(R.id.activity_details_content_fragment, detailsContentFragment)
    transaction.addToBackStack(null)
    transaction.commit()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!