"Android Development" it fragment development 1

Source: Internet
Author: User

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

First, Fragment simple introduction

1.Fragment appears as part of the activity interface;

2. Ability to have multiple fragment at the same time in an activity, and a fragment can be used in multiple activity;

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

4.Fragment is capable of responding to its own input events, and has its own life cycle, of course. Their life cycles are directly affected by the life cycle of the activity to which they belong.

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. A tablet's screen is much larger than a phone, and there are lots of other things to do with many other UI components, and there are a lot of other interactions 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 fragment to the XML:

New Fragment1, Fragment2 (note: There may be 2 packages that can choose to import android.app.Fragment or android.support.v4.app.Fragment are capable, I chose to use the former here. But there's 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 the second fragment "/></linearlayout>

We added two fragment to the 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>
Mainactivity code such as the following:
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);    }}

You can then execute project to display the fragment. Here is the.

    

2. Add fragment dynamically:

We just need to change 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 execute project to display the fragment dynamically, as follows.

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

1, try not to use the fragment in the app package. Since this is only after 3.0, the supported version number is too high, in the low version number is not used;

2, Android.support.v4.app.Fragment: can be compatible to 1.6 version number .

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

    (1) App.fragment and v4.fragment are able to use <fragment> tags are only in use when the assumption 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 <fragment> feel is the app package fragment to deal with. But at this point we are importing a sample of fragment in the Fragmentandroid official document in the V4 package that is explained by the fragment in the app package.

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

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



Above is the simple usage of fragment,Demo Download, the next section I will talk about the specific use of fragment. Welcome attention. My Blog Park address: http://www.cnblogs.com/getherBlog/p/3943547.html.


"Android Development" it fragment 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.