Interface data loading is implemented in Android, while interface loading in Android

Source: Internet
Author: User

Interface data loading is implemented in Android, while interface loading in Android

When we use the mobile phone news client, we will find that most news clients classify news, such as headlines, entertainment, sports, and technology. How can we implement this interface? This implementation is actually very simple. It is to implement switching between multiple viewpages in a Fragment, and then put a TabLayout on the ViewPage to achieve the linkage effect. If you don't know how to implement lazy loading of the Fragment interface, you can write a blog about multiple viewpages in Fragment later. Then, we will be wondering why we should use lazy loading even though loading the interface is good? This requires the benefit of lazy loading. As mentioned above, most news clients implement various news switching based on the principle that multiple viewpages are stored in one Fragment. If data in all viewpages is loaded after entering the program, this will undoubtedly increase the load on the APP runtime, while lazy loading will enable viewpage in fragment to only pre-load one interface when the program starts, when sliding to the next page, load the data of the next viewpage and load the interface cache. The next time you switch to this page, the page will not be reloaded. Now, let's explain why we should use lazy loading. Let's start with today's topic.

Step 1: Customize a fragment to allow it to inherit Fragment

1. Rewrite the setUserVisibleHint method.

2. Define an abstract method lazyInitData ()
1 package com. jereh. jinritoutiao. fragmentdemo. fragment; 2 3 import android. support. v4.app. fragment; 4 5/** 6 * Created by zhangdi on 2016/8/8. 7 */8 public abstract class BaseFragment extends Fragment {9 protected boolean isVisible = false; 10 @ Override11 public void setUserVisibleHint (boolean isVisibleToUser) {12 super. setUserVisibleHint (isVisibleToUser); 13 if (isVisibleToUser) {14 // load data 15 isVisible = true; 16 lazyInitData (); 17} else {18 isVisible = false; 19} 20} 21 public abstract void lazyInitData (); 22}

Step 2: Define another fragment to inherit the defined fragment

1. define a global variable to save the fragment state (this step is for the cache interface) define two boolean variables to indicate that the fragment view control has been prepared and whether the current fragment has loaded data.

2. Check whether the fragment status is empty. If it is empty, a new interface is generated.

3. Rewrite the lazyInitData () method of the parent class to implement lazy loading.
1/** 2 * A simple {@ link Fragment} subclass. 3 */4 public class NetNewsFragment extends BaseFragment {5 // define a global variable to save the Fragment state 6 private View v; 7 // listview displays 8 private lists of Data <NetNews> mData; 9 private PullToRefreshListView lv; 10 @ Override11 public void onCreate (Bundle savedInstanceState) {12 super. onCreate (savedInstanceState); 13 String u = getArguments (). getString ("api"); 14 if (! TextUtils. isEmpty (u) {15 url = u; 16} 17} 18 19 @ Override20 public View onCreateView (LayoutInflater inflater, ViewGroup container, 21 Bundle savedInstanceState) {23 if (v = null) {24 // associate the layout with 25 v = inflater. inflate (R. layout. fragment_net_news, container, false); 26 // find ViewPage27 lv = (PullToRefreshListView) v. findViewById (R. id. lv01); 28 mData = new ArrayList <> (); 29 // initialize the adapter 30 adapter = new NetNewsAdapte R (mData, getActivity (); 31 lv. setAdapter (adapter); 32 // The interface has been prepared. 33 isPrepared = true; 34 (new android. OS. handler ()). postDelayed (new Runnable () {35 @ Override36 public void run () {37 lazyInitData (); 38} 39}, 200); 40} 41 return v; 42} 43 44/** 45 * method for initializing Fragment data to achieve lazyInitData () {49 if (isPrepared & isVisible & isFirst) {50 lv. setRefreshing (); 51 VolleyUtil. get (url + "? Num = 20 & page = "+ page) 52. setCallBack (new NetCallBack () 53. build () 54. addRequestHeader ("apikey", Constants. API_KEY) 55. start (); 56 isFirst = false; 57} 58}

In order to highlight the code for lazy loading, the PullToListView style listening is omitted.(New android. OS. Handler ()). PostDelayed (newRunnable ())The method in is to asynchronously load the interface component and load the data when the initialization layout is reached.

So far, the implementation of Fragment data in Android is achieved by lazy loading. If you want to implement multiple interface switches and network requests are required for data in the future, we recommend that you use lazy loading. Well, let's talk about the introduction of lazy loading. If you feel that my writing is not bad, please recommend it, if you feel that the author has a poor introduction or that there is something wrong with the knowledge point, I hope to comment on it. Thank you.

 

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.