When you use the mobile news client, there will be a discovery, most of the news clients will classify news, such as headlines, entertainment, sports, technology and so on, how to achieve this interface? This realization is actually very simple, that is, in a fragment to achieve a number of viewpage switch, and then put a tablayout on the ViewPage, the association can achieve linkage effect. If you do not feel very clear, I can write a special article on the fragment into a number of ViewPage blog, today, I mainly introduce how to achieve the interface that is fragment lazy load . Well, everyone would be surprised. Since the loading interface is loaded directly, why use lazy load? This needs to say the benefits of lazy loading. The previous article has already introduced, most news client realizes each kind of news switching principle is a fragment to hold several viewpage, if enters the program to all viewpage data to load, then will undoubtedly increase the app to run the burden, Lazy loading can be implemented when the program started fragment ViewPage only preload an interface, wait until the next interface to load the next viewpage data, and implementation of the load interface cache, the next time you switch to this interface will no longer reload. Well, explain why you want to use lazy load, let's go to today's topic.
First step: customize a fragment let him inherit fragment
1, rewrite the Setuservisiblehint method
2. Define an abstract method Lazyinitdata ()
Package com.jereh.jinritoutiao.fragmentdemo.fragment;
Import android.support.v4.app.Fragment;
/**
* Created by Zhangdi on 2016/8/8.
* * Public
abstract class Basefragment extends Fragment {
protected Boolean isVisible = false;
@Override public
void Setuservisiblehint (Boolean isvisibletouser) {
Super.setuservisiblehint ( Isvisibletouser);
if (isvisibletouser) {
//load data
isVisible = true;
Lazyinitdata ();
} else {
isVisible = false;
}
}
public abstract void Lazyinitdata ();
}
Step Two: redefine a fragment inheritance just defined fragment
1, first define a global variable to save the state of fragment (this step is prepared for the cache interface) defines two Boolean variables indicating that the Fragment view control is ready and that the current fragment has already loaded the data
2, to determine whether the state of the fragment is not empty, and then generate a new interface
3, rewrite the parent class Lazyinitdata () method to implement lazy loading
/** * A Simple {@link Fragment} subclass.
* * public class Netnewsfragment extends Basefragment {//define a global variable to hold the fragment state private View V;
ListView display of the data private list<netnews> mdata;
Private Pulltorefreshlistview LV;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
String u= getarguments (). getString ("API"); if (!
Textutils.isempty (u)) {url = u; @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle Savedinstancesta TE) {if (v==null) {//link layout v = inflater.inflate (r.layout.fragment_net_news, container, Fals
e);
Find ViewPage LV = (pulltorefreshlistview) V.findviewbyid (R.ID.LV01);
Mdata = new arraylist<> ();
Initialize Adapter adapter = new Netnewsadapter (Mdata, getactivity ());
Lv.setadapter (adapter);
The interface is ready isprepared = true; (New ANDROID.OS.handler ()). postdelayed (New Runnable () {@Override public void run () {lazyinitdata ();
}},200);
} return v; /** * Initialize fragment data method to implement lazy load/@Override public void Lazyinitdata () {if (isprepared&&isvisible
&&isfirst) {lv.setrefreshing (); Volleyutil.get (url+ "? num=20&page=" +page). Setcallback (New Netcallback ()). Build (). Addrequestheader ("Apike
Y ", Constants.api_key). Start ();
Isfirst = false;
}
}
Here I'm highlighting the lazy loading code, so I've omitted Pulltolistview's style monitoring, and here (New Android.os.Handler ()). postdelayed (New Runnable () The method in this paper is to implement asynchronous loading in a multithreaded way and then have time to generate the interface component and then load the data when the initial layout is reached.
At this point, the implementation of the interface (Fragment) data lazy load of the purpose is achieved. Everyone in the future if you want to achieve multiple interface switching and data needs network request, we recommend lazy load.
Okay, so here's the introduction to lazy loading.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.