In-depth analysis of dynamic management fragment

Source: Internet
Author: User

I. The relationship between fragment and activity

To understand dynamic management fragment first to understand the relationship between fragment and activity

Fragment can be understood to divide activity into several fragments, fragment is part of activity, life cycle is dependent on activity, and cannot exist alone. It is not difficult to understand that all fragment management (find, add, delete, replace) should be carried out in the activity that fragment relies on, that is, activity is the place of fragment interaction, Do not attempt to manage another fragment in one fragment although it is possible to implement the required functionality but not conform to the specification. The Fragmentmanager component is required to complete specific operations on fragment.

Second, the use of Fragmentmanager components

The key code for using the Fragmentmanager component is as follows:

1, create Fragmentmanager Components Fragmentmanager Fm=super.getsupportfragmentmanager ();//2, Query method, Get an activity in one of the Fragmentfm.findfragmentbyid (R.id.fooler) fm.findfragmentbytag ("TagName")//3, Dynamically add Fragmentfm.begintransaction (). Add (r.id.content,contentfragment, "content"). commit ();//4, Replace Fragmentfm.begintransaction (). replace (R.id.oldfragment, newfragment). commit ();//5, Remove Fragmentfm.begintransaction (). Remove (R.id.myfragment). commit ();//6, pass Data f.setarguments (Bundle) method to fragment pass data

The above management fragment code should be organized in the activity that fragment belongs to, the following is a case to see the concrete implementation.

III. implementation of the case

First look at this application case (), when you click on the bottom of the first and second regions to achieve the central area of the switch

Interface Analysis: There are three areas: the head area titlefragment, the central area is a framelayout layout through the code dynamically loaded contentfragment, the bottom area floorfragment

1,the activity layout is as follows
 
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android "Android:layout_width=" match_parent "android:layout_height=" match_parent "> <fragment android:id=" @+id/fgTi Tle "android:layout_width=" match_parent "android:layout_height=" 45DP "Android:name=" Com.jereh.androi D.course.fragment.titlefragment "/> <framelayout android:id=" @+id/content "Android:layout_width=" Ma Tch_parent "android:layout_height=" match_parent "android:layout_below=" @id/fgtitle "/> <fr         Agment android:id= "@+id/floor" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:layout_alignparentbottom= "true" android:orientation= "horizontal" android:name= "com.jereh.an Droid.course.fragment.FloorFragment "/> </relativelayout> 
2.floorfragment Code
public class Floorfragment extends Fragment implements Onclicklistener {@Override public void Onattach (Activity act            ivity) {Super.onattach (activity); } @Override Public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancest ATE) {//TODO auto-generated method Stub View view=inflater.inflate (R.layout.floor_fragment, Container,fals        e);        LinearLayout home= (linearlayout) View.findviewbyid (r.id.home);        LinearLayout list= (linearlayout) View.findviewbyid (r.id.list);        Home.setonclicklistener (this);        List.setonclicklistener (this);    return view; }//Defines the interface, which is implemented in activity to reduce the coupling between activity and fragment public interface oncontentfragmentlistener{void SetContent    Fragment (String param);    } private Oncontentfragmentlistener Oncontentfragmentlistener; public void Setoncontentfragmentlistener (Oncontentfragmentlistener oncontentfragmentlistener) {This.onContentfragmentlistener = Oncontentfragmentlistener; } @Override public void OnClick (view view) {//TODO auto-generated Method stub if (Oncontentfragmentli Stener!=null) {switch (View.getid ()) {case R.id.home://callback mechanism, calling method of implementing Class O                Ncontentfragmentlistener.setcontentfragment ("main panel Fragment");            Break                Case R.id.list:oncontentfragmentlistener.setcontentfragment ("list Information");            Break }            }    }}
3. Contentfragment Code
public class Contentfragment extends Fragment {    private String title;    public void Setarguments (Bundle args) {        title=args.getstring ("title");    }    @Override public    View Oncreateview (layoutinflater inflater, ViewGroup container,            Bundle savedinstancestate) {        View view=inflater.inflate (r.layout.content_fragment, container,false);        if (Title!=null) ((TextView) View.findviewbyid (r.id.tvcontent)). SetText (title);        return view;}    }
4. Dynafragmentactivity Code
public class Dynafragmentactivity extends fragmentactivity implements Floorfragment.oncontentfragmentlistener {pri    Vate contentfragment contentfragment;    Private floorfragment floor;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Super.setcontentview (r.layout.dyna_fragment_activity);    Initfragment (); private void Initfragment () {floor= (floorfragment) Super.getsupportfragmentmanager (). Findfragmentbyid (R.id.floor)        ;        Floor.setoncontentfragmentlistener (this);//register listening, incoming implementation class object Contentfragment=new contentfragment ();        Fragmentmanager Fm=super.getsupportfragmentmanager ();    Fm.begintransaction (). Add (r.id.content,contentfragment, "content"). commit (); public void Setfragment (Fragment Fragment) {Getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.co    Ntent, fragment). commit (); } @Override public void setcontentfragment (String param) {//TODO auto-generated Method Stub bundle Bundle=new bundle ();        Bundle.putstring ("title", param);        Contentfragment=new contentfragment ();        Contentfragment.setarguments (bundle);    Super.getsupportfragmentmanager (). BeginTransaction (). replace (R.id.content, contentfragment). commit (); }    }

In-depth analysis of dynamic management fragment

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.