Fragment的两种创建方式

一.静态加载的方式

  • 自定义一个类继承Fragment
  • 实现onCreateView()方法 当系统第一次绘制fragment的用户界面时回调的方法
  • .onCreateView()方法中加载XML文件,返回当前Fragment布局转化的View对象
  • 把Fragment添加到Activity中

1.自定义一个类FragmentLeft继承系统的Fragment

public class FragmentLeft extends Fragment{
    public View onCreateView(LayoutInflater inflater ,ViewGroup container , Bundle savedInstanceState){
        View view = inflater.inflate(R.laryout.activity_left);
        return view;
    }
}

FragmentLeft的布局



    
    


2.再自定义一个FragmentRight继承系统Fragment

public class FragmentRight extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /**
         * 1.要加载的布局的资源id
         * 2.当前布局的父布局
         * 3.是否需要追加到父布局
         */
        View view = inflater.inflate(R.layout.activity_fragmentright, container, false);
        return view;
    }
}

FragmentRight的布局



    
    


3.MainActivity

public class MainActivity extends Activity {

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

MainActivity布局
在main_activity中注册fragment标签
==注意:它不是一个控件,它是一个引用型的标签==



    
    
    

二.动态加载Fragment

1.自定义类FragmentLeft继承Fragment

public class FragmentLeft extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_left, container,false);
        return view;
    }
}

FragmentLeft布局



    
    

2.自定义FragmentRight继承Fragment

public class FragmentRight extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_right, container,false);
        return view;
    }
}

FragmentRight布局



    
    

3.MainActivity

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         //获取Fragment的管理者对象 FragmentManager:用来在Framgent和Activity之间交互的接口
        FragmentManager fragmentManager = getFragmentManager();
        //将add remove replace等等操作称为事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
         //参数1表示fragment添加到Activity的区域的id 2.表示需要具体添加的Fragment对象
        transaction.add(R.id.layout_container_leftFragment, new FragmentLeft());
        transaction.add(R.id.layout_container_rightFragment, new FragmentRight());
        
        /**
         *transaction.remove(fragment);
         *transaction.replace(containerViewId, fragment);
         *transaction.hide(fragment)
         *transaction.show(fragment);
         */
        //提交事务--->保存
        transaction.commit();
    }

在activity_main布局里面添加两个LinearLayout用于放Fragment


            
    
    
         
    

你可能感兴趣的:(Fragment的两种创建方式)