fragment-instance creation and use

Source: Internet
Author: User

Today, contact with the fragment, on the Internet to see how it is used. Now it is created with the use of records, easy to query later.

1. Create your own fragment, inherit from fragment

 PackageCom.android.calculator2;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button;ImportAndroid.widget.ListView;Importandroid.view.animation.Animation;Importandroid.view.animation.AnimationUtils; Public classHistoryfragmentextendsFragment {PrivateView MView; PrivateListView Mlistview; PrivateButton mclearhistorybtn; PrivateButton mbackcalculatorbtn; PrivateHistoryadapter Mhistoryadapter; PrivateAnimation Myanimationin; PrivateAnimation myanimationout;  PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {MView = Inflater.inflate (R.layout.history_display, container,false); Myanimationin=animationutils.loadanimation (Getactivity (), R.anim.history_fragment_in_anim); Myanimationout=animationutils.loadanimation (Getactivity (), R.anim.history_fragment_out_anim); Myanimationin.setfillafter (true); Myanimationout.setfillafter (true); if(MView! =NULL) {mview.startanimation (myanimationin); MCLEARHISTORYBTN=(Button) Mview.findviewbyid (r.id.history_clear); MBACKCALCULATORBTN=(Button) Mview.findviewbyid (r.id.history_back); Mlistview=(ListView) Mview.findviewbyid (r.id.history_list); if(Getactivity ()instanceofHistoryclicklistener) {Mhistoryadapter=( (Historyclicklistener) getactivity ()). Gethistoryadapter ();            } mlistview.setadapter (Mhistoryadapter); Mbackcalculatorbtn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {if(Getactivity ()instanceofHistoryclicklistener)                    {((Historyclicklistener) getactivity ()). Historyclickhide ();            }                                                                                                                     }            }); Mclearhistorybtn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stub                    if(Getactivity ()instanceofHistoryclicklistener)                    {((Historyclicklistener) getactivity ()). Historyclickclear ();            }                }            }); returnMView; } Else {            return Super. Oncreateview (Inflater, container, savedinstancestate); }    }     Public InterfaceHistoryclicklistener {voidhistoryclickhide (); voidhistoryclickclear ();    Historyadapter Gethistoryadapter (); } @Override Public voidOndestroyview () {if(MView! =NULL) {mview.startanimation (myanimationout); }        Super. Ondestroyview (); }}

And then use it in your code.

                Fragmentmanager Mhistoryfm = Getfragmentmanager ();                 = mhistoryfm.begintransaction ();                " History ");                Mhistorytx.addtobackstack (null);                Mhistorytx.commit ();

It is also very simple to use. Just create it and load it into a layout in the activity.

The following points need to be noted

1. You must assign a layout container to your fragment in the XML of the activity, that is, the method inside

Mhistorytx.replace (R.id.frame_layout, Mhistoryfragment, "history");

R.id.frame_layout

2. If you want to remove a fragment, you must have an instance of this fragment, which is

Mhistorytx.replace (R.id.frame_layout, Mhistoryfragment, "History"); Mhistorytx.remove (mhistoryfragment);

The two mhistoryfragment above are the same instance

3. Try to get the handling of events in fragment to the activity. The usual method is

The operation that fragment wants to implement is defined as an interface, which is then implemented in the activity

Defining interfaces

     Public Interface Historyclicklistener {            void  historyclickhide ();             void historyclickclear ();            Historyadapter Gethistoryadapter ();    }

Implementing interfaces

 Public classCalculatorextendsActivityImplementsLogic.listener, historyclicklistener{@Override Public voidhistoryclickhide () {ANDROID.UTIL.LOG.E ("Zhangshuli", "MMMM"); Fragmentmanager Mhistoryfm=Getfragmentmanager (); Fragmenttransaction Mhistorytx=mhistoryfm.begintransaction ();        Mhistorytx.remove (mhistoryfragment);    Mhistorytx.commit (); } @Override Public voidhistoryclickclear () {ANDROID.UTIL.LOG.E ("Zhangshuli", "nnnn");        Mhistory.clear ();    Mlogic.onclear (); } @Override PublicHistoryadapter Gethistoryadapter () {returnMadapter; }}

Methods for callback activity in fragment

if instanceof Historyclicklistener) {(       (Historyclicklistener) getactivity ()). Historyclickclear ();}

fragment-instance creation and use

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.