Android --- Fragment

概念

Fragment表示 Activity 中的行为或用户界面部分。

  • 可以将多个片段(片段就是指 Fragment )组合在一个 Activity 中来构建多窗格 UI,以及在多个 Activity 中重复使用某个片段。
  • 可以将片段视为 Activity 的模块化组成部分,它具有自己的生命周期,能接收自己的输入事件,并且可以在 Activity 运行时添加或移除片段(有点像在不同 Activity 中重复使用的“子 Activity”)。
  • 片段必须始终嵌入在 Activity 中,其生命周期直接受宿主 Activity 生命周期的影响。 例如,当 Activity 暂停时,其中的所有片段也会暂停;当 Activity 被销毁时,所有片段也会被销毁。
  • 当您将片段作为 Activity 布局的一部分添加时,它存在于 Activity 视图层次结构的某个 ViewGroup 内部,并且片段会定义其自己的视图布局。您可以通过在 Activity 的布局文件中声明片段,将其作为 <fragment> 元素插入您的 Activity 布局中,即静态添加
  • 或者通过将其添加到某个现有 ViewGroup,利用应用代码进行动态插入,即动态添加。不过,片段并非必须成为 Activity 布局的一部分;您还可以将没有自己 UI 的片段用作 Activity 的不可见工作线程。

生命周期

一个Activity可以同时组合多个Fragment,一个Fragment也可被多个Activity 复用。Fragment可以响应自己的输入事件,并拥有自己的生命周期,但它们的生命周期直接被其所属的Activity的生命周期控制。

  • onAttach():当该Fragment被添加到Activity时被回调。该方法只会被调用一次。
  • onCreate(Bundle savedStatus):创建Fragment时被回调。该方法只会被调用一次。
  • onCreateView():每次创建、绘制该Fragment的View组件时回调该方法,Fragment将会显示该方法返回的View 组件。
  • onActivityCreated():当 Fragment 所在的Activity被启动完成后回调该方法。
  • onStart():启动 Fragment 时被回调。
  • onResume():恢复 Fragment 时被回调,在onStart()方法后一定会回调 onResume()方法。
  • onPause():暂停 Fragment 时被回调。
  • onStop():停止 Fragment 时被回调。
  • onDestroyView():销毁该 Fragment 所包含的View组件时调用。
  • onDestroy():销毁 Fragment 时被回调。 该方法只会被调用一次。
  • onDetach():将该 Fragment 从Activity中删除、替换完成时回调该方法,在onDestroy()方法后一定会回调 onDetach()方法。该方法只会被调用一次。

Fragment 加载

静态加载

直接在布局文件中完成配置,通过android:name属性指定 Fragement 的路径

 1.在需要的布局(activity_fragment.xml)中添加fragment的控件

<?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"
    android:gravity="center_horizontal"
    tools:context=".FragmentActivity">
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.androidstudiostudy.Fragment1"
        android:layout_width="200dp"
        android:layout_height="200dp"/>
</LinearLayout>

此时的添加的fragment控件没有样式

2.创建一个空的Fragment类,并删除这个新Fragment类中的多余代码只留 onCreateView(),Fragment创建视图时会调用这个方法

@Override
    // onCreateView -> Fragment 创建视图时调用的方法
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // 将指定的布局文件填充到该Fragment的视图中,并返回该视图
        // 使用LayoutInflater将布局文件转换为View对象,并将该View对象添加到指定的ViewGroup中。最后,它返回该View对象作为Fragment的视图
        /* 注意!!
         * LayoutInflater 的 inflate() 方法,它的作用是把xml 布局转换为对应的 View 对象
         * findViewById则是从布局文件中查找一个控件*/
        return inflater.inflate(R.layout.fragment_1, container, false);
    }

注意!LayoutInflater和findViewById的不同

LayoutInflater 的 inflate() 方法,它的作用是把 xml 布局转换为对应的 View 对象 ,而findViewById则是从布局文件中查找一个控件

3.在fragment类中绑定的布局文件中设置fragment样式(fragment_1.xml)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#00ffff"
    tools:context=".Fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

4.静态加载

利用fragment控件中的android:name=“……”属性指定fragment的路径。

<!-- fragement 的静态加载
         通过android:name属性指定 Fragement 的路径
    -->
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.androidstudiostudy.Fragment1"
        android:layout_marginTop="20dp"
        android:layout_width="200dp"
        android:layout_height="200dp"/>

动态加载

1.在布局文件中添加存放fragment的container,有一个专门的container:androidx.fragment.app.FragmentContainerView

<?xml version="1.0" encoding="utf-8"?>
<!-- 这是一个添加了 Fragment 的xml布局文件-->
<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:layout_marginRight="10dp"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".FragmentActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment2"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="20dp" />

</LinearLayout>


2.在需要添加Fragment的布局文件对应的activity文件中添加以下代码:

  • FragmentManager:用来管理Activity中的fragment
  • FragmentTransaction:事务,用来添加,移除,替换fragment,

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//开启事务
fragmentTransaction.add(参数1,参数2);

参数1:ViewGroup,即应放置片段的位置,由资源 ID 指定
参数2:要添加的片段(Fragment实例)

fragmentTransaction .add() ://往Activity中添加一个Fragment

fragmentTransaction .remove() ://从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈,
//这个Fragment实例将会被销毁。

fragmentTransaction .replace()://使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~

fragmentTransaction .hide()://隐藏当前的Fragment,仅仅是设为不可见,并不会销毁

fragmentTransaction .show()://显示之前隐藏的Fragment

fragmentTransaction .commit()://提交一个事务

一旦通过 FragmentTransaction 做出了更改,就必须调用 commit() 以使更改生效。

package com.example.androidstudiostudy;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;

// 创建一个新的activity 绑定布局 R.layout.activity_fragment ,在该布局里添加 fragment 控件,进行展示
public class FragmentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // 用Java代码添加fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 使用add()方法添加一个fragment片段
        Fragment2 fragment2 = Fragment2.newInstance(null,null);
        fragmentTransaction.add(R.id.fragment2,fragment2);
        fragmentTransaction.commit();
    }
}

 动态加载 - 替换Fragment

使用 FragmentTransaction.replace() 方法替换 Fragment

  • 参数1:containerViewId 应放置片段的位置
  • 参数2:要替换的片段
// FragmentTransaction.replace() 方法替换 Fragment
    public void replaceFragment(View view) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment1 newFragment = new Fragment1(); // 创建一个新的Fragment实例
        fragmentTransaction.replace(R.id.fragment2,newFragment);
        fragmentTransaction.commit();
    }

Fragment与Activity通信

  1. 使用接口(Interface):在Fragment中定义一个接口,然后在Activity中实现该接口。Fragment可以通过调用接口中的方法来与Activity进行通信。

  2. 使用广播(Broadcast):Fragment可以发送广播,而Activity可以注册广播接收器来接收Fragment发送的广播消息。

  3. 使用ViewModel:使用Android Architecture Components中的ViewModel来共享数据。Fragment和Activity可以通过观察ViewModel中的LiveData对象来实现通信。

  4. 使用EventBus:EventBus是一个开源的事件总线库,可以用于在Fragment和Activity之间进行事件通信。

  5. 使用Handle:在Fragment被添加到Activity时,获取到Activity中定义的Handle。

Activity向Fragment传值

利用控制器传递参数

1.实例化一个 fragment 对象

2.实例化一个Bundle对象

3.存入数据到Bundle对象中

4.调用Fragment 的 setArgument方法,传入 Bundle 对象

5.添加或者替换显示的Fragment

 // 法一.通过Bundle来传递参数
    public void commit(View view) {
        /* 注意!! FragmentManager 和 FragmentTransaction 不能变成全局变量,会报错 */
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 1.实例化一个 fragment 对象,注意这里实例化的不是
        Fragment2 f2 = new Fragment2();
        // 2.实例化一个Bundle对象
        Bundle bundle = new Bundle();
        // 3.存入数据到Bundle对象中
        bundle.putString("AtoF1","这是activity向fragment传递的第一个消息");
        // 4.调用Fragment 的 setArgument方法,传入 Bundle 对象
        f2.setArguments(bundle);
        // 5.添加或者替换显示的Fragment
        fragmentTransaction.replace(R.id.fragment2,f2);
        fragmentTransaction.commit();
    }

在fragment类获取数据

getArguments() 得到传回来的值

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Activity 向 Fragment 传值 - getArguments() 得到传回来的值,返回的实际是一个 Bundle
        Bundle bundle = getArguments();
        // 和Activity之间传值一样,是什么类型就get什么类型
        String msg1 = bundle.getString("AtoF1");
        // 根据布局id得到视图
        View v = inflater.inflate(R.layout.fragment_2, container, false);
        // 设置视图
        TextView textView = v.findViewById(R.id.textView2);
        textView.setText(msg1);
        // 返回设置好的视图
        return v ;
    }
利用环境上下文

利用Activity 和 Fragment二者相互关联,可以相互获取对方的对象这一特性。利用环境上下文可以访问到对方的方法。

onAttach 方法会得到环境上下文 context,根据这个context可以获取宿主activity的方法和变量

在宿主activity中定义方法,定义要传递的值

   // 法二.利用context环境上下文 和 onAttach ,在 Fragment1中获取数据
    /* 在 Activity 和 Fragment 建立关系的时候,onAttach 方法会得到环境上下文 context,根据这个context可以获取宿主activity的方法和变量*/
    public String getTitles(){
        return "这是通过环境上下文 和 onAttach 进行传值的";
    }

在对应Fragment的onAttach方法中,将当前环境变量强转成宿主Activity,从而访问到刚刚宿主activity定义的接收数据的方法,获取数据。

 @Override
    public void onAttach(@NonNull Context context) {
        Log.d("FragmentLife","onAttach-----Fragment与activity关联");
        // 强转成宿主activity
        String msg = ((FragmentActivity)context).getTitles();
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
        super.onAttach(context);
    }

Fragment向activity传值

 利用接口
  • 定义一个接口,在这个接口中声明一个用于传递数据的方法
  • 让接收数据的activity实现该接口,然后重写回调方法,目的:获取传入的值并做处理
  • 在自定义fragment中,声明一个回调接口的引用
  • 在onAttach()方法中,为第三步的引用赋值(可以把activity的对象赋值给它)
  • 用引用调用传递数据的方法,本质上调用的是activity中的那个方法,此时就可以把数据传递进去

本质是:在 Fragment 中定义接口和传递参数的方法,在activity中实现接口并重写接口中的方法
在 Fragment 中的声明一个回调接口的引用,因为 Activity 实现了 CommitData 接口的,所以可以将 Activity 转换为 CommitData 类型的对象
在 onAttach 方法中获取这个对象,并调用其中的方法并传入数据

Fragment类
// 1.定义一个接口,在这个接口中声明一个用于传递数据的方法
    public interface CommitData{
        public void sedMSG(String msg);
    }
    // 3.在自定义fragment中,声明一个回调接口的引用
    private CommitData commitData;

    @Override
    // 4.在onAttach()方法中,为第三步的引用赋值(可以把activity的对象赋值给它)
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 获取到的 Activity 对象强制转换为 CommitData 接口类型的对象,
        // 因为 Activity 实现了 CommitData 接口的,所以可以将其转换为 CommitData 类型的对象
        commitData = (CommitData) getActivity(); // 注意强转
        commitData.sedMSG("传递的");
    }
接收数据的activity类

实现接口重写回调方法

public class FragmentActivity extends AppCompatActivity implements Fragment2.CommitData{
    @Override
    // 2.让 接收数据的activity实现该接口,然后重写回调方法,目的:获取传入的值并做处理
    public void sedMSG(String msg) {
        TextView textView = findViewById(R.id.showData);
        textView.setText("传回的数据:"+msg);
    }
使用Handle 

Fragment

package com.example.androidstudiostudy;


// 新建的一个 Fragment 类
public class Fragment1 extends Fragment {

    private Handler oneHandle;

    public void setOneHandle(Handler handler) {
        this.oneHandle = handler;
    }

    @Override
    // onCreateView -> Fragment 创建视图时调用的方法
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d("FragmentLife", "onCreateView-----Fragment创建好了");
        
        return inflater.inflate(R.layout.fragment_1, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button button5 = getView().findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (oneHandle != null) {
                    oneHandle.sendEmptyMessage(1111);
                }
            }
        });
    }

   
 

}

Activity

newFragment.setOneHandle(this.oneHandle);

package com.example.androidstudiostudy;


// 创建一个新的activity 绑定布局 R.layout.activity_fragment ,在该布局里添加 fragment 控件,进行展示
public class FragmentActivity extends AppCompatActivity implements Fragment2.CommitData{
    Handler oneHandle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        oneHandle = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                System.out.println("Activity Fragment使用Handle传递数据");
                TextView textView = findViewById(R.id.a_f_handle);
                textView.setText("传递了:"+msg.what);
                return false;
            }
        });
    }

    // FragmentTransaction.replace() 方法替换 Fragment
    // 参数1:containerViewId 应放置片段的位置
    // 参数2:要替换的片段
    public void replaceFragment(View view) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment1 newFragment = new Fragment1();
        newFragment.setOneHandle(this.oneHandle);
        fragmentTransaction.replace(R.id.fragment2,newFragment);
        fragmentTransaction.commit();
    }

    
}
 使用ViewModel

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值