野生大熊猫

Android Fragment——FragmentManager分析及用例
获取Fragment的三种方式fragmentManager = getSupportFragmentManage...
扫描右侧二维码阅读全文
09
2018/07

Android Fragment——FragmentManager分析及用例

获取Fragment的三种方式

fragmentManager = getSupportFragmentManager();
// 通过id查找对应的Fragment实例
fragment1 = (StudyFragment) fragmentManager.findFragmentById(R.id.fragment_study1);
// 通过tag找到对应的Fragment,在动态加载和静态加载中都可以使用
 secondFragment = (SecondFragment) fragmentManager.findFragmentByTag("fragment_second");
// 获取添加到FragmentManager的所有Fragment,通过index访问,0代表最早添加的Fragment
fragmentManager.getFragments()

FragmentTransaction的一些常用方法

  FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务

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

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

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

  transaction.hide() //当你的fragment数量固定很少时隐藏当前的Fragment,仅仅是设为不可见,并不会销毁,多的时候可能出现OOM异常,

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

  detach()会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。

  attach()重建view视图,附加到UI上并显示。

  transatcion.commit()//提交一个事务

FragmentTransaction的用例

//添加Fragment到FragmentList中
private void addFragment(Fragment fragment, String tag){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fragment_container,fragment,tag);
        transaction.commit();
    }


// 清空fragmentList的所有Fragment,替换成新的Fragment,注意Fragment里面的坑
private void replaceFragment(Fragment fragment, String tag){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment,tag);
        transaction.commit();
    }


//移除指定的Fragment
private void removeFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.remove(fragment);
        transaction.commit();
    }


//把Fragment设置成显示状态,但是并没有添加到FragmentList中
private void showFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.show(fragment);
        transaction.commit();
    }


//把Fragment设置成显示状态,但是并没有添加到FragmentList中
private void hideFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(fragment);
        transaction.commit();
    }


// 效果和show相近,创建视图,添加到containerid指定的Added列表,FragmentList依然保留,但是会引起生命周期的变化
private void attachFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.attach(fragment);
        transaction.commit();
    }


// 效果和hide相近,清除视图,从containerid指定的Added列表移除,FragmentList依然保留,但是会引起生命周期的变化
private void detachFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.detach(fragment);
        transaction.commit();
    }

原文:https://blog.csdn.net/u012241729/article/details/60969606

Last modification:July 16th, 2018 at 01:48 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment