Android Basic---->fragment use

Source: Internet
Author: User

Fragmentation (Fragment) is a UI fragment that can be embedded in an activity, which allows the program to make more reasonable and full use of the space on the large screen, so it is widely used on the tablet.

Basic examples of Fragment

First, increase the Fragment,another_right_fragment.xml file:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "#ffff00"android:orientation= "vertical">    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_horizontal"Android:text= "This was another right fragment"android:textsize= "20SP" /></LinearLayout>

Second, Anotherrightfragment class:

 PackageCom.example.fragmenttest2;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/*** Created by Linux on 2016/8/9.*/ Public classAnotherrightfragmentextendsFragment {@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View View=inflater.inflate (r.layout.another_right_fragment, container,false); returnview; }}

Third, use in the mainactivity

 Public void addfragment (view view) {    new  anotherrightfragment ();    Getfragmentmanager (). BeginTransaction (). Add (R.id.right_layout, Anotherrightfragment, Fragment_tag). commit ();}

Four, the operation effect is as follows:

Some introductions of fragment

First, call Fragmentmanager's Findfragmentbyid () method, you can get an instance of the corresponding fragment in the activity, and then you can easily invoke the method in the fragment.

Rightfragment rightfragment = (rightfragment) Getfragmentmanager (). Findfragmentbyid (r.id.right_fragment);

Second, in each fragment can be called by the Getactivity () method to get the current fragment associated with the activity instance

Mainactivity activity = (mainactivity) getactivity ();

Third, replace the fragment,

 Public void replacefragment (view view) {    new  anotherrightfragment ();     = Getfragmentmanager ();     = fragmentmanager.begintransaction ();    Transaction.replace (r.id.right_layout, fragment);     // If this doesn't add up, back out    Transaction.addtobackstack (null);    Transaction.commit ();}

Iv. deletion of Fragment:

 Public void deletefragment (view view) {    Getfragmentmanager (). BeginTransaction (). Remove (Getfragmentmanager (). Findfragmentbytag (Fragment_tag)). commit ();}

Five, the operation effect:

Fragment using the methods in activity

First, the interface is defined in fragment and converted in the Onattach method, and then the method of mcallback.onarticleselected () can be called in fragment other methods.

Onheadlineselectedlistener Mcallback; Public InterfaceOnheadlineselectedlistener { Public voidonarticleselected ();} @Override Public voidOnattach (Context context) {log.i (TAG,"On Attach"); Super. Onattach (context); Try{mcallback=(onheadlineselectedlistener) context; } Catch(classcastexception e) {Throw NewClassCastException (context.tostring () + "must implement Onheadlineselectedlistener"); }}

Second, inherit the Onheadlineselectedlistener interface in Mainactivity.

 Public class extends Implements Headlinesfragment.onheadlineselectedlistener {    @Override    publicvoid  Onarticleselected () {        toast.maketext (mainactivity. This, "Hello World", Toast.length_short). Show ();    }}

This method is because it is in mainactivity, that is, can realize the interaction between fragment and fragment, but it is true.

The life cycle of fragment

Friendship Link

Use of Android Foundation---->fragment

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.