"Android" Save fragment Toggle State

Source: Internet
Author: User

"Android" Save fragment Toggle State

Objective

Switching fragment frequently can lead to frequent release and creation, and if fragment is a bloated experience it is very bad to share a method here.

Statement

Welcome reprint, but please keep the original source of the article:)
Blog Park: http://www.cnblogs.com
Farmer Uncle: Http://over140.cnblogs.com

Body

First, the application scenario

1, do not use Viewpager

2, can not use replace to switch fragment, will cause fragment release (call Ondestroyview)

Second, the realization

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" >
</FrameLayout>

<radiogroup
Android:id= "@+id/main_radio"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_gravity= "Bottom"
android:gravity= "Bottom"
Android:layout_marginbottom= " -6DP"
android: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/main_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_bottom"
android:drawabletop= "@drawable/bottom_4"/>

<radiobutton
Android:id= "@+id/radio_button4"
style= "@style/main_tab_bottom"
android:drawabletop= "@drawable/bottom_5"/>
</RadioGroup>

</LinearLayout>

Code Description:

It is very common to put 5 radiobutton at the bottom and click to switch between different fragment.

2. Activity

To set the Setoncheckedchangelistener event for RadioButton, additional code:

@Override
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, cannot find getitem new one, Setprimaryitem set hide and show, and finally Finishupdate commit transaction.

Mcontainer is the framelayout in XML.

Third, Fragmentpageradapter core code

@Override
Public Object Instantiateitem (viewgroup container, int position) {
if (mcurtransaction = = null) {
Mcurtransaction = Mfragmentmanager.begintransaction ();
}

Final Long itemId = Getitemid (position);

Do we already has 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 object) {
if (mcurtransaction = = null) {
Mcurtransaction = Mfragmentmanager.begintransaction ();
}
if (DEBUG) log.v (TAG, "Detaching Item #" + getitemid (position) + ": f=" + Object
+ "v=" + ((Fragment) object). GetView ());
Mcurtransaction.detach ((Fragment) object);
}

@Override
public void Setprimaryitem (ViewGroup container, int position, object object) {
Fragment Fragment = (Fragment) object;
if (fragment! = Mcurrentprimaryitem) {
if (Mcurrentprimaryitem! = null) {
Mcurrentprimaryitem.setmenuvisibility (FALSE);
Mcurrentprimaryitem.setuservisiblehint (FALSE);
}
if (fragment! = null) {
Fragment.setmenuvisibility (TRUE);
Fragment.setuservisiblehint (TRUE);
}
Mcurrentprimaryitem = fragment;
}
}

@Override
public void Finishupdate (ViewGroup container) {
if (mcurtransaction! = null) {
Mcurtransaction.commitallowingstateloss ();
Mcurtransaction = null;
Mfragmentmanager.executependingtransactions ();
}
}

Fragmentpageradapter is the class that the support package comes with.

Iv. attention

Before their own simulation viewpager with Attach, setmenuvisibility, setuservisiblehint to control the display of fragment hidden, often appear fragment overlap phenomenon, very headache, No overlap has been found since the change.

V. Post-maintenance of the article

2013-12-01 Upload Sample code: Http://files.cnblogs.com/over140/SampleFragmentSwitch.zip

@Override
public void Setmenuvisibility (Boolean menuvisible) {
Super.setmenuvisibility (menuvisible);
if (this.getview () = null)
This.getview (). setvisibility (menuvisible? View.VISIBLE:View.GONE);
}

Re-doing the example found that they do not come out of effect, and later found that the code is missing.

2014-01-08 want to achieve the effect of this article or recommend the direct use of Viewpager, through the custom Viewpager disable the left and right swipe and automatic destruction can, according to the review under abnormal circumstances ghosting phenomenon is quite serious.

End

Need to look at the source code, can be a good solution to the problem.

Http://www.cnblogs.com/over140/p/3362047.html

Related Article

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.