Android Fragment 小结

参考:http://www.cnblogs.com/Gaojiecai/p/4084252.html

一.Fragment  生命周期

fragment有自己的一套生命周期 ,但同时也依托于Activity的生命活动,与其有着密切的关联。                                  

  onAttach()-->onCreat()-->onCreatView()-->onActivityCreated()-------与Activity的Createed生命周期对应

        onStart()------------------------与Activity的Started生命周期对应

  onResume()----------与Activity的Resume的生命周期对应

onPause()------与Activity的Paused的生命舟曲对应

  onStop() ---------与Activity的Stoped所对应

onDestoryView()-->onDestory()-->onDetach()------与Activity的Destroyed对应

所多出来的几个周期如下

onAttach(Activity);  //called once the fragment is associated with its activity.当Activity与Fragment发生关联时调用

onCreate(Bundle); //called to do initial creation of the fragment. 当初始创建Fragment时调用

onCreateView(LayoutInflater,ViewGroup,Bundle);  //creates and returns the view hierarchy associated with the fragment.创建该关联的Fragment的视图

onActivityCreate(bundle);  //tells the fragment that its activity has completed its own Activity.onCreate().当Activity的onCreate();方法完成时调用



  • onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.告知fragment当前视图的所有状态都被储存了

      onStart()   makes the fragment visible to the user (based on its containing activity being started).

      onDestoryView();  //与onCreateView相对应,当改Fragment被移除时调用

      onDetach();  //与onAttach()相对应,当Fragment与Activity的关联被取消时调用

      注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现。


      二.Fragment的使用

      Fragment的使用方法分为两种,静态使用和动态调用。

      1.静态使用,就当时使用一般的控件使用,但仅能作为显示,可操作性会比较差,如切换页面等功能会受限制。

      1)类继承Fragment,重写onCreateView决定Fragment的布局

      2)在Activity中声明所写Fragment,像使用普通的View一样。


      public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.demofragment, container, false);
      return view;
      }


      在mainactivity.xml中将此作为普通控件添加


      	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          		xmlns:tools="http://schemas.android.com/tools"
          		android:layout_width="match_parent"
         	 	android:layout_height="match_parent" >
      
         	   <fragment
              	  android:id="@+id/id_fragment_demo"
              	  android:name="com.example.dynamicfragment.DemoFragment"
              	  android:layout_width="fill_parent"
              	  android:layout_height="45dp" />

          2 动态使用,需要在Mainactivity.java做一些操作,实现对Fragment的 增删改。

      1)实现静态引用的所有步骤,在MainActivity.java 调用setContentView()后 将默认的Fragment动态加载过来,随后对下方tab做设置进行使用。

                    获取FragmentManager    和FragmentTransaction

      	FragmentManager manager = getFragmentManager();
              FragmentTransaction transaction = manager.beginTransaction();
      
      
      	contentFragment = new ContentFragment();
              transaction.replace(R.id.id_content, contentFragment);
              transaction.commit();

      3 fragment实现onClickListener 来动态改变

    1. 将fragment的点击操作交给activity来操作,其中分别为点击切换到下一个Fragment



    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值