├── .gitignore
├── ic_launcher-web.png
├── res
├── drawable-hdpi
│ └── ic_launcher.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ └── ic_launcher.png
├── drawable-xxhdpi
│ └── ic_launcher.png
├── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
└── layout
│ ├── item_example1.xml
│ ├── load_empty.xml
│ ├── load_error.xml
│ ├── load_ing.xml
│ ├── activity_main.xml
│ ├── activity_example2.xml
│ ├── activity_example1.xml
│ └── activity_simple.xml
├── .classpath
├── project.properties
├── proguard-project.txt
├── src
└── com
│ └── shizhefei
│ ├── test
│ ├── MainActivity.java
│ ├── SimpleActivity.java
│ ├── Example2Activity.java
│ ├── load
│ │ └── LoadViewHelper.java
│ └── Example1Activity.java
│ └── view
│ └── vary
│ ├── IVaryViewHelper.java
│ ├── VaryViewHelperX.java
│ └── VaryViewHelper.java
├── .project
├── README.md
└── AndroidManifest.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | /gen/
2 | /bin/
3 |
--------------------------------------------------------------------------------
/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LuckyJayce/LoadViewHelper/HEAD/ic_launcher-web.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LuckyJayce/LoadViewHelper/HEAD/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LuckyJayce/LoadViewHelper/HEAD/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LuckyJayce/LoadViewHelper/HEAD/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LuckyJayce/LoadViewHelper/HEAD/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/res/layout/item_example1.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | LoadView
5 | Hello world!
6 | Settings
7 | SimpleActivity
8 | Example1Activity
9 | Example2Activity
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-22
15 |
--------------------------------------------------------------------------------
/res/layout/load_empty.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
21 |
22 |
--------------------------------------------------------------------------------
/res/layout/load_error.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
21 |
22 |
--------------------------------------------------------------------------------
/res/layout/load_ing.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
20 |
21 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/src/com/shizhefei/test/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.shizhefei.test;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.view.View;
7 |
8 | import com.shizhefei.view.loadview.R;
9 |
10 | public class MainActivity extends Activity {
11 |
12 | @Override
13 | protected void onCreate(Bundle savedInstanceState) {
14 | super.onCreate(savedInstanceState);
15 | setContentView(R.layout.activity_main);
16 | }
17 |
18 | public void showSimple(View view) {
19 | startActivity(new Intent(getApplicationContext(), SimpleActivity.class));
20 | }
21 |
22 | public void showExample1(View view) {
23 | startActivity(new Intent(getApplicationContext(), Example1Activity.class));
24 | }
25 |
26 | public void showExample2(View view) {
27 | startActivity(new Intent(getApplicationContext(), Example2Activity.class));
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | LoadView
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
14 |
15 |
21 |
22 |
28 |
29 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LoadViewHelper
2 | 切换加载中,加载失败,加载成功布局,定义一个LoadViewHelper所有界面通用。
3 |
4 |
5 | LoadViewHelper helper = new LoadViewHelper(listView);
6 | //将listview切换成显示加载中布局
7 | helper.showLoading("加载中...");
8 | //将listview切换成显示空数据布局
9 | helper.showEmpty("暂无数据", "重试", new OnClickListener() {
10 |
11 | @Override
12 | public void onClick(View v) {
13 |
14 | }
15 | });
16 |
17 | # 联系方式和问题建议
18 |
19 | * 微博:http://weibo.com/u/3181073384
20 | * QQ 群: 开源项目使用交流,问题解答: 549284336(开源盛世)
21 |
22 |
23 | Copyright 2014 shizhefei(LuckyJayce)
24 |
25 | Licensed under the Apache License, Version 2.0 (the "License");
26 | you may not use this file except in compliance with the License.
27 | You may obtain a copy of the License at
28 |
29 | http://www.apache.org/licenses/LICENSE-2.0
30 |
31 | Unless required by applicable law or agreed to in writing, software
32 | distributed under the License is distributed on an "AS IS" BASIS,
33 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 | See the License for the specific language governing permissions and
35 | limitations under the License.
36 |
--------------------------------------------------------------------------------
/src/com/shizhefei/view/vary/IVaryViewHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2015 shizhefei(LuckyJayce)
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 | http://www.apache.org/licenses/LICENSE-2.0
7 | Unless required by applicable law or agreed to in writing, software
8 | distributed under the License is distributed on an "AS IS" BASIS,
9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 | See the License for the specific language governing permissions and
11 | limitations under the License.
12 | */
13 | package com.shizhefei.view.vary;
14 |
15 | import android.content.Context;
16 | import android.view.View;
17 |
18 | public interface IVaryViewHelper {
19 |
20 | public abstract View getCurrentLayout();
21 |
22 | public abstract void restoreView();
23 |
24 | public abstract void showLayout(View view);
25 |
26 | public abstract void showLayout(int layoutId);
27 |
28 | public abstract View inflate(int layoutId);
29 |
30 | public abstract Context getContext();
31 |
32 | public abstract View getView();
33 |
34 | }
--------------------------------------------------------------------------------
/res/layout/activity_example2.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
10 |
11 |
20 |
21 |
22 |
23 |
24 |
29 |
30 |
31 |
35 |
36 |
--------------------------------------------------------------------------------
/res/layout/activity_example1.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
10 |
11 |
16 |
17 |
22 |
23 |
28 |
29 |
30 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/src/com/shizhefei/test/SimpleActivity.java:
--------------------------------------------------------------------------------
1 | package com.shizhefei.test;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.view.View.OnClickListener;
7 | import android.widget.Toast;
8 |
9 | import com.shizhefei.test.load.LoadViewHelper;
10 | import com.shizhefei.view.loadview.R;
11 |
12 | public class SimpleActivity extends Activity {
13 |
14 | private LoadViewHelper helper;
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_simple);
20 | View contentLayout = findViewById(R.id.content_layout);
21 | helper = new LoadViewHelper(contentLayout);
22 | }
23 |
24 | public void showError(View view) {
25 | helper.showError("加载失败", "重试", new OnClickListener() {
26 |
27 | @Override
28 | public void onClick(View v) {
29 | Toast.makeText(getApplicationContext(), "点击了重试", Toast.LENGTH_SHORT).show();
30 | }
31 | });
32 | }
33 |
34 | public void showEmpty(View view) {
35 | helper.showError("暂无数据", "重试", new OnClickListener() {
36 |
37 | @Override
38 | public void onClick(View v) {
39 | Toast.makeText(getApplicationContext(), "点击了重试", Toast.LENGTH_SHORT).show();
40 | }
41 | });
42 | }
43 |
44 | public void showLoading(View view) {
45 | helper.showLoading("加载中...");
46 | }
47 |
48 | public void showSuccess(View view) {
49 | helper.restore();
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
28 |
29 |
32 |
33 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/src/com/shizhefei/test/Example2Activity.java:
--------------------------------------------------------------------------------
1 | package com.shizhefei.test;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.webkit.WebView;
7 | import android.webkit.WebViewClient;
8 | import android.widget.EditText;
9 |
10 | import com.shizhefei.test.load.LoadViewHelper;
11 | import com.shizhefei.view.loadview.R;
12 | import com.shizhefei.view.vary.VaryViewHelperX;
13 |
14 | public class Example2Activity extends Activity {
15 |
16 | private EditText editText;
17 | private WebView webView;
18 | private LoadViewHelper helper;
19 |
20 | @Override
21 | protected void onCreate(Bundle savedInstanceState) {
22 | super.onCreate(savedInstanceState);
23 | setContentView(R.layout.activity_example2);
24 | editText = (EditText) findViewById(R.id.editText1);
25 | webView = (WebView) findViewById(R.id.webView1);
26 | helper = new LoadViewHelper(new VaryViewHelperX(webView));
27 | webView.setWebViewClient(webViewClient);
28 | webView.loadUrl(editText.getText().toString());
29 | }
30 |
31 | public void clickLoad(View view) {
32 | webView.loadUrl(editText.getText().toString());
33 | }
34 |
35 | private WebViewClient webViewClient = new WebViewClient() {
36 | @Override
37 | public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
38 | super.onPageStarted(view, url, favicon);
39 | helper.showLoading("");
40 | };
41 |
42 | @Override
43 | public void onPageFinished(WebView view, String url) {
44 | super.onPageFinished(view, url);
45 | helper.restore();
46 | };
47 |
48 | @Override
49 | public boolean shouldOverrideUrlLoading(WebView view, String url) {
50 | view.loadUrl(url);
51 | return true;
52 | }
53 |
54 | };
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/res/layout/activity_simple.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
10 |
11 |
16 |
17 |
22 |
23 |
28 |
29 |
34 |
35 |
36 |
42 |
43 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/src/com/shizhefei/view/vary/VaryViewHelperX.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2015 shizhefei(LuckyJayce)
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 | http://www.apache.org/licenses/LICENSE-2.0
7 | Unless required by applicable law or agreed to in writing, software
8 | distributed under the License is distributed on an "AS IS" BASIS,
9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 | See the License for the specific language governing permissions and
11 | limitations under the License.
12 | */
13 | package com.shizhefei.view.vary;
14 |
15 | import android.content.Context;
16 | import android.view.View;
17 | import android.view.ViewGroup;
18 | import android.view.ViewGroup.LayoutParams;
19 | import android.widget.FrameLayout;
20 |
21 | /**
22 | * 用于切换布局,用一个新的布局覆盖在原布局之上
23 | *
24 | * @author LuckyJayce
25 | *
26 | */
27 | public class VaryViewHelperX implements IVaryViewHelper {
28 |
29 | private IVaryViewHelper helper;
30 | private View view;
31 |
32 | public VaryViewHelperX(View view) {
33 | super();
34 | this.view = view;
35 | ViewGroup group = (ViewGroup) view.getParent();
36 | LayoutParams layoutParams = view.getLayoutParams();
37 | FrameLayout frameLayout = new FrameLayout(view.getContext());
38 | group.removeView(view);
39 | group.addView(frameLayout, layoutParams);
40 |
41 | LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
42 | View floatView = new View(view.getContext());
43 | frameLayout.addView(view, params);
44 | frameLayout.addView(floatView, params);
45 | helper = new VaryViewHelper(floatView);
46 | }
47 |
48 | @Override
49 | public View getCurrentLayout() {
50 | return helper.getCurrentLayout();
51 | }
52 |
53 | @Override
54 | public void restoreView() {
55 | helper.restoreView();
56 | }
57 |
58 | @Override
59 | public void showLayout(View view) {
60 | helper.showLayout(view);
61 | }
62 |
63 | @Override
64 | public void showLayout(int layoutId) {
65 | showLayout(inflate(layoutId));
66 | }
67 |
68 | @Override
69 | public View inflate(int layoutId) {
70 | return helper.inflate(layoutId);
71 | }
72 |
73 | @Override
74 | public Context getContext() {
75 | return helper.getContext();
76 | }
77 |
78 | @Override
79 | public View getView() {
80 | return view;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/com/shizhefei/test/load/LoadViewHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2015 shizhefei(LuckyJayce)
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 | http://www.apache.org/licenses/LICENSE-2.0
7 | Unless required by applicable law or agreed to in writing, software
8 | distributed under the License is distributed on an "AS IS" BASIS,
9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 | See the License for the specific language governing permissions and
11 | limitations under the License.
12 | */
13 | package com.shizhefei.test.load;
14 |
15 | import com.shizhefei.view.loadview.R;
16 | import com.shizhefei.view.vary.IVaryViewHelper;
17 | import com.shizhefei.view.vary.VaryViewHelper;
18 |
19 | import android.view.View;
20 | import android.view.View.OnClickListener;
21 | import android.widget.Button;
22 | import android.widget.TextView;
23 |
24 | /**
25 | * 自定义要切换的布局,通过IVaryViewHelper实现真正的切换
26 | * 使用者可以根据自己的需求,使用自己定义的布局样式
27 | *
28 | * @author LuckyJayce
29 | *
30 | */
31 | public class LoadViewHelper {
32 |
33 | private IVaryViewHelper helper;
34 |
35 | public LoadViewHelper(View view) {
36 | this(new VaryViewHelper(view));
37 | }
38 |
39 | public LoadViewHelper(IVaryViewHelper helper) {
40 | super();
41 | this.helper = helper;
42 | }
43 |
44 | public void showError(String errorText, String buttonText, OnClickListener onClickListener) {
45 | View layout = helper.inflate(R.layout.load_error);
46 | TextView textView = (TextView) layout.findViewById(R.id.textView1);
47 | textView.setText(errorText);
48 | Button button = (Button) layout.findViewById(R.id.button1);
49 | button.setText(buttonText);
50 | button.setOnClickListener(onClickListener);
51 | helper.showLayout(layout);
52 | }
53 |
54 | public void showEmpty(String errorText, String buttonText, OnClickListener onClickListener) {
55 | View layout = helper.inflate(R.layout.load_empty);
56 | TextView textView = (TextView) layout.findViewById(R.id.textView1);
57 | textView.setText(errorText);
58 | Button button = (Button) layout.findViewById(R.id.button1);
59 | button.setText(buttonText);
60 | button.setOnClickListener(onClickListener);
61 | helper.showLayout(layout);
62 | }
63 |
64 | public void showLoading(String loadText) {
65 | View layout = helper.inflate(R.layout.load_ing);
66 | TextView textView = (TextView) layout.findViewById(R.id.textView1);
67 | textView.setText(loadText);
68 | helper.showLayout(layout);
69 | }
70 |
71 | public void restore() {
72 | helper.restoreView();
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/com/shizhefei/view/vary/VaryViewHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2015 shizhefei(LuckyJayce)
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 | http://www.apache.org/licenses/LICENSE-2.0
7 | Unless required by applicable law or agreed to in writing, software
8 | distributed under the License is distributed on an "AS IS" BASIS,
9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 | See the License for the specific language governing permissions and
11 | limitations under the License.
12 | */
13 | package com.shizhefei.view.vary;
14 |
15 | import android.content.Context;
16 | import android.view.LayoutInflater;
17 | import android.view.View;
18 | import android.view.ViewGroup;
19 |
20 | /**
21 | * 用于切换布局,用一个新的布局替换掉原先的布局
22 | *
23 | * @author LuckyJayce
24 | *
25 | */
26 | public class VaryViewHelper implements IVaryViewHelper {
27 | private View view;
28 | private ViewGroup parentView;
29 | private int viewIndex;
30 | private ViewGroup.LayoutParams params;
31 | private View currentView;
32 |
33 | public VaryViewHelper(View view) {
34 | super();
35 | this.view = view;
36 | }
37 |
38 | private void init() {
39 | params = view.getLayoutParams();
40 | if (view.getParent() != null) {
41 | parentView = (ViewGroup) view.getParent();
42 | } else {
43 | parentView = (ViewGroup) view.getRootView().findViewById(android.R.id.content);
44 | }
45 | int count = parentView.getChildCount();
46 | for (int index = 0; index < count; index++) {
47 | if (view == parentView.getChildAt(index)) {
48 | viewIndex = index;
49 | break;
50 | }
51 | }
52 | currentView = view;
53 | }
54 |
55 | @Override
56 | public View getCurrentLayout() {
57 | return currentView;
58 | }
59 |
60 | @Override
61 | public void restoreView() {
62 | showLayout(view);
63 | }
64 |
65 | @Override
66 | public void showLayout(View view) {
67 | if (parentView == null) {
68 | init();
69 | }
70 | this.currentView = view;
71 | // 如果已经是那个view,那就不需要再进行替换操作了
72 | if (parentView.getChildAt(viewIndex) != view) {
73 | ViewGroup parent = (ViewGroup) view.getParent();
74 | if (parent != null) {
75 | parent.removeView(view);
76 | }
77 | parentView.removeViewAt(viewIndex);
78 | parentView.addView(view, viewIndex, params);
79 | }
80 | }
81 |
82 | @Override
83 | public void showLayout(int layoutId) {
84 | showLayout(inflate(layoutId));
85 | }
86 |
87 | @Override
88 | public View inflate(int layoutId) {
89 | return LayoutInflater.from(view.getContext()).inflate(layoutId, null);
90 | }
91 |
92 | @Override
93 | public Context getContext() {
94 | return view.getContext();
95 | }
96 |
97 | @Override
98 | public View getView() {
99 | return view;
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/src/com/shizhefei/test/Example1Activity.java:
--------------------------------------------------------------------------------
1 | package com.shizhefei.test;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Random;
6 |
7 | import android.app.Activity;
8 | import android.os.AsyncTask;
9 | import android.os.Bundle;
10 | import android.view.View;
11 | import android.view.View.OnClickListener;
12 | import android.widget.ArrayAdapter;
13 | import android.widget.ListView;
14 |
15 | import com.shizhefei.test.load.LoadViewHelper;
16 | import com.shizhefei.view.loadview.R;
17 |
18 | public class Example1Activity extends Activity {
19 |
20 | private ListView listView;
21 | private LoadDataTask task;
22 | private LoadViewHelper helper;
23 | private ArrayAdapter adapter;
24 | private List data = new ArrayList();
25 |
26 | @Override
27 | protected void onCreate(Bundle savedInstanceState) {
28 | super.onCreate(savedInstanceState);
29 | setContentView(R.layout.activity_example1);
30 | listView =(ListView) findViewById(R.id.listView1);
31 | helper = new LoadViewHelper(listView);
32 | listView.setAdapter(adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data));
33 | task = new LoadDataTask(3);
34 | task.execute();
35 | }
36 |
37 | @Override
38 | protected void onDestroy() {
39 | super.onDestroy();
40 | if (task != null) {
41 | task.cancel(true);
42 | }
43 | }
44 |
45 | public void showLoadError(View view) {
46 | if (task != null) {
47 | task.cancel(true);
48 | }
49 | task = new LoadDataTask(1);
50 | task.execute();
51 | }
52 |
53 | public void showLoadEmpty(View view) {
54 | if (task != null) {
55 | task.cancel(true);
56 | }
57 | task = new LoadDataTask(2);
58 | task.execute();
59 | }
60 |
61 | public void showLoadSuccess(View view) {
62 | if (task != null) {
63 | task.cancel(true);
64 | }
65 | task = new LoadDataTask(3);
66 | task.execute();
67 | }
68 |
69 | private class LoadDataTask extends AsyncTask> {
70 | private int type;
71 |
72 | public LoadDataTask(int type) {
73 | super();
74 | this.type = type;
75 | }
76 |
77 | @Override
78 | protected void onPreExecute() {
79 | super.onPreExecute();
80 | helper.showLoading("加载中...");
81 | }
82 |
83 | @Override
84 | protected List doInBackground(Void... params) {
85 | try {
86 | // 模拟2秒到5秒的等待时间
87 | Thread.sleep((new Random().nextInt(30) + 20) * 100);
88 | } catch (InterruptedException e) {
89 | e.printStackTrace();
90 | }
91 | if (type == 1) {
92 | return null;
93 | } else if (type == 2) {
94 | return new ArrayList(0);
95 | }
96 | List strings = new ArrayList();
97 | for (int i = 0; i < 20; i++) {
98 | strings.add("数据" + i);
99 | }
100 | return strings;
101 | }
102 |
103 | @Override
104 | protected void onPostExecute(List result) {
105 | super.onPostExecute(result);
106 | if (result == null) {
107 | helper.showError("加载失败", "重试", new OnClickListener() {
108 |
109 | @Override
110 | public void onClick(View v) {
111 | task = new LoadDataTask(1);
112 | task.execute();
113 | }
114 | });
115 | } else if (result.isEmpty()) {
116 | helper.showEmpty("暂无数据", "重试", new OnClickListener() {
117 |
118 | @Override
119 | public void onClick(View v) {
120 | task = new LoadDataTask(2);
121 | task.execute();
122 | }
123 | });
124 | } else {
125 | data.clear();
126 | data.addAll(result);
127 | helper.restore();
128 | adapter.notifyDataSetChanged();
129 | }
130 | }
131 |
132 | }
133 | }
134 |
--------------------------------------------------------------------------------