您的位置:首页 > 移动开发 > Android开发

Fragment 深究

2015-09-20 15:57 477 查看
1. Fragment与activity之间的关系:  红砖和围墙的关系 。一个围墙可以由多个红砖组成。一个红砖同时适合多个围墙。多对多的关系。

  a. 围墙没有了 销毁了(ondestroy)  红砖也没有了。

  b. 当围墙在暂定的时候(onpuse)  红砖也暂定修改了。

  c. 当围墙正在维修中(onresume)时候  红砖可以添加和移除。 

2. Creating a Fragment (创建Fragment)

To create a fragment, you must create a subclass of Fragment (or an existing subclass of it)创建一个Fragment,必须继承Fragment这个类,或者继承这个类的子类。Fragment与Activity相似。它有和activity相似的方法。onCreate(), onStart(), onPause(), onStop()。

3. Adding a fragment to an activity (如何向activity里添加Fragment)

  a. xml 布局文件中 添加 Fragment。

  b. 代码中添加 Fragment 

     1>获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。

     2>开启一个事务,通过调用beginTransaction方法开启。

     3>向容器加入Fragment,add或者replace或者remove方法实现,需要传人容器的id和Fragment的       实例。

     4>提交事务,调用commit方法调用。

3. the Fragment lifecycle (Fragment生命周期)

   1> onAttach方法:Fragment与Activity建立关联的时候调用。

   2> onCreateView方法:为Fragment加载布局时调用。返回View试图,如何不期望可以返回null。

   3> onActivityCreated方法:当Activity中的onCreate方法执行完后调用这个方法。

   4> onDestoryView方法:Fragment中的布局被移除的时候调用。

   5> onDetach方法 :Fragment与Activity解除关联的时候调用。

4. Communicating with the Activity(Fragment之间 如何 进行通信)

   1> 主要是通过getActivity这个方法实现的。getActivity方法可以让Fragment获取到关联的Activity,然后再调用Activity的findViewByid方法,就可以获取到和这个Activity关联的其它Fragment的试图。

    View listView = getActivity().findViewById();

   2> 可以通过FragmentManager这个方式找到Fragment 然后进行控制

    ExampleFragment fragment = (ExampleFragment)getFragmentManager().findFragmentById(R.id.example_fragment);

     findFragmentById()  :找到提供UI的Fragment.

     findFragmentByTag() :发现没有提供UI的Fragment.

5. Fragments 的类别

系统内置了三种Fragments,这三种Fragments分别有不同的应用场景分别为:

    1>. DialogFragment 对话框式的Fragments,可以将一个fragments对话框并到activity管理的fragments back stack 中,允许用户返回到前一个曾摒弃的fragments。

    2>. ListFragments 和ListAcitivity的效果相似,提供了onListItemClick()和setListAdapter等功能来管理list view。

    3>. PreferenceFragments 类似于PreferenceActivity.可以创建类似Ipad的设置界面。This is useful when creating a "settings" activity for your application.当给应用做setting的时候,可以使用。

6. Adding a user interface (添加用户界面)

  如果fragment在activity中作为UI进行显示,oncreatview 这个方法,返回return view。ListFragment除外。默认返回listview

7. Adding a fragment without a UI (添加Fragment没有UI)

   add(Fragment, String) (supplying a unique string "tag" for the fragment, rather than a view ID).使用这个方法添加一个没有UI的Fragment 不用实现onCreateView()这个方法。后期需要找到你添加的Fragment通过findFragmentByTag()方法找到Fragment。

8. Managing Fragments (管理 Fragment)

  1> FragmentManager 通过调用 getFragmentManager()方法找到。

  2> findFragmentById()  :找到提供UI的Fragment.

     findFragmentByTag() :发现没有提供UI的Fragment.

  3> FragmentTransaction 事务 负责add remove Fragment。

9. Performing Fragment Transactions (事务的使用)

  FragmentManager fragmentManager = getFragmentManager();

    FragmentTransaction  fragmentTransaction = fragmentManager.beginTransaction();

    

   Fragment  newFragment = new ExapleFragement();

   fragmentTransaction.replace(R.id.fragment_container, newFragment);

   fragmentTransaction.commit();

   

 Tip1: For each fragment transaction, you can apply a transition animation, by calling setTransition() before you commit.(在你commit提交之前 调用setTransition()这个方法,Fragment切换动画)

 Tip2: By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button.(如果在commit之前调用addToBackStack()这个方法,后退的时候可以找到原有的fragment,如何没有调用这个方法
原有的fragment就销毁了 不能返回过来了)。

1O. Creating event callbacks to the activity (事件回调)

     1> interface inside the fragment (在fragment1声明一个接口)。

     2> host activity implement it (在Activity里面实现一个这个接口);

     3> 在fragment1接口里面 写一个方法。 并且在onattch里面实现这个方法。(原因,onattch方法的实现表面了 fargment与Activity绑定成功)。

     4> 在activity里面重写 fragment1接口里面的方法。在方法里面进行fargment2事务管理 使用Fragment.setArguments(Bundle)进行传值。

     5> 在fragment2中使用 Fragment.getArguments()来读出Bundle。进行标示。

   

11. Fragment 之间的 传值 。

  

这两个方法Fragment.setArguments(Bundle)和Fragment.getArguments(),你必须通过这两个方法确保参数别持久化保存。第一、你要创建一个Bundle,然后你需要放入键值对,最后调用Fragment.setArguments()把数据存起来。第二、你必须使用Fragment.getArguments()来读出Bundle。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息