In-depth analysis of Android dynamic management fragment: "Do you know these components?"

In-depth analysis of Android dynamic management fragment: "Do you know these components?"

1. The relationship between Fragment and Activity

To understand dynamic management of Fragment, you must first understand the relationship between Fragment and Activity

Fragment can be understood as dividing Activity into several fragments. Fragment is part of Activity, and its life cycle depends on Activity. It cannot exist independently. It is not difficult to understand that all Fragment management (finding, adding, deleting, replacing) should be done in the Activity that Fragment depends on, that is, Activity is the place for Fragment interaction. Don't try to manage another Fragment in a Fragment. Although it can achieve the required functions, it does not meet the specifications. To complete specific operations on Fragment, you need to use FragmentManager component.

2. Use FragmentManager component

The key code for using the FragmentManager component is as follows:

  1. //1. Create a FragmentManager component  
  2. FragmentManager fm= super .getSupportFragmentManager();
  3. //2. Query method to get a Fragment in Activity  
  4. fm.findFragmentById(R.id.fooler)
  5. fm.findFragmentByTag("tagName")
  6. //3. Dynamically add Fragment  
  7. fm.beginTransaction().add(R.id.content,contentFragment, "content" ).commit();
  8. //4. Replace Fragment  
  9. fm.beginTransaction().replace(R.id.OldFragment, newFragment).commit();
  10. //5. Delete Fragment  
  11. fm.beginTransaction().remove(R.id.myFragment).commit();
  12. //6. Pass data to Fragment  
  13. f.setArguments(Bundle) method passes data

     

The above management Fragment code should be organized in the Activity to which the Fragment belongs. Let's take a look at the specific implementation through a case.

3. Implementation Case

First, let's take a look at the application case (as shown in the figure). When you click the first area and the second area at the bottom, the center area is switched.

Interface analysis: There are three areas: the header area TitleFragment, the center area is a FrameLayout layout that dynamically loads ContentFragment through code, and the bottom area FloorFragment

1. The layout of Activity is as follows

  1. RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"  
  2. android:layout_width= "match_parent"  
  3. android:layout_height= "match_parent" >
  4. <fragment android:id= "@+id/fgTitle"  
  5. android:layout_width= "match_parent"  
  6. android:layout_height= "45dp"  
  7. android:name= "com.jereh.android.course.fragment.TitleFragment"  
  8. />
  9. <FrameLayout android:id= "@+id/content"  
  10. android:layout_width= "match_parent"  
  11. android:layout_height= "match_parent"  
  12. android:layout_below= "@id/fgTitle"  
  13. />
  14. <fragment android:id= "@+id/floor"  
  15. android:layout_width= "match_parent"  
  16. android:layout_height= "wrap_content"  
  17. android:layout_alignParentBottom= "true"  
  18. android:orientation= "horizontal"  
  19. android:name= "com.jereh.android.course.fragment.FloorFragment"  
  20. />
  21. </RelativeLayout>
  22.  
  23. Copy code
  24. 2. FloorFragment code
  25. Copy code
  26.  
  27. public   class FloorFragment extends Fragment implements OnClickListener {
  28. @Override  
  29. public   void onAttach(Activity activity) {
  30. super .onAttach(activity);
  31.  
  32. }
  33. @Override  
  34. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  35. Bundle savedInstanceState) {
  36. // TODO Auto-generated method stub  
  37. View view=inflater.inflate(R.layout.floor_fragment, container, false );
  38. LinearLayout home=(LinearLayout)view.findViewById(R.id.home);
  39. LinearLayout list=(LinearLayout)view.findViewById(R.id.list);
  40. home.setOnClickListener( this );
  41. list.setOnClickListener( this );
  42. return view;
  43. }
  44. //Define an interface that is implemented in the Activity to reduce the coupling between the Activity and the Fragment  
  45. public   interface OnContentFragmentListener{
  46. void setContentFragment(String param);
  47. }
  48. private OnContentFragmentListener onContentFragmentListener;
  49. public   void setOnContentFragmentListener(
  50. OnContentFragmentListener onContentFragmentListener) {
  51. this .onContentFragmentListener = onContentFragmentListener;
  52. }
  53. @Override  
  54. public   void onClick(View view) {
  55. // TODO Auto-generated method stub  
  56. if (onContentFragmentListener!= null ){
  57. switch (view.getId()){
  58. case R.id.home:
  59. //Callback mechanism, calling the method of the implementation class  
  60. onContentFragmentListener.setContentFragment( "Main Panel Fragment" );
  61. break ;
  62. case R.id.list:
  63. onContentFragmentListener.setContentFragment( "list information" );
  64. break ;
  65. }
  66. }
  67. }
  68. }
3. ContentFragment code
  1. public   class ContentFragment extends Fragment {
  2. private String title;
  3. public   void setArguments(Bundle args) {
  4. title=args.getString( "title" );
  5. }
  6. @Override  
  7. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  8. Bundle savedInstanceState) {
  9. View view=inflater.inflate(R.layout.content_fragment, container, false );
  10. if (title!= null )((TextView)view.findViewById(R.id.tvContent)).setText(title);
  11. return view;
  12. }
  13. }

     

4. DynaFragmentActivity code

  1. public   class DynaFragmentActivity extends FragmentActivity implements FloorFragment.OnContentFragmentListener {
  2. private ContentFragment contentFragment;
  3. private FloorFragment floor;
  4. @Override  
  5. protected   void onCreate(Bundle savedInstanceState) {
  6. super .onCreate(savedInstanceState);
  7. super .setContentView(R.layout.dyna_fragment_activity);
  8. initFragment();
  9. }
  10. private   void initFragment(){
  11. floor=(FloorFragment) super .getSupportFragmentManager().findFragmentById(R.id.floor);
  12. floor.setOnContentFragmentListener( this ); //Register to listen, pass in the implementation class object  
  13. contentFragment= new ContentFragment();
  14. FragmentManager fm= super .getSupportFragmentManager();
  15. fm.beginTransaction().add(R.id.content,contentFragment, "content" ).commit();
  16. }
  17.  
  18. public   void setFragment(Fragment fragment){
  19. getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
  20. }
  21.  
  22. @Override  
  23. public   void setContentFragment(String param) {
  24. // TODO Auto-generated method stub  
  25. Bundle bundle = new Bundle();
  26. bundle.putString( "title" , param);
  27. contentFragment= new ContentFragment();
  28. contentFragment.setArguments(bundle);
  29. super .getSupportFragmentManager().beginTransaction()
  30. .replace(R.id.content, contentFragment).commit();
  31. }
  32.  
  33. }

<<:  The future of mobile is not in apps

>>:  Hundreds of iOS apps exposed to FREAK vulnerability risk

Recommend

Xu Guanhua: Zhou Guangzhao's scientific light will always shine

This article was published in the Bulletin of the...

Bilibili Operations: How to grow from 0 to millions of followers?

With the explosive growth of the video industry, ...

Do you know how important this inconspicuous little moss is?

Disclaimer: In accordance with general ecological...

Musk: A new generation of flying Roadster sports car will be launched in 2020

Tesla CEO Elon Musk has made many surprising stat...

How does a spacecraft flying in space find its way home?

After a spacecraft enters space, there is one uns...

Cutting-edge practical experts: SEO trend prediction for 2016!

Search engine optimization has become one of the ...

ARKit & OpenGL ES - ARKit principle and implementation

Principle If you want to learn more about OpenGL ...

This silly big dog has saved more than 4,000 people

December 24 was a sleepless night for the North A...

Efficient strategy to recall lost APP users!

Before starting today's topic, let's talk...