[Android Development] Fragment development 1, androidfragment

Source: Internet
Author: User

[Android Development] Fragment development 1, androidfragment

 

I have always known that Fragment is very powerful, but I have never been able to learn it. Now I have some free time, so I just went to get started with Fragment. I will also write down my learning process. If there are any shortcomings, I hope you can correct them and make progress together!

 

1. Fragment Introduction

1. Fragment appears as part of the Activity interface;

2. Multiple Fragment can appear in an Activity at the same time, and one Fragment can also be used in multiple activities;

3. During Activity running, you can add, remove, or replace Fragment (add (), remove (), and replace ());

4. Fragment can respond to input events and have their own lifecycles. Of course, their lifecycles are directly affected by the lifecycles of the activities they belong.

 

Why should we use Fragment? The main purpose is to use large screen devices, such as tablets, to support more dynamic and flexible uidesign. The screen size of a tablet is much larger than that of a mobile phone. More space is available for more UI components, and more interaction is generated between these components. We can regard Fragment as a "small Activity", which is more concise.

 

 

Ii. simple use of Fragment

Then we will explain how to simply display two Fragment instances.

    

1. Add Fragment in XML:

Create Fragment1 and Fragment2. (Note: You may have two packages to import to android. app. fragment or android. support. v4.app. fragment is acceptable. I chose to use the former here, but there is a difference between the two. I will talk about it at the end ):

Fragment1 code: 1 package com. example. fragment; 2 3 import android. app. fragment; 4 import android. OS. bundle; 5 import android. util. log; 6 import android. view. layoutInflater; 7 import android. view. view; 8 import android. view. viewGroup; 9 10 import com. example. fragmentdemo. r; 11 12 public class Fragment1 extends Fragment {13 @ Override14 public View onCreateView (LayoutInflater inflater, ViewGroup container, 15 Bundle savedInstanceState) {16 Log. e ("TAG", "in"); 17 return inflater. inflate (R. layout. fragment1, container, false); 18} 19}View Code Fragment2 Code: 1 package com. example. fragment; 2 3 import android. app. fragment; 4 import android. OS. bundle; 5 import android. view. layoutInflater; 6 import android. view. view; 7 import android. view. viewGroup; 8 9 import com. example. fragmentdemo. r; 10 11 public class Fragment2 extends Fragment {12 @ Override13 public View onCreateView (LayoutInflater inflater, ViewGroup container, 14 Bundle savedInstanceState) {15 return inflater. inflate (R. layout. fragment2, container, false); 16} 17}Xml Code of View Code Fragment1: 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: background = "# FF69B4" 6 android: orientation = "vertical"> 7 8 <TextView 9 android: layout_width = "wrap_content" 10 android: layout_height = "wrap_content" 11 android: layout_gravity = "center" 12 android: text = "This is the first Fragment"/> 13 14 </LinearLayout>Xml Code of View Code Fragment2: 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: background = "# EECBAD" 6 android: orientation = "vertical"> 7 8 <TextView 9 android: layout_width = "wrap_content" 10 android: layout_height = "wrap_content" 11 android: layout_gravity = "center" 12 android: text = "this is the second Fragment"/> 13 14 </LinearLayout>View Code we add two Fragment in activity_main.xml, the Code is as follows: 1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 android: layout_width = "match_parent" 3 android: layout_height = "match_parent" 4 android: baselineAligned = "false"> 5 6 <fragment 7 android: id = "@ + id/fragment1" 8 android: name = "com. example. fragment. fragment1 "9 android: layout_width =" wrap_content "10 android: layout_height =" match_parent "11 android: layout_weight =" 1 "/> 12 13 <fragment14 android: id = "@ + id/fragment2" 15 android: name = "com. example. fragment. fragment2 "16 android: layout_width =" wrap_content "17 android: layout_height =" match_parent "18 android: layout_weight =" 1 "/> 19 20 </LinearLayout>View Code MainActivity Code: 1 package com. example. fragmentdemo; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 6 public class MainActivity extends Activity {7 8 @ Override 9 protected void onCreate (Bundle savedInstanceState) {10 super. onCreate (savedInstanceState); 11 setContentView (R. layout. activity_main); 12} 13}View Code

Then run the project to display the Fragment. below is.

    

2. Add Fragment dynamically:

You only need to modify the code in MainActivity and activity_main.xml.

Activity_main.xml code: 1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 android: layout_width = "match_parent" 3 android: layout_height = "match_parent" 4 android: baselineAligned = "false" 5 android: orientation = "vertical"> 6 7 <LinearLayout 8 android: id = "@ + id/main" 9 android: layout_width = "match_parent" 10 android: layout_height = "match_parent" 11 android: orientation = "vertical"/> 12 13 </LinearLayout>View Code MainActivity Code: 1 package com. example. fragmentdemo; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 6 import com. example. fragment. fragment2; 7 8 public class MainActivity extends Activity {9 10 @ Override11 protected void onCreate (Bundle savedInstanceState) {12 super. onCreate (savedInstanceState); 13 setContentView (R. layout. activity_main); 14 getFragmentManager (). beginTransaction () 15. replace (R. id. main, new Fragment2 ()). commit (); 16} 17}View Code

 

Then run the project to dynamically display the Fragment. The following figure shows it.

 

Iii. Differences between Fragment under the app package and V4 package

1. Try not to use fragment in the app package, because it is available only after 3.0. The supported version is too high and cannot be used in earlier versions;

2. android. support. v4.app. Fragment: compatible with version 1.6;

3. Questions about the use of the <fragment> label for the two fragment:

(1) The <fragment> label can be used for both app. fragment and v4.fragment. Only when app. fragment is used, there is no special place to inherit the Activity.

(2) When v4.fragment uses the <fragment> label, pay special attention to the following: when the layout of this Activity contains the <fragment> label, this Activity must inherit the FragmentActivity, otherwise, an error is reported. In this case, the compilation system considers the fragment in the app package as the Fragment if it is not followed by the FragmentActivity. However, in this case, the Fragment in the FragmentAndroid official document of v4 is explained by Fragment in the app package.

(3) The Fragment classes and methods in the app package correspond to each other in the V4 package.

4. To use v4.fragment, call the FragmentManager () method instead of the getFragmentManager () method.

Reposted from: http://blog.csdn.net/a465456#/article/details/10379211. thank you.

 

      

The above is the simple usage of Fragment. Download the Demo. In the next section, I will describe the detailed usage of Fragment. Welcome to our CSDN blog address: http://blog.csdn.net/u010049692/article/details/38919531.

 

 

      


For Android development, please feel free to ask questions about Fragment.

1. Disabling remove is the best method;

2. In this example, Fragment is needed to implement the tab. ViewPager can be used to manage Fragment. ViewPager will pre-load the interface and slide smoothly.
3,
The following example shows how to replace the current Fragment with another Fragment and retain the current state of this Fragment in the rollback stack.
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment ();
FragmentTransaction transaction = getFragmentManager (). beginTransaction ();

// Replace whatever is in the fragment_container view with this fragment,
// And add the transaction to the back stack
Transaction. replace (R. id. fragment_container, newFragment );
Transaction. addToBackStack (null );

// Commit the transaction
Transaction. commit ();
 
Fragment in android Development

The view returned by the etView method is obtained through inflate using another XML file B.

The author of this source code encapsulates this view. It is not recommended for beginners to read this source code. Don't confuse it. It can be implemented simply, and its reusability is better.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.