Fragment碎片

目录

一、创建Fragment

布局文件

动态使用

二、Fragment的生命周期

四种状态:

三、活动和碎片的生命周期方法回调顺序:


Fragment
碎片和活动非常相似,它有自己的生命周期,在创建时也需要创建布局和类。引入碎片是为了更好的适配大屏幕设备。一个活动可以引入多个碎片,一个碎片也可以引入到多个活动中,从而实现代码复用。


一、创建Fragment

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


布局文件

<?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:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="button1"/>
</LinearLayout>
使用Fragment
静态使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </fragment>Left
    <fragment
        android:id="@+id/right_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>打开页面 TRANSACTION
</LinearLayout>


动态使用


Fragment还支持在程序运行时,动态的添加到活动中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </fragment>
    <FrameLayout
        android:id="@+id/right_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    @Override
    @SuppressLint("MissingInflatedId")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1 = findViewById(R.id.button1);
        button1.setOnClickListener(view->{
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.right_fragment, new RightFragment());
            transaction.commit();
        });
    }
}


二、Fragment的生命周期

四种状态:


和活动一样Fragment在其生命周期也有四种状态:
1. 运行状态,当Fragment所绑定的活动处于运行状态,并且这个Fragment可见时,那么它就处于运行状态。
2. 暂停状态,当Fragment所绑定的活动处于暂停状态,可见的Fragment就会转入暂停状态。
3. 停止状态,当活动转入停止状态时,它所绑定的碎片都会转入停止状态。此外在FragmentTransaction提交前,如果调用了addToBackStack(),那么通过remove()或replace()移除的Fragment都会进入停止状态。
4. 销毁状态,当活动进入销毁状态,它所绑定的碎片都会进入销毁状态。此外此外在FragmentTransaction提交前,如果没有调用了addToBackStack(),那么通过remove()或replace()移除的Fragment都会进入销毁状态。


三、活动和碎片的生命周期方法回调顺序:


打开页面
按下HOME键
重新打开页面
按下返回键
常用API
在动态添加、替换或移除Fragment时,使用到了FragmentManager和FragmentTransaction
FRAGMENTMANAGER
FragmentManager负责管理活动所绑定的碎片,调用getSupportFragmentManager()可获取到FragmentManager
常用API

//获取活动绑定的Fragment 
getFragments()
//通过TAG获取指定的Fragment;这个TAG,在FragmentTransaction的add(int containerViewId, Fragment fragment,String tag)进行设置。
findFragmentByTag(String tag)
//弹出栈顶Fragment
popBackStack()
//tag可以为null或相应的tag,flags只有0或1
popBackStack(String tag,int flags)
/*


1. 如果tag为null,flags为0时,弹出回退栈中最上层的那个fragment。
2. 如果tag为null ,flags为1时,弹出回退栈中所有fragment。
3. 如果tag不为null,那就会找到这个tag所对应的fragment,flags为0时,弹出该fragment以上的Fragment,如果是1,弹出该fragment(包括该fragment)以上的fragment。
 

*/
//开启事务,返回FragmentTransaction
beginTransaction()
FRAGMENTTRANSACTION
通过FragmentTransaction实现在Activity运行时可动态地加入、移除、交换Fragment
//将remove或replace移除的fragment添加到返回栈。
addToBackStack() 
//增加framgent到队列中,并显示该fragment到指定布局中。
add(id, fragment)
//销毁队列中指定的fragment。
remove(fragment)
//替换fragment
replace(id, fragment)
//隐藏fragment,不会改变fragment的状态
hide()
//显示fragment
show()
//transaction 的最后一步必须是 commit 操作,commit() 方法告诉 FragmentManager 完成了 transaction 的所有操作,要注意的是,commit() 方法调用后,不会立即生效,而是要取决于主线程的任务安排。
commit()
//立即生效
commitNow()
文件 已添加 6274589-223081c9f7158599.webp 查看删除

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值