Reasons for fragment in Android

Source: Internet
Author: User
Tags switches

Home of the Viewpager has more than 10 fragment, in the fast switching, prone to lag phenomenon.

Second, analysis
When Viewpager switches to the current fragment, fragment loads the layout and displays the content, and if the user quickly switches Viewpager, that is, fragment needs to load the UI content and switch fragment frequently, It is easy to stutter (similar to loading a picture when the ListView is sliding quickly).

Iii. Programme of Treatment
1.Fragment Weight Reduction
If the Viewpager loaded fragment are lighter, and the layout of the fragment is properly streamlined, the fragment load can be increased to slow down the lag.

2. Prevent fragment from being destroyed
Viewpager when switching, if frequently destroy and load fragment, it is prone to lag phenomenon, prevent fragment destruction can effectively slow down the phenomenon.

(1) Cover Destroyitem method in Pageradapter to prevent destruction of fragment
@Override public void Destroyitem (ViewGroup container, int position, object object) {//super.destroyitem (container, posi tion, object); }

(2) through the Pageradapter Setoffscreenpagelimit () method can be set to retain a few fragment, the appropriate increase in parameters to prevent fragment is frequently destroyed and created.
Risk: In the case of more fragment, some low-end models are prone to oom problems.

3.Fragment Content Lazy Loading
(1) Description
When switching to the current fragment, do not immediately load the contents of the fragment, but instead load a simple empty layout, and then start a delay task, the delay time is T, when the user in the fragment stay longer than T, continue to perform the load task , and when the user switches to another fragment, the dwell time is lower than t, the delay task is canceled.

(2) Specific operation
First, set the delay task
Private Runnable Load_data = new Runnable () {@Override public void run () {//Here the data contents are loaded onto fragment}};

Start a task
<pre name= "code" class= "Java" > @Override public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
Initialize the view, it is a good idea to set a progress dialog box to prompt the user to load the data
Initview ();
Start the task, set here after 500 milliseconds to start loading data handler.postdelayed (load_data,500);

return view;
}


Cancel the task if the user switches to another fragment
The overloaded method that determines whether fragment is visible @override public void Setuservisiblehint (Boolean isvisibletouser) {Super.setuservisiblehint ( Isvisibletouser); if (!isvisibletouser) mhandler.removecallbacks (load_data); }

(3) Note
Using Setuservisiblehint to determine if a user is switching to another fragment, there is a flaw because the delay task is canceled when the Viewpager starts to slide, and in the case of insufficient sliding offsets, Viewpager will continue to roll back to the current fragment, causing the current fragment load task to be canceled without restarting the load task.
Here I use the practice is to add a onpagechangelistener to Viewpager, the listener's onpageselected (position) can listen to viewpager the current switch to which fragment, This will cancel the deferred load task for other fragment.

Reasons for fragment in Android

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.