1、fragment加载到Activity
(1)、添加fragment到Activity的布局文件
(2)、动态在activity中添加fragment
例子:
// 步骤1:获取FragmentManager FragmentManager fragmentManager = getFragmentManager(); // 步骤2:获取FragmentTransaction FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // 步骤3:创建需要添加的Fragment final mFragment fragment = new mFragment(); // 步骤4:动态添加fragment // 即将创建的fragment添加到Activity布局文件中定义的占位符中(FrameLayout) // FragmentTransaction add = fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.add(R.id.fragment_container,fragment,"ssss"); fragmentTransaction.addToBackStack("ssss") fragmentTransaction.commit();
2、FragmentPagerAdapter和FragmentStatePagerAdapter的区别
FragmentPagerAdapter适用于页面较少的情况,而FragmentStatePagerAdapter适用于页面较多的情况
因为在源码中FragmentStatePagerAdapter中的destroyItem中用了remove方法回收内存
而FragmentPagerAdapter中并不回收内存
3、fragment的生命周期
onArrach()当碎片和活动建立关联的时候调用
onCreateView()为碎片创建视图(加载布局)时调用
onActivityCreated()确保与碎片相关联的活动一定已经创建完毕的时候调用
onDestroyView()当与碎片关联的视图被移除的时候调用
onDetach()当碎片和活动解除关联的时候调用
fragment的通信
1、在fragment中调用Activity中的方法getActivity
2、在Activity中调用Fragment中的方法接口回调
3、在Fragment中的调用Fragment中的findFragmentById或findFragmentByTag中的方法
FragmentManager的replace、add、remove
replace:把Activity的fragment替换
add:加到最上层
remove:删除
例子:
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFragment aFragment = mFragment.newInstance("woshilskdj"); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().add(R.id.fragment_container,aFragment,"afragment").commit(); //在activity中加载Fragment } }
mFragment.java
public class mFragment extends Fragment { private TextView title; private Button enterB; private Button changeText; private bFragment bfragment; public static mFragment newInstance(String title){ mFragment fragment = new mFragment(); Bundle bundle = new Bundle(); bundle.putString("title",title); fragment.setArguments(bundle); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View contentView = inflater.inflate(R.layout.fragment, container, false); // 设置布局文件 return contentView; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { title = view.findViewById(R.id.fragment); enterB = view.findViewById(R.id.fragmentB); changeText = view.findViewById(R.id.changeText); enterB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(bfragment==null){ bfragment = new bFragment(); } Fragment fragment = getFragmentManager().findFragmentByTag("afragment"); //判断fragmentManager有没有已经标识好的“afragment”的实例 if(fragment!=null){
//把fragment隐藏起来 再加载bfragment 这样返回的时候 不会再创建新的fragment getFragmentManager().beginTransaction().hide(fragment).add(R.id.fragment_container,bfragment).addToBackStack(null).commit(); }else{ getFragmentManager().beginTransaction().replace(R.id.fragment_container,bfragment).addToBackStack(null).commit(); } } }); changeText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { title.setText("我是苏"); } }); Bundle bundle = getArguments(); if(bundle!=null){ title.setText(bundle.getString("title")); } }