Fragment development of "Android development" 1

Source: Internet
Author: User

   Always know that fragment is very powerful, but has not been to study, now some free time, so I went to learn a brief introduction of fragment. I will also write down their learning process, if there is any shortage of places to hope that Daniel, common progress!

I. Introduction of Fragment

1.Fragment appears as part of the activity interface;

2. Multiple fragment can occur simultaneously in one activity, and a fragment may be used in multiple activity;

3. During activity operation, you can add, remove, or replace fragment (add (), remove (), replace ());

4.Fragment can respond to its own input events and have its own life cycle, and of course, their life cycle is directly affected by the activity's life cycle that they belong to.

Then why are we using fragment? The main purpose is to support a more dynamic and flexible UI design on large-screen devices, such as tablets. Tablet screens are much larger than phones, have more room to put more UI components, and create more interaction between these components. We can think of fragment as "small activity", fragment more concise.

Second, the simple use of fragment

So let's simply show 2 fragment as an example to explain.

    

1. Add the fragment in the XML:

New Fragment1, Fragment2 (note: There may be 2 packages can be selected to import android.app.Fragment or android.support.v4.app.Fragment is possible, I chose to use the former, but there is a difference between the two, and I'll talk about it at the end :


Fragment1 Code:
Package Com.example.fragment;import Android.app.fragment;import Android.os.bundle;import android.util.Log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Com.example.fragmentdemo.r;public class Fragment1 extends Fragment {    @Override public    View Oncreateview ( Layoutinflater inflater, ViewGroup container,            Bundle savedinstancestate) {        log.e ("TAG", "in");        Return Inflater.inflate (R.layout.fragment1, container, false);}    }
Fragment2 Code:
Package Com.example.fragment;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Com.example.fragmentdemo.r;public class Fragment2 extends Fragment {    @Override public    View Oncreateview ( Layoutinflater inflater, ViewGroup container,            Bundle savedinstancestate) {        return inflater.inflate ( R.layout.fragment2, container, false);}    }
XML code for FRAGMENT1:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: Background= "#FF69B4"    android:orientation= "vertical" >    <textview        android:layout_width= "Wrap_ Content "        android:layout_height=" wrap_content "        android:layout_gravity=" center "        android:text=" This is the first fragment "/></linearlayout>
XML code for FRAGMENT2:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: Background= "#EECBAD"    android:orientation= "vertical" >    <textview        android:layout_width= "Wrap_ Content "        android:layout_height=" wrap_content "        android:layout_gravity=" center "        android:text=" This is a second fragment "/></linearlayout>
We add two fragment in Activity_main.xml, the code is as follows:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:baselinealigned=" false ">    <fragment        Android:id= "@+id/fragment1"        android:name= "com.example.fragment.Fragment1"        android:layout_width= "Wrap_ Content "        android:layout_height=" match_parent "        android:layout_weight=" 1 "/>    <fragment        Android:id= "@+id/fragment2"        android:name= "Com.example.fragment.Fragment2"        android:layout_width= "wrap _content "        android:layout_height=" match_parent "        android:layout_weight=" 1 "/></linearlayout>
The mainactivity code is as follows:
Package Com.example.fragmentdemo;import Android.app.activity;import Android.os.bundle;public class MainActivity Extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate ( Savedinstancestate);        Setcontentview (R.layout.activity_main);    }}

Then run the project to show fragment, below.

    

2. Add fragment dynamically:

We just need to modify the code in Mainactivity and Activity_main.xml.

Activity_main.xml Code:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:baselinealigned=" false "    android:orientation=" Vertical ">    <linearlayout        android:id=" @+id/main "        android:layout_width=" Match_parent        " android:layout_height= "Match_parent"        android:orientation= "vertical"/></linearlayout>
Mainactivity Code:
Package Com.example.fragmentdemo;import Android.app.activity;import Android.os.bundle;import Com.example.fragment.fragment1;public class Mainactivity extends Activity {    @Override    protected void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Getfragmentmanager (). BeginTransaction ()                . Replace (R.id.main, New Fragment1 ()). commit ();}    }

Then run the project to dynamically display the fragment, the following is.

Third, the difference between the app package and the fragment under the V4 package

1, try not to use the app package in the fragment, because this is after 3.0, the supported version is too high, in the lower version is not used;

2, Android.support.v4.app.Fragment: can be compatible to 1.6 of the version ;

3. Questions about the use <fragment> labelling of these two fragment:

    (1) App.fragment and v4.fragment can use <fragment> tag only in the use of the time if it is app.fragment there is no special place to inherit activity.

    (2) When v4.fragment uses <fragment> tags , it should pay special attention to: when the activity of the layout of the <fragment> tag, the Activity must inherit fragmentactivity, otherwise it will error. At this point, if you do not B Ji Cheng fragmentactivity, the compilation system will think of <fragment> as the app package fragment to handle. But at this point we are importing an example of fragment in the Fragmentandroid official document in the V4 package, which is explained by the fragment in the app package.

(3) The class and method of fragment in the app package have corresponding corresponding in the V4 package.

Reprinted from: http://blog.csdn.net/a465456465/article/details/10415211, thank you.



The above is a simple way to use fragment,Demo Download, the next section I'll talk about fragment's detailed use. Welcome attention, my Blog Park address: http://www.cnblogs.com/getherBlog/p/3943547.html.


Fragment development of "Android development" 1

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.