Objective
Lazy load, is when the fragment is completely visible when we go to load data, we do application development, one Activity
may be viewpager
(or other containers) with multiple fragment to use, and if each fragment need to load data, or loaded locally, or loaded from the network, it activity
becomes necessary to initialize a large amount of resources at the time of the first creation. Such a result, of course, we will not be satisfied. So, can it be done when switching to this fragment, it is to initialize it?
The answer is in this method in the fragment setUserVisibleHint
.
See the API documentation for this method in fragment:
Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and are persistent across fragment instance state save and restore.
An app may set this to false to indicate that fragment's UI is scrolled out of visibility or are otherwise not directly Visible to the user. This May is used by the system to prioritize operations such as fragment lifecycle updates or loader ordering />parameters
This method is used to tell the system whether the fragment UI is visible. So we just need to inherit fragment and rewrite the method to implement the data loading operation when the fragment is visible, that is, fragment lazy loading.
The code is as follows:
* * DATE:14-7-17 * PROJECT:ACCESS-CONTROL-V2/package Cn.irains.access_contr
Ol_v2.common;
Import android.support.v4.app.Fragment; /** * AUTHOR:MSDX (645079761@qq.com) * TIME:14-7-17 PM 5:46 * * Public abstract class Lazyfragment extends Fragment
{protected Boolean isVisible;
/** * Implements slow loading of fragment data here. * @param isvisibletouser */@Override public void Setuservisiblehint (Boolean isvisibletouser) {Super.setuservis
Iblehint (Isvisibletouser);
if (Getuservisiblehint ()) {isVisible = true;
Onvisible ();
else {isVisible = false;
Oninvisible ();
} protected void Onvisible () {lazyload ();
} protected abstract void Lazyload (); protected void Oninvisible () {}}
In Lazyfragment, I added three methods, one that is onVisiable
called when fragment is set to be visible, and one is when onInvisible
fragment is set to invisible. In addition lazyLoad
, an abstract method is written, which is invoked inside the onvisible. You might think, why not getUserVisibleHint
just call it in there?
I wrote this for the reuse of code. Because in fragment, we also need to create a view ( onCreateView()
method), and may also need to do a small amount of initialization when it is not visible (such as initializing a remote service that needs to be invoked via Aidl), and so on. In the case of a setUserVisibleHint
onCreateView
previous call, lazyLoad
a null pointer exception is used when the view is uninitialized. Instead lazyLoad
of pumping out into a method, its subclasses can do this:
public class Openresultfragment extends lazyfragment{
//Flag bit, flag already initialized.
private Boolean isprepared;
@Override public
View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
LOG.D (Log_tag, "Oncreateview");
View view = Inflater.inflate (R.layout.fragment_open_result, container, false);
XXX Initializes the control of view
isprepared = true;
Lazyload ();
return view;
}
@Override
protected void Lazyload () {
if (!isprepared | |!isvisible) {return
;
}
Populating data for individual controls
}
In the above class, we added a flag bit to isPrepared
flag whether the initialization is complete. Then call after the initialization we need to complete, as in the example above, view
after initialization, set isPrepared
to True, and call the method at the same time lazyLoad()
. And in the lazyLoad()
midst of it, judgment isPrepared
and isVisible
as long as one is not true, does not go down. That is, it is only when the initialization is complete, and when it is visible, that it continues to load, which avoids the problem of using the uninitialized finish.
Summarize
Here's my introduction to the lazy load implementation of fragment, so if you're interested, you can delve deeper into it, such as writing a fragment with slow initialization and a feature that refreshes when visible. I hope the content of this article for everyone's study or work can help, if there is doubt you can message exchange.