zoukankan      html  css  js  c++  java
  • fragment

    1.getActivity();//获取包含该fragment的活动(activity)上下文
    2.getContext();//获取该fragment上下文
    3.getActivity().getApplicationContext();//通过包含该fragment的活动(activity)获取整个应用的上下文
    4.getContext().getApplicationContext();//通过该fragment获取整个应用的上下文

    onCreateView():加入fragment的layout

    onCreateView是创建的时候调用,onViewCreated是在onCreateView后被触发的事件

    FragmentManager

    调用getSupportFragmentManager() 获取FragmentManager

    popBackStack() :使片段从返回栈弹出

    addOnBackStackChangedListener():注册监听返回栈变化的监听器

    接口:
    FragmentManager.BackStackEntry

    片段返回栈中片段的数量

    类:
    FragmentManager.OnBackStackChangedListener

    监听返回栈变化的监听器

    POP_BACK_STACK_INCLUSIVE:

    若已设置,则将提供返回栈条目,直到抵达栈底

    fragmentManager中的FragmentController:定义了生命周期和对fragment的管理操作

    FragmentHostCallback: 负责各种回调

    #将一个fragment实例添加到Activity里面指定id的容器中
    add(Fragment fragment, String tag)
    add(int containerViewId, Fragment fragment)
    add(int containerViewId, Fragment fragment, String tag);
     #将一个fragment实例从FragmentManager的FragmentList中移除
    remove(Fragment fragment);
    #只控制Fragment的隐藏
    hide(Fragment fragment)
    #只控制Fragment的显示
    show(Fragment fragment)
    #清除视图,从containerid指定的Added列表移除,FragmentList依然保留
    detach(Fragment fragment)
    #创建视图,添加到containerid指定的Added列表,FragmentList依然保留
    attach(Fragment fragment)
    #替换containerViewId中的fragment,它会把containerViewId中所有fragment删除,然后添加当前的fragment
    replace(int containerViewId, Fragment fragment)
    replace(int containerViewId, Fragment fragment, String tag)

    FragmentTransaction的用例

    addFragment(Fragment fragment,String tag)添加Fragment到FragmentList中

    replaceFragment(Fragment fragment,String tag) 清空所有fragment,替换成新的

    removeFragment(Fragment fragment) :移除指定的fragment
    showFragment(Fragment fragment):把Fragment设置为显示(未添加到FragmentList中)
    hideFragment(Fragment fragment):把Fragment设置为隐藏状态
    attachFragment(Fragment Fragment):效果与show相近,添加到containerid指定的Added列表,FragmentList仍然保留,但会引起生命周期变化
    detachFragment(Fragment Fragment):效果与hide相近,从containerid指定的Added列表删除,FragmentList仍然保留,但会引起生命周期变化

    在onPageScrollStateChanged()方法中传入状态state
    1. state =1     开始滑动
    2. state = 2    滑动结束
    3. state = 0    停止

    在onPageScrolled()方法中传入

    1. 当前fragment的position
    2. 位置偏移量positionOffset
    3. 像素偏移量positionOffsetPixels

    getArguments()

    使用Fragment自带的getArguments()传递参数

    若使用构造器来传递参数,则再切换横竖屏的时候会出问题

    若使用get/setArguments,则数据不会显示异常

    LayoutInflater 中inflate方法两个参数和三个参数的区别:

    三个参数:public View inflate(@LayoutRes int resource, @Nullable ViewGroup root , boolean attachToRoot)

    1. root不为null, attachToRoot为true   : 第一个参数所指定的布局添加到 第二个参数的View中
    2. root不为null, attachToRoot为false  : 因为最后一个参数为false,第一个参数所指定的布局添加到第二个参数的View中,因此我们可以用   父布局.addView(view)的方法实现 子布局添加进入父布局
    3. root为 null ,当root为null时,无论attachToRoot是true或false,显示效果一样,固定为传入的布局的样式

    两个参数的inflate方法: 

    1. ROOT为 null,   固定为传入的布局的样式,等同于三个参数的 root为空
    2. root 不为 null , 将第一个参数所指定的布局添加到第二个参数的View中
  • 相关阅读:
    Luogu P3919【模板】可持久化数组(可持久化线段树/平衡树)
    线段树||BZOJ5194: [Usaco2018 Feb]Snow Boots||Luogu P4269 [USACO18FEB]Snow Boots G
    线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel
    CF 610E. Alphabet Permutations
    BZOJ 1227: [SDOI2009]虔诚的墓主人
    BZOJ1009: [HNOI2008]GT考试
    BZOJ3674: 可持久化并查集加强版
    BZOJ3261: 最大异或和
    BZOJ2741: 【FOTILE模拟赛】L
    BZOJ3166: [Heoi2013]Alo
  • 原文地址:https://www.cnblogs.com/acg88688/p/11870566.html
Copyright © 2011-2022 走看看