Fragement Basics of Android

Source: Internet
Author: User

In Android3.0, Google introduced the concept of fragmentation (fragement) in order to fit a large screen such as a tablet.

Fragement can have its own layout and can be displayed in the activity as if it were a control. Can be added dynamically or statically.

The life cycle of fragement is similar to activity, with five more methods on the activity basis:

0nStop, OnStart, Onresume, onactivitycreated, Oncreateview, OnCreate, Onattach, Ondestoryview, Ondestory, Ondetach

Called when the onattach:fragment is associated with the activity.

Oncreateview: Called when the layout is loaded for fragment.

Onactivitycreated: Called when the OnCreate method in the activity finishes executing.

Called when the layout in Ondestoryview:fragment is removed.

Called when the ondetach:fragment is associated with the activity.

Fragmentation Basics:

http://blog.csdn.net/guolin_blog/article/details/8881711

Use fragmentation to be compatible with both mobile phones and tablets:

http://blog.csdn.net/guolin_blog/article/details/8744943

With the widespread use of fragmentation, fragement is no longer limited to solving the problem of plate adaptation.

Fragement + Viewpager + Radiogroup implements the bottom sliding menu bar:

1> Defining the main interface

<?xml version= "1.0" encoding= "Utf-8"?><LinearLayout android:orientation= "Vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"xmlns:android= "Http://schemas.android.com/apk/res/android" > <Android.support.v4.view.ViewPager Android:id= "@+id/main_viewpager"Android:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_weight= "1.0"/> <Radiogroup Android:id= "@+id/main_tab_radiogroup"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center"android:orientation= "Horizontal" > <RadioButton Android:id= "@+id/radio_message"Android:button= "@null"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:drawabletop= "@drawable/icon_message"Android:text= "Message"android:checked= "true"/> <RadioButton Android:id= "@+id/radio_contacts"Android:button= "@null"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:drawabletop= "@drawable/icon_contacts"Android:text= "Contacts"/> <RadioButton Android:id= "@+id/radio_setting"Android:button= "@null"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:drawabletop= "@drawable/icon_setting"Android:text= "Settings"/> </RadioGroup></LinearLayout>

2> icon click-to-color through the XML control, placed under the drawable:

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_checked=" true "android:drawable=" @drawable/message_selected "/>    <item android:state_checked= "false" android:drawable= "@drawable/message_unselected"/></selector>

3> Mainactivity Class

 Public classMainactivityextendsappcompatactivity { PublicViewpager Mviewpager; PrivateRadiogroup Mradiogroup; PrivateRadioButton Radiobutton_message; PrivateRadioButton radiobutton_contacts; PrivateRadioButton radiobutton_settings; PrivateArraylist<fragment>fragmentarraylist; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Initbutton ();    Initpager (); }     Public voidInitbutton () {Mradiogroup=(Radiogroup) Findviewbyid (R.id.main_tab_radiogroup); Radiobutton_message=(RadioButton) Findviewbyid (r.id.radio_message); Radiobutton_contacts=(RadioButton) Findviewbyid (r.id.radio_contacts); Radiobutton_settings=(RadioButton) Findviewbyid (r.id.radio_setting); //Monitor the button's click to display the corresponding fragementMradiogroup.setoncheckedchangelistener (NewRadiogroup.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) {                intCurrent = 0; Switch(checkedid) { Caser.id.radio_message:current= 0;  Break;  Caser.id.radio_contacts:current= 1;  Break;  Caser.id.radio_setting:current= 2;  Break; }                if(Mviewpager.getcurrentitem ()! =Current )                {Mviewpager.setcurrentitem (current);    }            }        }); }     Public voidInitpager () {Mviewpager=(Viewpager) Findviewbyid (R.id.main_viewpager); Fragmentarraylist=NewArraylist<fragment>(); Messagefragement messagefragement=Newmessagefragement (); Contactsfragement contactsfragement=Newcontactsfragement (); Settingsfragement settingsfragement=Newsettingsfragement ();        Fragmentarraylist.add (messagefragement);        Fragmentarraylist.add (contactsfragement);                Fragmentarraylist.add (settingsfragement); Mviewpager.setadapter (NewMyadapter (Getsupportfragmentmanager (), fragmentarraylist)); Mviewpager.setcurrentitem (0); //Monitor fragement swipe to display the corresponding buttonMviewpager.setonpagechangelistener (NewViewpager.onpagechangelistener () {@Override Public voidOnpagescrolled (intPositionfloatPositionoffset,intpositionoffsetpixels) {} @Override Public voidOnpageselected (intposition) {                Switch(position) { Case0: Mradiogroup.check (r.id.radio_message);  Break;  Case1: Mradiogroup.check (r.id.radio_contacts);  Break;  Case2: Mradiogroup.check (r.id.radio_setting);  Break; }} @Override Public voidOnpagescrollstatechanged (intState )    {            }        }); }     Public classMyadapterextendsFragmentpageradapter {ArrayList<Fragment>list;  PublicMyadapter (Fragmentmanager FM, arraylist<fragment>list) {            Super(FM);  This. List =list; } @Override PublicAndroid.support.v4.app.Fragment GetItem (intarg0) {            returnList.get (arg0); } @Override Public intGetCount () {returnlist.size (); }    }}

Fragement Basics of Android

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.