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