星空网 > 软件开发 > 操作系统

加载页面遮挡耗时操作任务页面

加载页面遮挡耗时操作任务页面images/loading.gif' data-original="http://images2015.cnblogs.com/blog/822717/201511/822717-20151127164216968-701451219.gif" />

在Android的开发中,往往有这种需求,比如一个耗时的操作,联网获取网络图片、内容,数据库耗时读写等等,在此耗时操作过程中,开发者也许不希望用户再进行其他操作(其他操作可能会引起逻辑混乱),而此时需要给用户一个额外的加载页面遮挡住主逻辑代码的运行,待主页面的耗时操作完成后,自动消失这样加载过度页面,恢复出正常应该显示的页面。

举个实际的例子,如代码使用Android WebView打开一个网页链接试图加载某个网站,但网络质量不佳,需要耗时很久,那么在这个过程中,较好的用户体验做法是:给用户一个加载进度页面,遮挡住WebView。当加载的内容成功后在完全切换回正常的逻辑页面。
Android AndroidProgressLayout实现了这样的功能,Android AndroidProgressLayout在github上的项目主页是:https://github.com/antonkrasov/AndroidProgressLayout 

测试代码如下:

activity_main.

 1 <com.github.androidprogresslayout.ProgressLayout ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/apk/res-auto" 3   android:id="@+id/progressLayout" 4   android:layout_width="match_parent" 5   android:layout_height="match_parent" > 6  7   <TextView 8     android:id="@+id/textView" 9     android:layout_width="wrap_content"10     android:layout_height="wrap_content"11     android:textSize="40sp"12     android:layout_centerInParent="true" />13 14 </com.github.androidprogresslayout.ProgressLayout>

MainActivity.java:

 1 package com.zzw.testandroidprogresslayout; 2  3 import com.github.androidprogresslayout.ProgressLayout; 4  5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.os.Handler; 8 import android.os.Message; 9 import android.view.animation.Animation;10 import android.widget.TextView;11 12 public class MainActivity extends Activity {13 14   @Override15   protected void onCreate(Bundle savedInstanceState) {16     super.onCreate(savedInstanceState);17     setContentView(R.layout.activity_main);18 19     final ProgressLayout progressLayout = (ProgressLayout) findViewById(R.id.progressLayout);20     final TextView textView = (TextView) findViewById(R.id.textView);21 22     Handler handler = new Handler() {23       @Override24       public void handleMessage(Message msg) {25         textView.setText("测试完成");26 27         // 切换回正常显示页面28         progressLayout.showContent();29       }30     };31     // 开始加载... 假设从这里开始一个耗时的操作将开始启动,在此启动过程中,开发者希望用户稍事休息,等待。。。32     progressLayout.showProgress();33 34     // 假设有一个耗时的加载业务逻辑,需要5秒完成。35     handler.sendEmptyMessageDelayed(0, 5000);36   }37 }

 




原标题:加载页面遮挡耗时操作任务页面

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流