这个项目是根据 StateLayout 项目而来,感谢原作者的贡献,我本人在功能实现上只做了稍许改动,主要的不同在于这个是完全使用Kotlin编写的,可以看做是一个kotlin练手项目。 由于我也是刚刚使用Kotlin,使用还不是很熟练,如果在写法、功能实现上有不够优雅的地方欢迎指正!
- 优化了各个View的加载逻辑,在调用相应布局时才会加载,节约资源。
- 删除了部分无用的方法
下面是原项目的readme部分
用法与ScrollView一致,只允许一个根布局
<com.fingdo.statelayout.StateLayout android:id="@+id/state_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 内容布局 one root view --> </com.fingdo.statelayout.StateLayout><declare-styleable name="StateLayout"> <!-- 错误提示图标 --> <attr name="errorImg" format="reference" /> <!-- 错误提示文字 --> <attr name="errorText" format="string" /> <!-- 空数据提示图标 --> <attr name="emptyImg" format="reference" /> <!-- 空数据提示文字 --> <attr name="emptyText" format="string" /> <!-- 没有网络提示图标 --> <attr name="noNetworkImg" format="reference" /> <!-- 没有网络提示文字 --> <attr name="noNetworkText" format="string" /> <!-- 超时提示图标 --> <attr name="timeOutImg" format="reference" /> <!-- 超时提示文字 --> <attr name="timeOutText" format="string" /> <!-- 登录提示图标 --> <attr name="loginImg" format="reference" /> <!-- 登录提示文字 --> <attr name="loginText" format="string" /> <!-- 加载提示文字 --> <attr name="loadingText" format="string" /> </declare-styleable><com.fingdo.statelayout.StateLayout xmlns:sl="http://schemas.android.com/apk/res-auto" android:id="@+id/state_layout" android:layout_width="match_parent" android:layout_height="match_parent" sl:emptyImg="@drawable/ic_state_empty" sl:emptyText="空数据提示文字" sl:errorImg="@drawable/ic_state_error" sl:errorText="错误提示文字" sl:loadingText="加载提示文字" sl:loginImg="@drawable/ic_state_login" sl:loginText="登录提示文字" sl:noNetworkImg="@drawable/ic_state_no_network" sl:noNetworkText="没有网络提示文字" sl:timeOutImg="@drawable/ic_state_time_out" sl:timeOutText="超时提示文字"> </com.fingdo.statelayout.StateLayout>//type为StateLayout的固定Type变量 public static final int ERROR = 1; public static final int EMPTY = 2; public static final int TIMEOUT = 3; public static final int NOT_NETWORK = 4; public static final int LOADING = 5; public static final int LOGIN = 6;//展示没有网络的界面 stateLayout.showNoNetworkView(); //展示超时的界面 stateLayout.showTimeoutView(); //展示空数据的界面 stateLayout.showEmptyView(); //展示错误的界面 stateLayout.showErrorView(); //展示登录的界面 stateLayout.showLoginView(); //如下图所示 1,直接显示 2,设置提示stringId和图片Id显示 3,设置提示stringId显示 4,设置提示字符串现实 5,设置提示字符串和图片Id显示//显示加载界面 stateLayout.showLoadingView(); 1,直接显示 2,设置提示stringId显示 3,设置提示字符串显示 4,设置自定义加载View显示,如: 1)进度条 2)显示gif的View 3)自定义布局View //显示自定义界面 stateLayout.showCustomView();动画默认为false,如果需要开启动画,请调用
//开启动画 stateLayout.setUseAnimation(true);如果用户不设置自定义动画,一般为默认的渐隐缩放动画 如果用户需要设置动画,请调用
//设置动画 stateLayout.setViewSwitchAnimProvider(new FadeScaleViewAnimProvider());stateLayout自定义了两种动画
//渐隐缩放,渐显放大动画 FadeScaleViewAnimProvider //渐隐渐显动画 FadeViewAnimProvider用户如需自定义动画样式,请实现ViewAnimProvider接口
重写showAnimation和hideAnimation方法。
//以FadeViewAnimProvider为例 public class FadeViewAnimProvider implements ViewAnimProvider { @Override public Animation showAnimation() { Animation animation = new AlphaAnimation(0.0f,1.0f); animation.setDuration(200); animation.setInterpolator(new DecelerateInterpolator()); return animation; } @Override public Animation hideAnimation() { Animation animation = new AlphaAnimation(1.0f,0.0f); animation.setDuration(200); animation.setInterpolator(new AccelerateDecelerateInterpolator()); return animation; } }请实现StateLayout里面的OnViewRefreshListener接口。
重写两个方法:
//刷新界面 void refreshClick(); //登录点击 void loginClick();感谢lufficc 开源代码提供的动画思路




