IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Fragment的使用案例,Fragment动态加载限定符,返回栈, -> 正文阅读

[移动开发]Fragment的使用案例,Fragment动态加载限定符,返回栈,

1. 最简单案例(静态添加方式)

旨在体验流程。在一个Activity中添加左右两个Fragment
left_fragment.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_700"
    >
    <Button
        android:text="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        />
</LinearLayout>

right_fragment.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button
        android:text="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

然后编写各自对应的Fragment子类即可。
LeftFragment.java文件:

public class LeftFragment  extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_fragment, container, false);
        return view;
    }
}

类似的添加RightFragment.java类文件。至于为什么是复写onCreateView方法,这就涉及到fragmentactivity的生命周期对比,简单的图示如下:
在这里插入图片描述
上图来源博客:Activity和Fragment生命周期

观察可以知道,其实只要在fragmentonStart之前均可,但是我们看下onActivityCreated方法:

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

并没有提供可以用于布局实例化的LayoutInflater。而在onCreateView中却提供了这些东西。

然后在Activity.java的布局文件中使用fragment标签的android:name来进行关联:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".TestActivity">

    <fragment
        android:id="@+id/id_leftfragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.weizu.send_message.fragments.LeftFragment"/>

    <fragment
        android:id="@+id/id_rightfragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.weizu.send_message.fragments.RightFragment" />

</LinearLayout>

这里LinearLayout的布局方向设置为水平horizontal,且使用android:layout_weight时候,需要讲对应的自生动态调整方向设置为0dp。最终效果如下:

在这里插入图片描述

2. 最简单案例(动态添加方式)

前面介绍了静态添加方式,这里来使用FragmentLayout来实现代码里面的动态添加。还是上面的案例,我们对右边的fragment不在xml布局文件中关联,修改Activity.java的布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".TestActivity">

    <fragment
        android:id="@+id/id_leftfragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.weizu.send_message.fragments.LeftFragment"/>

    <FrameLayout
        android:id="@+id/id_rightfragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        >
    </FrameLayout>

</LinearLayout>

然后在ActivityonCreate中操作:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    FrameLayout frameLayout = findViewById(R.id.id_rightfragment);
    Fragment fragment = new RightFragment();
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.id_rightfragment, fragment);
    transaction.commit();
}

上面使用布局管理器FragmentManager来进行事物提交,使用了replacecommit

3. 在Fragment中实现返回栈

此时如果如果点击返回按钮,程序会直接退出,如果我们希望按下返回键的时候,可以回到上一个Fragment,应该如何实现?
只需要新增:

transaction.addToBackStack(null); // 新增

即可实现。

那么,这里就有一个常见的面试问题:
Activity中调用replaceaddToBackStack方法使用了后的生命周期:

  • 新替换的Fragment没有在BackStack中,onAttach->onCreate->onViewCreate->onActivityCreated->onStart->onResume;
  • 新替换的FragmentBackStack中,onCreateView->onViewCreated->onActivityCreated->onStart->onResume;
  • 被替换的FragmentonPause->onStop->onDestoryView->onDestory->onDetach;

结合上面的对应生命周期函数图,可以理解,也就是如果已经添加到了BackStack那么就不用在onCreate。注意到onCreateonDestory分别处于正二和倒二位置。

4. 动态加载布局技巧

我们希望程序能够根据设备屏幕的分辨率大小,在运行时候自己决定加载哪个布局文件。

4.1 使用限定符

在这里插入图片描述
在这里插入图片描述

为了适应大屏设备,这里定义一个单页模式和双页模式。
activity_test.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".TestActivity">

    <fragment
        android:id="@+id/id_leftfragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.weizu.send_message.fragments.LeftFragment"/>

</LinearLayout>

然后在res目录下新建一个文件夹layout-large,然后创建一个activity_test.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".TestActivity">

    <fragment
        android:id="@+id/id_leftfragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.weizu.send_message.fragments.LeftFragment"/>

    <FrameLayout
        android:id="@+id/id_rightfragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        >
    </FrameLayout>

</LinearLayout>

Android studio中显示如下:
在这里插入图片描述
就可以实现在不同的设备分辨率是,判断大小进而加载不同布局文件。

但是这个large到底是多大呢?

Android提供了一个更加灵活的限定符,即:最小宽度限定符(smallest-width qualifier)

比如我们可以在res目录下新建layout-sw600dp文件夹,即表示当设备最小宽度为600dp的时候,会加载layout-sw600dp/activity_test布局文件。


Thanks

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-26 12:12:28  更:2021-07-26 12:12:50 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/20 2:26:07-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码