Android 第十四章 FragmentContainerView

这篇具有很好参考价值的文章主要介绍了Android 第十四章 FragmentContainerView。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  1. FragmentContainerView extends FrameLayout
  2. FragmentContainerView是专门为Fragments设计的自定义布局。它扩展了FrameLayout,因此它可以可靠地处理Fragment 事务,并且它还具有与Fragment 行为协调的附加特性
  3. FragmentContainerView应用作Fragments的容器,通常设置在活动的xml布局
  4. FragmentContainerView将只允许Fragment的Fragment.onCreateView返回的视图。尝试添加任何其他视图将导致IllegalStateException
  5. 对于17以上的API,FragmentContainerView禁用布局动画和转换。否则,应通过FragmentTransaction.setCustomAnimations完成动画。如果animateLayoutChanges设置为true或直接调用setLayoutTransition,则将引发UnsupportedOperationException
  6. 使用退出动画的Fragment 在FragmentContainerView的所有其他Fragment 之前绘制。这样可以确保退出的Fragment 不会出现在视图的顶部。
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView 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/fcv"
    android:name="com.krd.fragment.FirstFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:tag="first_tag"
    tools:context=".MainActivity">

</androidx.fragment.app.FragmentContainerView>
public class MainActivity extends AppCompatActivity {

    private Button btn_First, btn_Second;

    private FragmentTransaction fragmentTransaction;
    private FirstFragment firstFragment = null;
    private SecondFragment secondFragment = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initClick();
        initFragment();
    }

    private void initView() {
        btn_First = findViewById(R.id.btn_First);
        btn_Second = findViewById(R.id.btn_Second);
    }

    private void initData() {
    }

    private void initClick() {
        btn_First.setOnClickListener(v -> {
            showFragment(firstFragment);
        });
        btn_Second.setOnClickListener(v -> {
            showFragment(secondFragment);
        });

    }

    private void initFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        firstFragment = new FirstFragment();
        fragmentTransaction.add(R.id.fcv, firstFragment);
        secondFragment = new SecondFragment();
        fragmentTransaction.add(R.id.fcv, secondFragment);
        fragmentTransaction.commit();
        showFragment(firstFragment);
    }

    private void hideFragment() {
        if (null != firstFragment) {
            fragmentTransaction.hide(firstFragment);
        }
        if (null != secondFragment) {
            fragmentTransaction.hide(secondFragment);
        }
    }

    private void showFragment(Fragment fragment) {
        if (null != fragment) {
            fragmentTransaction = getSupportFragmentManager().beginTransaction();
            hideFragment();
            fragmentTransaction.show(fragment);
            fragmentTransaction.commit();
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_First"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="firstFragment"
            android:textAllCaps="false"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btn_Second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:text="secondFragment"
            android:textAllCaps="false"
            android:textSize="25sp" />
    </LinearLayout>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fcv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="@color/teal_200" />

</LinearLayout>

文章来源地址https://www.toymoban.com/news/detail-513720.html

到了这里,关于Android 第十四章 FragmentContainerView的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 第十四章 json模块

    第十四章 json模块

    Python 中的json 模块提供了对JSON 的支持,用于将JSON 格式字符串转换为Python 对象。首先需要了解一下什么是JSON。 什么是JSON JSON 是基于JavaScript 语言的轻量级的数据交换格式,是JavaScript 对象的表示法(JavaScriptObject Notation),它是用来存储和交换文本信息的。信息表示格式为:

    2024年02月09日
    浏览(13)
  • [C国演义] 第十四章

    [C国演义] 第十四章

    力扣链接 常见的子数组问题 ⇒ 要使用动态规划的解法 那么要确定dp数组的含义 ⇒ do[i] — — 以 s[i] 结尾的子数组可不可以用 wordDict中的字符串来表示 那么问题来了, 如何判断字符串[j, i] 在没在wordDict中呢? 我们可以用一个 哈希表 . 将wordDict导入一个哈希表中, count 判读一个

    2024年02月08日
    浏览(10)
  • 第十四章 ObjectScript - 系统函数

    本节重点介绍 ObjectScript 中一些最常用的系统函数。 这些函数的名称不区分大小写。 类库还提供了大量实用方法,可以像使用函数一样使用它们。 在给定一些输入的情况下,可以使用以下函数来选择一个值: $CASE 将给定的测试表达式与一组比较值进行比较,然后返回与匹配

    2024年02月10日
    浏览(14)
  • Nodejs 第十四章(process)

    Nodejs 第十四章(process)

    process 是Nodejs操作当前进程和控制当前进程的API,并且是挂载到globalThis下面的全局API API 介绍 1. process.arch 返回操作系统 CPU 架构 跟我们之前讲的os.arch 一样 \\\'arm\\\' 、 \\\'arm64\\\' 、 \\\'ia32\\\' 、 \\\'mips\\\' 、 \\\'mipsel\\\' 、 \\\'ppc\\\' 、 \\\'ppc64\\\' 、 \\\'s390\\\' 、 \\\'s390x\\\' 、以及  \\\'x64\\\' 2. process.cwd() 返回当前的工作目

    2024年02月10日
    浏览(11)
  • 第十四章 使用Vercel部署在线文档

    第十四章 使用Vercel部署在线文档

    文档网站需要发布到互联网上才能让更多的人知道。传统的发布方法需要做以下准备。 Linux服务器; 网页服务软件 Nginx; 购买域名 + 实名认证; HTTPS 证书; Sftp 上传工具; Github Action CI 自动发布最新文档。 这里面租用服务器和域名需要一笔花费。安装 Linux、Nginx,配置域名

    2024年02月07日
    浏览(14)
  • 《微服务实战》 第十四章 RabbitMQ应用

    《微服务实战》 第十四章 RabbitMQ应用

    第十六章 Spring cloud stream应用 第十五章 RabbitMQ 延迟队列 第十四章 RabbitMQ应用 一般MQ用于系统解耦、削峰使用,常见于微服务、业务活动等场景。 RabbitMQ整体上是一个生产者与消费者模型,主要负责接收、存储和转发消息。 Producer:生产者,就是投递消息的一方。消息一般可

    2024年02月06日
    浏览(10)
  • 第十四章 Unity 移动和旋转(下)

    第十四章 Unity 移动和旋转(下)

    本章节我们介绍另外两种形式的旋转,也对应了两个方法。首先是RotateAround方法,他是围绕穿过世界坐标中的 point 点的 axis轴旋转 angle 度。这个方法虽然比较晦涩难懂,但是我们使用一个案例,大家就非常明白了。我们创建一个新的“SampleScene5”场景,然后创建一个“Cube”

    2024年02月08日
    浏览(14)
  • 第十四章 TIM基本定时器

    第十四章 TIM基本定时器

    目录 13.1 定时器的分类 13.2 TIM基本定时器简介 13.2.1 定时器的概念和作用 13.2.2 TIM基本定时器的工作原理和使用场景 13.3 TIM基本定时器功能框图 13.3.1 时钟源 13.3.2 控制器 13.3.3 时基(定时器的心脏) 13.3.4 影子寄存器 13.4 TIM基本定时器的初始化和配置方法 13.4.1 定时时间的计算

    2024年02月05日
    浏览(12)
  • 【Rust】Rust学习 第十四章智能指针

    【Rust】Rust学习 第十四章智能指针

    指针  ( pointer )是一个包含内存地址的变量的通用概念。这个地址引用,或 “指向”(points at)一些其他数据。Rust 中最常见的指针是第四章介绍的  引用 ( reference )。引用以   符号为标志并借用了他们所指向的值。除了引用数据没有任何其他特殊功能。它们也没有任

    2024年02月12日
    浏览(13)
  • 第十四章 开放条件下的宏观经济

    第十四章 开放条件下的宏观经济

    国际收支是指一个经济体的居民与非居民之间因各种经济往来而发生的收入和支付的系统记录。 国际收支是一个经济概念。 国际收支反映的是以货币数量记录的全部国际经济交易。商品和服务买卖、物物交换、金融资产之间的交换、无偿的单项产品和服务的转移、无偿的单

    2024年02月09日
    浏览(11)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包