Summary Fragment was designed to simplify the development of different screen resolutions, and he would represent a functional UI and its associated data as a module for reuse. Fragment can be thought of as an activity that can be embedded in a layout, with its own life cycle.
Fragment was designed to simplify the development of different screen resolutions, and he would represent a functional UI and its associated data as a module for reuse. Fragment can be thought of as an activity that can be embedded in a layout, with its own life cycle. For example, I now have Activitya and activityb on my phone, but there is more room on the tablet, which can show the functions of Activitya and ACTIVITYB in the same interface. Without fragment we would not be able to put Activitya and ACTIVITYB functions together without changing the code.
See fragment for more information: http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/0828/383.html
In this article, let's recall his usage:
Fragmentmanager Fragmentmanager ==new examplefragment (); Fragmenttransaction.add ( R.id.fragment_container, fragment); Fragmenttransaction.commit ();
R.id.fragment_containeris fragment the ID of the layout control to embed. When I want to be nested in another place, I ExampleFragment can do the same thing in a copy of the UI, if we want to R.id.fragment_container show another fragment in this control we can also use it fragmentTransaction.replace(R.id.fragment_container, fragment2)来替换之前的fragment。 because we can display different UI dynamically in the same place, Therefore fragment is suitable for the following interface effect:
The above interface is the effect of Baidu Cloud app, every click on a tab to switch to an interface, we can easily think of the principle is to click on a tab fragmenttransaction.replace to replace the new fragment.
But frequent switching of fragment can lead to frequent release and creation, which can be very bad if the fragment is bloated or needs to load data from the network. The Replace method is the culprit that causes the fragment to be recreated when switching, obviously we want to use fragment, but the substituted fragment is not released so that the next switch back is displayed directly.
We know that Viewpager actually has a fragmentpageradapter, fragmentpageradapter can not only be used in Viewpager, but also can be used as a fragment switch provider, In fact, viewpager his role in the same way. Fragmentpageradapter can save a lot of fragment for extraction, and encapsulates the interface to be removed, in fact, through the fragmenttransaction to achieve.
I can do this to achieve the effect of Baidu Cloud, how to remove from the Fragmentpageradapter fragment reference Viewpager Source:
Package Com.nmbb.sample.fragmentswitch;import Android.os.bundle;import android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmentpageradapter;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.framelayout;import Android.widget.RadioButton; Public classMainactivity extends Fragmentactivity implements Oncheckedchangelistener, Onclicklistener {PrivateRadioButton mTab1; PrivateRadioButton mTab2; PrivateRadioButton mTab3; PrivateRadioButton mTab4; PrivateRadioButton mTab5; PrivateFramelayout Mcontainer; PublicCompoundbutton Currentbuttonview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); MTAB1=(RadioButton) Findviewbyid (R.id.radio_button0); MTAB2=(RadioButton) Findviewbyid (R.id.radio_button1); MTAB3=(RadioButton) Findviewbyid (R.id.radio_button2); MTAB4=(RadioButton) Findviewbyid (R.id.radio_button3); MTAB5=(RadioButton) Findviewbyid (R.ID.RADIO_BUTTON4); Mcontainer=(framelayout) Findviewbyid (R.id.container); Mtab1.setoncheckedchangelistener ( This); Mtab2.setoncheckedchangelistener ( This); Mtab3.setoncheckedchangelistener ( This); Mtab4.setoncheckedchangelistener ( This); Mtab5.setoncheckedchangelistener ( This); Mtab1.setonclicklistener ( This); Mtab2.setonclicklistener ( This); Mtab3.setonclicklistener ( This); Mtab4.setonclicklistener ( This); Mtab5.setonclicklistener ( This); Mtab1.performclick (); } @Override Public voidoncheckedchanged (Compoundbutton Buttonview, Boolean isChecked) {if(isChecked) {Fragment Fragment=(Fragment) mfragmentpageradapter. Instantiateitem (Mcontainer, Buttonview.getid ()); Mfragmentpageradapter.setprimaryitem (Mcontainer,0, fragment); Mfragmentpageradapter.finishupdate (Mcontainer); } } PrivateFragmentpageradapter Mfragmentpageradapter =NewFragmentpageradapter (Getsupportfragmentmanager ()) {@Override PublicFragment GetItem (intposition) { Switch(position) { CaseR.id.radio_button1:returnFragmenttest.instantiation (2); CaseR.id.radio_button2:returnFragmenttest.instantiation (3); CaseR.id.radio_button3:returnFragmenttest.instantiation (4); CaseR.id.radio_button4:returnFragmenttest.instantiation (5); CaseR.id.radio_button0:default: returnFragmenttest.instantiation (1); }} @Override Public intGetCount () {return 5; } }; @Override Public voidOnClick (View v) {//TODO auto-generated Method Stub }}
Instantiateitem find fragment from Fragmentmanager, cannot find getitem new one, Setprimaryitem set hide and show, and finally Finishupdate commit transaction.
Mcontainer is the framelayout in XML.
FragmentTestThe code is as follows:
Package Com.nmbb.sample.fragmentswitch;import Android.os.bundle;import android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.TextView ; Public classFragmenttest extends Fragment { Public StaticFragmenttest Instantiation (intposition) {Fragmenttest Fragment=Newfragmenttest (); Bundle args=NewBundle (); Args.putint ("position", position); Fragment.setarguments (args); returnfragment; } @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {returnInflater.inflate (R.layout.fragment_test, container,false); } @Override Public voidonviewcreated (view view, Bundle savedinstancestate) {super.onviewcreated (view, savedinstancestate); TextView Text1=(TextView) View.findviewbyid (Android. R.ID.TEXT1); Text1.settext ("Fragment"+ getarguments (). GetInt ("position",1)); } @Override Public voidsetmenuvisibility (Boolean menuvisible) {super.setmenuvisibility (menuvisible); if( This. GetView ()! =NULL) This. GetView (). setvisibility (menuvisible?View.VISIBLE:View.GONE); }}
Where the following code is critical, there is no change in the following code to toggle tab when ghosting occurs:
@Override Public void setmenuvisibility (Boolean menuvisible) { super.setmenuvisibility (menuvisible); if (thisnull) this. GetView (). setvisibility (menuvisible? View.VISIBLE:View.GONE);}
Data hold when switching fragment