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

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


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

在Android的开发中,往往有这种需求,比如一个耗时的操作,联网获取网络图片、内容,数据库耗时读写等等,在此耗时操作过程中,开发者也许不希望用户再进行其他操作(其他操作可能会引起逻辑混乱),而此时需要给用户一个额外的加载页面遮挡住主逻辑代码的运行,待主页面的耗时操作完成后,自动消失这样加载过度页面,恢复出正常应该显示的页面。
举个实际的例子,如代码使用Android WebView打开一个网页链接试图加载某个网站,但网络质量不佳,需要耗时很久,那么在这个过程中,较好的用户体验做法是:给用户一个加载进度页面,遮挡住WebView。当加载的内容成功后在完全切换回正常的逻辑页面。
Android AndroidProgressLayout实现了这样的功能,Android AndroidProgressLayout在github上的项目主页是:https://github.com/antonkrasov/AndroidProgressLayout
AndroidProgressLayout要实现的功能如图:


现在给出一个例子加以说明。

使用AndroidProgressLayout,需要先写一个布局:

<com.github.androidprogresslayout.ProgressLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:progressLayout="http://schemas.android.com/apk/res-auto"
    android:id="@+id/progress_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    progressLayout:progressBackground="#4fc3f7" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />

</com.github.androidprogresslayout.ProgressLayout>


Java代码:

package zhangphil.demo;

import com.github.androidprogresslayout.ProgressLayout;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final ProgressLayout progressLayout = (ProgressLayout) findViewById(R.id.progress_layout);
		
		final TextView text = (TextView) findViewById(R.id.text);

		Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				text.setText("加载完毕!");

				// 切换回正常显示页面
				progressLayout.showContent();
			}
		};
		
		// 开始加载... 假设从这里开始一个耗时的操作将开始启动,在此启动过程中,开发者希望用户稍事休息,等待。。。
		progressLayout.showProgress();

		// 假设有一个耗时的加载业务逻辑,需要5秒完成。
		handler.sendEmptyMessageDelayed(0, 5000);
	}
}


相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发之捕获屏幕的变更事件实战(包括竖屏与横屏切换,回到桌面与切换到任务列表)
Android Studio App开发之捕获屏幕的变更事件实战(包括竖屏与横屏切换,回到桌面与切换到任务列表)
38 0
|
7月前
|
Shell Android开发 容器
你真了解Android任务栈 Task 与启动模式吗?
你真了解Android任务栈 Task 与启动模式吗?
59 0
|
8月前
|
Android开发
Android 使用ViewPager实现手动左右切换页面和底部点点跟随切换效果
Android 使用ViewPager实现手动左右切换页面和底部点点跟随切换效果
134 0
|
6月前
|
API Android开发 数据安全/隐私保护
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
229 0
|
4月前
|
Android开发
UniApp Android 页面拖动,去掉半圆形阴影
UniApp Android 页面拖动,去掉半圆形阴影
43 0
|
8月前
|
数据库 Android开发 数据库管理
Android使用Room操作SQLite数据库让其变得无比高效和简洁(进一步完善用RecyclerView显示数据库中的数据)
Android使用Room操作SQLite数据库让其变得无比高效和简洁(进一步完善用RecyclerView显示数据库中的数据)
52 0
|
4月前
|
Android开发
[Android]视图的控触操作-MotionEvent
[Android]视图的控触操作-MotionEvent
31 0
|
4月前
|
XML Java 调度
Android App网络通信中通过runOnUiThread快速操纵界面以及利用线程池Executor调度异步任务实战(附源码 简单易懂)
Android App网络通信中通过runOnUiThread快速操纵界面以及利用线程池Executor调度异步任务实战(附源码 简单易懂)
30 0
|
4月前
|
算法 Java 数据安全/隐私保护
Android App开发之利用JNI实现加密和解密操作实战(附源码 简单易懂)
Android App开发之利用JNI实现加密和解密操作实战(附源码 简单易懂)
69 0
|
4月前
|
XML Java Android开发
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
55 0