Fragment

1、Fragment的简单使用

        <1>Fragment:在Activity中使用的碎片,有自己的布局、生命周期和输入事件
        <2>使用Fragment的步骤
                (1)创建类,并继承Fragment;
  1. public class FirstFragment extends Fragment{
  2. }
                (2)重写Fragment的onCreateView()生命周期方法,并返回一个View;
  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  3. Bundle savedInstanceState) {
  4. // TODO 加载fragment视图(两种方法,推荐使用第二种)
  5. //View view = inflater.inflate(R.layout.first_fragment, null);
  6. View view2 = inflater.inflate(R.layout.first_fragment, container, false);
  7. TextView textView = (TextView) view2.findViewById(R.id.textView);
  8. //返回加载的视图对象
  9. return view2;
                (3)使用fragment的两种方法
                        a.在布局文件中使用<fragment android:name="自定义Fragment的类路径"/>保证碎片显示唯一:id/tag
  1. <fragment
  2. android:id="@+id/firstFragment"
  3. android:layout_width="match_parent"
  4. android:layout_height="150dp"
  5. android:layout_below="@id/tv"
  6. android:name="com.qf.day12demo01.FirstFragment"
  7. />
                        b.动态添加
                                1.获取到一个fragment管理器对象
  1. FragmentManager manager = getFragmentManager();
                                2.通过这个管理器对象要开启一个事务
  1. FragmentTransaction transaction = manager.beginTransaction();
                                3.把预留在布局中的位置,换成要展示的碎片对象
  1. transaction.replace(R.id.replaceId, new SecondFragment());
                                4.提交事务      
  1. transaction.commit();
          <3>使用FragmentManager
                (1)作用:管理多个Fragment之间的交互和传值
                (2)Activity.getFragmentManger()         3.0以后
                (3)FragmentActivity.getSupportFragmentManager() 3.0以前,引用v4包
                (4)FragmentTransaction beginTransaction() 获取Fragment事务处理对象
        <4>s使用FragmentTransaction
                (1)replace(int containerViewId, Fragment fragment)    把预留在布局中的位置,换成要展示的碎片对象
                (2)commit()     提交本次事务处理
3、Fragment的生命周期
        <1>11个生命周期方法
                (1)onAttach(Activity)  连接宿主Activity
                (2)onCreate(Bundle)  创建Fragment
                (3)onCreateView(LayoutInflater, ViewGroup, Bundle)创建Fragment视图
                (4)onActivityCreated(Bundle) 当宿主Activity的onCreate()执行完之后调用
                (5)onStart() 
                (6)onResume() 
                (7)onPause()
                (8)onStop() 
                (9)onDestroyView() 销毁Fragment视图,与onCreateView对应
                (10)onDestroy()  销毁Fragment,与onCreate对应
                (11)onDetach() 与宿主Activity断开连接,与onAttach对应
        <2>生命周期流程
                (1)当Activity创建时,调用Fragment的onAttach->onCreate->onCreateView->onActivityCreated
                (2)当Activity启动时,调用Fragment的onStart
                (3)当Activity获取焦点时,调用Fragment的onResume
                (4)当跳转到另一个Activity时,调用Fragment的onPause-->onStop
                (5)当返回Activity时,调用Fragment的onStart->onResume
                (6)销毁Activity时,调用Fragment的onDestroyView->onDestory->onDettach
3、Fragment与Activity之间的传值
        <1>Activity-->Fragment
                (1)在activity中添加碎片的时候,通过碎片对象的.setArgments(bundle)
  1. public void btnSendMsg(View view){
  2. TextView tv_top = (TextView) findViewById(R.id.tv_top);
  3. tv_top.setText(content+new Date());
  4. FragmentTransaction transaction = manager.beginTransaction();
  5. bottomFragment = new BottomFragment();
  6. Bundle bundle = new Bundle();
  7. bundle.putString("msg", content+new Date());
  8. //把碎片对象和要传递的数据绑定
  9. bottomFragment.setArguments(bundle);
  10. transaction.replace(R.id.replaceId, bottomFragment);//替换的碎片对象中是绑定有数据的
  11. transaction.commit();
  12. }
                (2)在fragment里面,通过getArgments();得到一个bundle对象,再从bundle对象里面获取内容
  1. Bundle bundle = getArguments();
  2. if (bundle != null) {
  3. String msg = bundle.getString("msg");
  4. tv_bottom.setText(msg);
  5. }
        <2>Fragment-->Activity
                (1)在activity中声明一个公共的方法,在这个方法中必须要有一个参数(参数类型就是要传递的数据类型)
  1. public void setContent(String s){
  2. tv_main.setText(s);
  3. }
                (2)在fragment里面,通过getActivity(),可以获取到宿主activity对象,再调用宿主对象中到提供的公共方法,把数据传递在这个方法中
  1. @Override
  2. public void onClick(View v) {
  3. // TODO 向宿主activity传值
  4. //获取到当前碎片所在的宿主activity对象
  5. MainActivity activity = (MainActivity) getActivity();
  6. activity.setContent(content+new Date());
  7. }
                (3)Activity方式
                        1.在Activity中获取Fragment中的UI控件,并增加相关事件
                        2.在Activity声明公共方法,在Fragment中调用getActivity()并强转,则可以调用公共方法向其他Fragment控件传值
                        3.获取assests下的文件流      InputStream is = getResources().getAssets().open("day01.txt");
原文地址:https://www.cnblogs.com/android-blogs/p/5710242.html