Objective
Frequent switching fragment can lead to frequent release and creation, and if fragment experience is very bad, share a method here.
Body
First, the application scene
1, do not use Viewpager
2, can not use replace to switch fragment, will cause fragment release (call Ondestroyview)
Second, realize
1, XML
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <framelayout android:id= "@+id/" Container "android:layout_width=" match_parent "android:layout_height=" 0dip "android:layout_weight=" 1.0 "> </F ramelayout> <radiogroup android:id= "@+id/main_radio" android:layout_width= "Fill_parent" Android:layout_heigh t= "Wrap_content" android:layout_gravity= "bottom" android:gravity= "bottom" android:layout_marginbottom= " -6DP" Andro
id:orientation= "Horizontal" > <radiobutton android:id= "@+id/radio_button0" style= "@style/main_tab_bottom" android:drawabletop= "@drawable/bottom_1"/> <radiobutton android:id= "@+id/radio_button1" style= "@style/ma
In_tab_bottom "android:drawabletop=" @drawable/bottom_2 "/> <radiobutton android:id=" @+id/radio_button2 " style= "@style/main_tab_bottom" Android:drawabletop= "@drawable/bottom_3"/> <radiobutton android:id= "@+id/radio_button3" style= "@style/main_tab_bot Tom "android:drawabletop=" @drawable/bottom_4 "/> <radiobutton android:id=" @+id/radio_button4 "style=" @st Yle/main_tab_bottom "android:drawabletop=" @drawable/bottom_5 "/> </RadioGroup> </LinearLayout>
Code Description:
Very common bottom put 5 radiobutton, click to switch to different fragment.
2. Activity
Set Setoncheckedchangelistener event for RadioButton, other code:
public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {
if (ischecked) {
Fragment Fragment = (Fragment) mfragmentpageradapter.instantiateitem (Mcontainer, Buttonview.getid ());
Mfragmentpageradapter.setprimaryitem (Mcontainer, 0, fragment);
Mfragmentpageradapter.finishupdate (Mcontainer);
}
Private Fragmentpageradapter Mfragmentpageradapter = new Fragmentpageradapter (Getsupportfragmentmanager ()) {
@ Override public
Fragment getitem (int position) {
switch (position) {case
R.id.radio_button1:
return new Fragment1 ();
Case R.id.radio_button2: Return
new Fragment2 ();
Case R.ID.RADIO_BUTTON3: Return
new Fragment3 ();
Case R.ID.RADIO_BUTTON4: Return
new Fragment4 ();
Case R.ID.RADIO_BUTTON0:
default: Return
new Fragment0 ();
}
@Override public
int GetCount () {return
5;
}
};
Code Description:
Instantiateitem find fragment from Fragmentmanager, can't find GetItem new one, Setprimaryitem set hide and display, finally Finishupdate commit TRANSACTION, Mcontainer is the framelayout in XML.
Three, Fragmentpageradapter core code
@Override public Object Instantiateitem (viewgroup container, int position) {if (mcurtransaction = null) {MCURTR
Ansaction = Mfragmentmanager.begintransaction ();
Final Long itemId = Getitemid (position);
Do we already have this fragment?
String name = Makefragmentname (Container.getid (), itemId);
Fragment Fragment = Mfragmentmanager.findfragmentbytag (name);
if (fragment!= null) {if (DEBUG) log.v (TAG, "Attaching Item #" + ItemId + ": f=" + fragment);
Mcurtransaction.attach (fragment);
else {fragment = GetItem (position);
if (DEBUG) log.v (TAG, "adding Item #" + ItemId + ": f=" + fragment);
Mcurtransaction.add (Container.getid (), Fragment, Makefragmentname (Container.getid (), itemId));
} if (fragment!= Mcurrentprimaryitem) {fragment.setmenuvisibility (false);
Fragment.setuservisiblehint (FALSE);
return fragment; @Override public void Destroyitem (ViewGroup container, int position, object) {if (mcurtransaction = = null) {mcurtransaction = Mfragmentmanager.begintransaction (); } if (DEBUG) log.v (TAG, "Detaching Item #" + getitemid (position) + ": f=" + object + "v=" + ((Fragment) object). Getv
Iew ());
Mcurtransaction.detach ((Fragment) object); @Override public void Setprimaryitem (ViewGroup container, int position, object object) {Fragment Fragment = (fragm
ENT) object; if (fragment!= Mcurrentprimaryitem) {if (Mcurrentprimaryitem!= null) {mcurrentprimaryitem.setmenuvisibility (FAL
SE);
Mcurrentprimaryitem.setuservisiblehint (FALSE);
} if (fragment!= null) {fragment.setmenuvisibility (true);
Fragment.setuservisiblehint (TRUE);
} Mcurrentprimaryitem = fragment; @Override public void Finishupdate (ViewGroup container) {if (mcurtransaction!= null) {Mcurtransaction.comm
Itallowingstateloss ();
Mcurtransaction = null;
Mfragmentmanager.executependingtransactions (); } public void Setmenuvisibility (Boolean menuvisible) {
Super.setmenuvisibility (menuvisible);
if (This.getview ()!= null)
This.getview (). setvisibility (menuvisible? View.VISIBLE:View.GONE);
}
}
The Fragmentpageradapter is a support package with its own class.
Iv. attention
Before the simulation of their own viewpager with attach, setmenuvisibility, setuservisiblehint to control the display of fragment hidden, often appear fragment overlapping phenomenon, very headache, After this change, no overlap has been found at this time.
End: The above is an example of Android Fragment, hoping to help students in need.