FragmentContainerView详解

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值