Android fragment switch load data lag problem

Source: Internet
Author: User

Then the progress of the previous project, the previous article on how to use fragment to implement the drop-down menu, the common menu, to achieve the function of switching the main interface data, the problem is: the use of the fragment switch interface method, but the load of data too much, The user switches from one interface to the interface, at least a two-second lag, which is intolerable, the code is as follows:

private void Initopenmenuitem (View popupwindow_view) {<span style= "White-space:pre" ></span> Drawablecentertextview Menu_price = (drawablecentertextview) popupwindow_view<span style= "White-space:pre" > </span>.findviewbyid (R.id.menu_price); <span style= "White-space:pre" ></span>menu_ Price.setonclicklistener (New Onclicklistener () {<span style= "White-space:pre" ></span> Fragmenttransaction transaction;<span style= "White-space:pre" ></span> @Override <span style= " White-space:pre "></span>public void OnClick (View v) {<span style=" White-space:pre "></span> Transaction = Manager.begintransaction (); <span style= "White-space:pre" ></span><span style= " Background-color:rgb (255, 255, 0); " ><span style= "Font-size:18px;color: #ff0000;" ><strong>transaction.replace (R.id.fragment_container, pricefragment); </strong></span></ Span><span style= "White-space:pre" ></span>titleview.seTtext (Pricefragment.getfragmenttitle ()); <span style= "White-space:pre" ></span>transaction.commit (); <span style= "White-space:pre" ></span>popupwindow.dismiss (); <span style= "White-space:pre" ></ Span>}<span style= "White-space:pre" &GT;&LT;/SPAN&GT;}); <span style= "White-space:pre" &GT;&LT;/SPAN&GT;}
The above code in the switch fragment interface, according to fragment method Api,replace is to regenerate the instance, which will inevitably lead to the data reload, we have to take a hidden way or add a progress bar to alleviate. Here I use the Hide method provided by fragment, to achieve a quick display of data, modify the code as follows:

private void Initopenmenuitem (View popupwindow_view) {Drawablecentertextview Menu_price = (Drawablecentertextview) Popupwindow_view.findviewbyid (R.id.menu_price); Menu_price.setonclicklistener (new Onclicklistener () { Fragmenttransaction transaction; @Overridepublic void OnClick (View v) {transaction = Manager.begintransaction ();/* * qiulinhe:2015 July 21 10:54:51 * Solve the problem of switch lag */<span style= "Font-size:18px;color: #333333;" ><strong style= "Background-color:rgb (192, 192, 192); >if (!pricefragment.isadded ()) {//First determine if the add is over Transaction.hide (openpositionfragment). Add (R.id.fragme Nt_container, Pricefragment). commit (); Hides the current fragment,add next to activity in Titleview.settext (Pricefragment.getfragmenttitle ());}                else {transaction.hide (openpositionfragment). Show (Pricefragment). commit ();//Hide current fragment, show next Titleview.settext (Pricefragment.getfragmenttitle ()); </strong></span><span style= "color: #c0c0c0;" >}</span>p Opupwindow.dismiss ();}});} 
The principle is:

Turned to the Android official doc, and some components of the source code, found that the replace () This method is only used in the last fragment no longer need to use the simple method.

The correct way to switch is add (), hide (), add () another fragment when switching again, just hide () and show () another.
This makes it possible to do multiple fragment switches without re-instantiating:


Because is the company project, cannot upload the entire project document, but may refer to my solution way, has the same question schoolmate can exchange to have the more elegant solution.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android fragment switch load data lag problem

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.