Optimal scheme of jamming when Android uses ViewPager to switch Fragment quickly

  • 2021-10-25 08:00:07
  • OfStack

When ViewPager switches to the current Fragment, Fragment will load the layout and display the content. If the user switches ViewPager quickly at this time, that is, Fragment needs to load UI content and switches Fragment frequently, it is easy to get stuck (similar to loading pictures while ListView slides quickly).

Optimization scheme:

1. Fragment lightweight

If the Fragment loaded by ViewPager is lightweight, the loading speed of Fragment can be improved by properly simplifying the layout of Fragment, thus slowing down the jamming phenomenon.

2. Prevent Fragment from being destroyed

When ViewPager is switched, if Fragment is destroyed and loaded frequently, it is easy to cause jamming. Preventing the destruction of Fragment can effectively slow down the jamming phenomenon.

(1) The method of overwriting destroyItem in PagerAdapter prevents the destruction of Fragment


@Override    
public void destroyItem(ViewGroup container, int position, Object object) {      
      //super.destroyItem(container, position, object);    
}

(2) Through the setOffscreenPageLimit () method of PagerAdapter, several Fragment can be set and kept, and the Fragment can be prevented from being destroyed and created frequently by appropriately increasing the parameters.

Risk: In the case of more Fragment, some low-end models are prone to OOM problems.

3. Fragment content delayed loading

(1) Description

When switching to the current Fragment, the contents of Fragment are not loaded immediately, but a simple empty layout is loaded first, and then a delay task is started. The delay time is T. When the user stays in the Fragment for more than T, the loading task continues; When the user switches to other Fragment and the residence time is lower than T, the delayed task is cancelled.

(2) Specific operation

First, set the deferred task


private Runnable LOAD_DATA = new Runnable() {    
    @Override    
    public void run() {      
     // Where the data content is loaded into the Fragment Upper     
    }  
};

Start a task


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
  // Initialize the view, where it is best to set the 1 Progress dialog boxes, prompting the user that data is being loaded 
  initView();
  // Start the task, set here 500 Start loading data in milliseconds   handler.postDelayed(LOAD_DATA,500)
  return view ; 
}

Cancel the task if the user switches to another Fragment


// Judge Fragment Visual overloaded method 
@Override  
public void setUserVisibleHint(boolean isVisibleToUser) {    
    super.setUserVisibleHint(isVisibleToUser);    
          if(!isVisibleToUser)        
            mHandler.removeCallbacks(LOAD_DATA);
}

(3) Attention

Using setUserVisibleHint to judge whether the user switches to other Fragment has a defect, because it will cancel the delayed task when ViewPager starts sliding, and in the case of insufficient sliding offset, ViewPager will continue to roll back to the current Fragment, resulting in the cancellation of the loading task of the current Fragment without restarting the loading task.

What I use here is to add an OnPageChangeListener to ViewPager, and the onPageSelected (position) of this listener can monitor which Fragment ViewPager is currently switching to, and cancel the delayed loading task of other Fragment here.

Summarize


Related articles: