Efficient Extraction of loading, not afraid of loading more pages, extraction of loading

Source: Internet
Author: User
Tags blank page

Efficient Extraction of loading, not afraid of loading more pages, extraction of loading

Today's apps have basically two operations: loading data and displaying data on pages. However, if there are many pages. To load data on each page, you need to write the loading page. I used to use dialog to extract a class. Add the following code here if necessary. I found that I added the loading Code repeatedly most of the time. For this reason, we always work overtime.
Please refer to this Article without limit: http://blog.csdn.net/wanghao200906/article/details/46805085

Below is the status when there are many pages

I need to write more code one by one. Not only is the code not easy to read, but I am also tired.

Next we will discuss how to efficiently write loading.

There are four general page situations
Loading: it is to scroll the page and obtain the loaded data in the background. The data on each page is different, so the subclass is implemented to abstract the data directly.
Loading failed: Generally, You need to click and reload.
Blank Page: Click to reload
Loading successful: a successful page is displayed. Each page is different, so sub-classes must be implemented. This must be abstract.

I used framelayout to display the loaded page. There are four pages in total. The status returned by the loaded data allows the page to display the corresponding animation.

Repeat several ideas
1. Load the three pages first and start to execute the loading page.
2. load data and use the thread pool to process time-consuming hype. How to access the network and let the subclass determine whether data is available?
3. Data available display success page
Data unavailable display loading failure page
For example, if the data list is 0, an empty page is loaded.
The following code goes directly to the time:

Package com. example. every_text.view; import com.wang.cn. manager. threadManager; import com.wang.cn. utils. UIUtils; import android. content. context; import android. OS. systemClock; import android. util. attributeSet; import android. view. view; import android. widget. frameLayout;/*** first add the sequence load --> showPagerView --> createSuccessView ** @ author wanghao ** to put the time-consuming operations in the subclass into the load, and then load returns a status, in showPagerView, select the displayed Page Based on the status * If Installation is successful. For a long time, createSuccessView */public abstract class LoadingPager extends FrameLayout {// load the default state private static final int STATE_UNLOADED = 1; // The loading State private static final int STATE_LOADING = 2; // status of failed loading private static final int STATE_ERROR = 3; // status of empty loading private static final int STATE_EMPTY = 4; // status of Successful loading: private static final int STATE_SUCCEED = 5; private View mLoadingView; // view private View in a circle MErrorView; // incorrect view private View mEmptyView; // empty view private View mSucceedView; // successful view private int mState; // default state private int loadpage_empty; private int loadpage_error; private int loadpage_loading; public LoadingPager (Context context, int loading, int error, int empty) {super (context); loadpage_empty = empty; loadpage_error = error; loadpage_loading = loading; init ();} public LoadingPa Ger (Context context, AttributeSet attrs, int defStyle, int loading, int error, int empty) {super (context, attrs, defStyle); loadpage_empty = empty; loadpage_error = error; loadpage_loading = loading; init ();} public LoadingPager (Context context, AttributeSet attrs, int loading, int error, int empty) {super (context, attrs); init ();} private void init () {// initialization status mState = STATE_UNLOADED; // Initialization Three State views. At this time, three State views are superimposed together. mLoadingView = createLoadingView (); if (null! = MLoadingView) {addView (mLoadingView, new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT);} mErrorView = createErrorView (); if (null! = MErrorView) {addView (mErrorView, new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT);} mEmptyView = createEmptyView (); if (null! = MEmptyView) {addView (mEmptyView, new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT);} showSafePagerView ();} private void showSafePagerView () {// run directly to the main thread UIUtils. runInMainThread (new Runnable () {@ Override public void run () {showPagerView () ;}) ;} private void showPagerView () {// at this time, the mState is not null and the mState is declared as STATE_UNLOADED loading failed. Therefore, only the error in lodaing and empty are displayed. if (null! = MLoadingView) {mLoadingView. setVisibility (mState = STATE_UNLOADED | mState = STATE_LOADING? View. VISIBLE: View. INVISIBLE);} if (null! = MErrorView) {mErrorView. setVisibility (mState = STATE_ERROR? View. VISIBLE: View. INVISIBLE);} if (null! = MEmptyView) {mEmptyView. setVisibility (mState = STATE_EMPTY? View. VISIBLE: View. INVISIBLE);} if (mState = STATE_SUCCEED & mSucceedView = null) {mSucceedView = createSuccessView (); addView (mSucceedView, new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT);} if (null! = MSucceedView) {mSucceedView. setVisibility (mState = STATE_SUCCEED? View. VISIBLE: View. INVISIBLE) ;}} public void show () {// for the first time, it must be circled. Therefore, even the error and empty must be in the unload if (mState = STATE_ERROR | mState = STATE_EMPTY) {mState = STATE_UNLOADED;} state ;} // if it is unload, the state is changed to loading. At this time, the server obtains the data if (mState = STATE_UNLOADED) {mState = STATE_LOADING; TaskRunnable task = new TaskRunnable (); threadmanager.getlongpool(cmd.exe cute (task);} showSafePagerView ();}/*** production field Face ** @ return */protected abstract View createSuccessView ();/*** processing download time-consuming operations ** @ return */protected abstract LoadResult load (); /*** blank interface ** @ return */public View createEmptyView () {if (loadpage_empty! = 0) {return UIUtils. inflate (loadpage_empty);} return null;}/*** failed page ** @ return */public View createErrorView () {if (loadpage_empty! = 0) {return UIUtils. inflate (loadpage_error);} return null;}/*** page being rotated ** @ return */public View createLoadingView () {if (loadpage_empty! = 0) {return UIUtils. inflate (loadpage_loading);} return null;} class TaskRunnable implements Runnable {@ Override public void run () {final LoadResult loadResult = load (); SystemClock. sleep (500); UIUtils. runInMainThread (new Runnable () {@ Override public void run () {mState = loadResult. getValue (); showPagerView () ;}} public enum LoadResult {ERROR (3), EMPTY (4), SUCCESS (5); int value; loadResult (int value) {this. value = value ;}public int getValue () {return value ;}}}

The following is how to use

Package com.wang.cn. base; import com. example. every_text.view.LoadingPager; import com. example. every_text.view.LoadingPager.LoadResult; import com.wang.cn. r; import com.wang.cn. utils. UIUtils; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener;/*** @ author wang * @ version Creation Time: 11:31:11 AM, January 1, July 8, 2015 class description base class of activity */public abstract class BaseActivity extends Activity {public LoadingPager loadingPage; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); loadingPage = new LoadingPager (UIUtils. getContext (), R. layout. loadpage_loading, R. layout. loadpage_error, R. layout. loadpage_empty) // loads three pages {@ Override protected LoadResult load () {return BaseActivity. this. load (); // passed to the subclass} @ Override protected View createSuccessView () {return BaseActivity. this. createSuccessView (); // passed to the subclass}; // you can click loadingPage. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {loadingPage. show () ;}}); // display loadingPage of loading. show (); setContentView (loadingPage);}/*** refresh the page project ** @ return */protected abstract View createSuccessView (); /*** request the server to obtain the current status **/protected abstract LoadResult load ();}

What if the base class calls the parent class?

Package com.wang.cn; import android. content. intent; import android. OS. systemClock; import android. view. view; import android. view. view. onClickListener; import android. widget. textView; import com. example. every_text.view.LoadingPager.LoadResult; import com.wang.cn. base. baseActivity; import com.wang.cn. utils. UIUtils; import com.wang.cn. utils. viewUtils;/*** @ author wang * @ version Creation Time: july 8, 2015 11:31:11 class description main function */public class MainActivity extends BaseActivity {// refresh the page Project @ Override protected View createSuccessView () {View inflate = UIUtils. inflate (R. layout. activity_main); TextView TV = ViewUtils. findViewById (inflate, R. id. textView1); TV. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (UIUtils. getContext (), FragmetActivity. class); startActivity (intent) ;}}); return inflate ;}// refresh the page Project @ Override protected LoadResult load () {SystemClock. sleep (2000); return LoadResult. SUCCESS ;}}

Let's get started with loading data and displaying code. No longer need to worry about writing loading. You don't have to worry about where to add it because handler gets a lot of msg.
Code: http://download.csdn.net/detail/wanghao200906/8880719 (with a tool class I 've been using for years)

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.