FragmentContainerView 位于androidx.fragment.app包下面,是专门为Fragment设计的自定义布局。它扩展了FrameLayout,所以它可以可靠地处理Fragment事务,并且它还有其他功能来协调Fragment行为。FragmentContainerView应该被用作fragment的容器,通常在一个activity的xml布局中设置,例如:
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.fragment.app.FragmentContainerView>
FragmentContainerView也可以通过使用android:name属性来添加一个Fragment。FragmentContainerView将执行一次操作:
创建Fragment的新实例
调用Fragment.onInflate(Context, AttributeSet, Bundle)
执行FragmentTransaction将Fragment添加到适当的FragmentManager
你可以选择包括一个android:tag,它允许你使用FragmentManager#findFragmentByTag(String)来检索添加的Fragment。
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.MyFragment"
android:tag="my_tag">
</androidx.fragment.app.FragmentContainerView>
FragmentContainerView不应该被用作Fragment用例之外的其他viewgroup (FrameLayout, LinearLayout等)的替代。
FragmentContainerView只允许由Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle)返回的view。试图添加任何其他视图将导致IllegalStateException。
对于大于17的api, FragmentContainerView禁用布局动画和transitions。否则,动画应该通过FragmentTransaction.setCustomAnimations(int, int, int, int)完成。如果animateLayoutChanges被设置为true或setLayoutTransition(LayoutTransition)被直接调用,将抛出UnsupportedOperationException。
在FragmentContainerView中,使用exit动画的片段在所有其他动画之前绘制。这确保了退出片段不会出现在视图的顶部。
————————————————
版权声明:本文为CSDN博主「challenge51all」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/challenge51all/article/details/116848243