前言: 以前SmartLoadingView1.0的时候,我也发了一篇掘金。我把本身自定这个View的过程和细节及动画进行了讲解。反应还不错。可是虽然博客反应好,可是github的star量却少的可怜。我一直没有发现缘由。直到最近我在重构MVVMdemo的时候,用了下本身的控件,虽然没有bug。可是api很是很差用,包括方法接口,太长。一个简单的功能,一大段代码。因而做者用了1周的时间,进行翻天覆地的改变。不但更新了api并且还增长了不少功能。那么接下来简单介绍下怎么用吧。java
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
复制代码
dependencies {
implementation 'com.github.lihangleo2:SmartLoadingView:2.0.1'
}
复制代码
<com.lihang.smartloadview.SmartLoadingView android:id="@+id/smartLoadingView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自带dialog按钮" android:textColor="#fff" android:textSize="15dp" app:background_normal="#fff" app:errorMsg="联网失败文案" app:background_error="#ED7676" app:background_cannotClick="#bbbbbb" app:cornerRaius="30dp" app:textScrollMode="marquee" app:smart_clickable="true" app:speed="400" />
复制代码
1.一、动画结束后自动跳转 | 1.二、本身监听动画实现逻辑 |
---|---|
![]() |
![]() |
这里点击事件和普通的控件点击事件一致。设置setOnClickListener()便可。android
smartLoadingView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
smartLoadingView.start();
Observable.timer(2000, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread()).subscribe(along -> {
smartLoadingView.onSuccess(MainActivity.this, SecondActivity.class);
});
}
});
复制代码
点击按钮,联网开始时,启动动画git
smartLoadingView.start();
复制代码
这里我用了RxJava延迟了2s 模拟联网成功。你也能够用handler延迟实现这个功能,这里用了下lambda表达式,能够忽略,只要看下面代码。github
//这样既可实现,从一个页面转场动画跳转到另一个页面(注意这样跳转,第一个页面会被finish掉)。
smartLoadingView.onSuccess(MainActivity.this, SecondActivity.class);
复制代码
前面点击事件和启动动画和 1.1 都同样,不一样的时,联网成功的时候,增长动画结束的监听(这里逻辑都是本身处理,不会关闭和跳转任何页面)api
smartLoadingView.onSuccess(new SmartLoadingView.AnimationFullScreenListener() {
@Override
public void animationFullScreenFinish() {
Toast.makeText(MainActivity.this, "监听动画结束", Toast.LENGTH_SHORT).show();
}
});
复制代码
2.一、请求失败,文案显示在控件上 | 2.二、请求失败,回到初始状态 |
---|---|
![]() |
![]() |
这里点击和启动动画都和上面一致。若是你xml里设置了失败文案的话,联网失败时,只须要调用app
smartLoadingView.netFaile();
复制代码
固然,若是你再联网前并不知道失败文案也能够这样,带入当前失败的文案maven
smartLoadingView.netFaile(msg);
复制代码
若是大家的需求是,失败文案显示在别的地方,或者只是弹一个toast,按钮箱回到初始状态,只须要这样ide
smartLoadingView.backToStart();
复制代码
3.一、正常的联网,正常出结果 | 3.二、正常联网,打勾出结果 |
---|---|
![]() |
![]() |
3.三、打勾出结果,打勾消失 | 3.四、打勾出结果,提醒用户 |
![]() |
![]() |
这里点击事件和启动动画都和以前同样。正常出结果,只须要结合失败的方法去使用,失败文案,失败背景设置成关注成功的样式,调用只须要这样post
smartLoadingView.netFaile("关注成功");
复制代码
再次点击后,回到初始状态。注意这里不能使用backToStart()。由于backToStart()是出结果时使用,即便你使用也不起效果,这里已经出告终果“关注成功”。因此此时,再次点击,须要以下学习
smartLoadingView.reset();
复制代码
前面都是同样的,只是出结果时,实现AnimationOKListener接口
smartLoadingView.onSuccess(new SmartLoadingView.AnimationOKListener() {
@Override
public void animationOKFinish() {
Toast.makeText(MainActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
}
});
复制代码
若是想实现抖音那样,打勾后,打勾消失,只须要实现,添加一个模式就行了,OKAnimationType.HIDE。(固然上面就是默认的OKAnimationType.NORMAL)
smartLoadingView.onSuccess(new SmartLoadingView.AnimationOKListener() {
@Override
public void animationOKFinish() {
Toast.makeText(MainActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
}
}, SmartLoadingView.OKAnimationType.HIDE);
复制代码
这个就有点相似提醒效果,无论你的控件在屏幕上的任何位置,最终都会运行到屏幕中间,提醒用户,成功了。也只需添加一个模式OKAnimationType.TRANSLATION_CENTER
smartLoadingView.onSuccess(new SmartLoadingView.AnimationOKListener() {
@Override
public void animationOKFinish() {
Toast.makeText(MainActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
}
}, SmartLoadingView.OKAnimationType.TRANSLATION_CENTER);
复制代码
设计这个的初衷是由于,可能按钮的错误文案太长了,按钮放不下时使用的
4.一、文字来回滚动 | 4.二、仿跑马灯滚动 |
---|---|
![]() |
![]() |
只须要在xml里加上 app:textScrollMode="normal"。或者能够不加,由于默认滚动就是这种方式
xml里只须要加上 app:textScrollMode="marquee"
5.一、设置不可点击状态 | |
---|---|
![]() |
![]() |
在xml里能够经过app:smart_clickable="false"进行设置。固然也能经过代码来设置
smartLoadingView.setSmartClickable(false);
复制代码
6.一、登陆demo | 6.二、关注demo |
---|---|
![]() |
![]() |
能够下载demo自行查看
长宽都是用系统的layout_width和layout_height,包括设置padding。若是不设置,是有默认间距的
Android工做多年了,一直向往大厂。在前进的道路上是孤独的。若是你在学习的路上也感受孤独,请和我一块儿。让咱们在学习道路上少些孤独