Gif-Load-ReTry-Refresh

Introduction: 支持 gif 图片的 Load 反馈框架,只需一张 gif 图,一行代码解决初次加载,重试加载,刷新加载,与生命周期绑定,LeakCanary 检测无内存泄漏
More: Author   ReportBugs   
Tags:

View-Load-ReTry:这个加载框架有点不一样,针对 View 进行加载,加载页面还保持了原 View 的属性,侧重点在灵活,哪里需要加载哪里

  • 原理 :找到需要加载的 View,放入 FrameLayout(包含自定义的各种情况的加载反馈 View),再把 FrameLayout 放回需要加载 View 的 Parent 中 ,然后根据需求调用显示加载或者异常 View。
  • 功能 :只要当前需要加载 View 有 Parent 就可以实现加载反馈(仅不支持复用型的 View 场景),同一页面支持 N 个 View 的加载,彼此互不影响。
  • 封装 :全局配置封装,与业务解耦,一个入口控制全部的加载反馈页面。

示例,Demo 只添加了一种 Error 反馈页面做演示,使用时可按需自定义多种 Error 反馈页面(Gif 中的 View 残留现象是录制时丢帧问题,嘿嘿)

常规使用 Activity+Fragment 多 View 加载
normal fix


使用

初步配置

引入

Step 1. Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
        implementation 'com.github.NoEndToLF:View-Load-ReTry:2.0.2'
}

自定义加载状态页面 Adapter,继承 BaseLoadRetryAdapter,下面各方法都是按需被调用,取决于你主动设置的显示哪个 Adapter。

方法 参数 作用
onLoadStart View 显示这个加载状态页面前开始前调用,用于你自定义页面中控件的初始化,此 View 为当前显示的加载页面 View,以下方法中的 View 都是。
getCoverViewLayoutId return R.layout 加载页面的布局 Layout
onFalied View,Ogject 加载失败的回调(会在你主动调用错误页面对应的那个 Adapter 里调用),Object 可以是任意的对象,方便你显示加载错误的原因
onSuccess View 加载成功的回调(会在你设置的 Load 状态那个 Adapter 里调用),这里可以做一些加载动画的停止操作,另需要手动让 View.Gone,暴露在这里是方便各位添加加载页面消失的动画

自定义 Adapter 示例(Demo 中 normal 用法)

public class LoadAdapterForActivity extends BaseLoadRetryAdapter{
    @Override
    public void onLoadStart(View view) {
        ((TextView)view.findViewById(R.id.tv_text)).setText("加载中 ...");
    }
    @Override
    public void onFalied(View view, Object object) {
    }
/**这里在加载完成的时候做了一个淡出动画*/
    @Override
    public void onSuccess(View view) {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
        alphaAnimation.setDuration(500);
        view.startAnimation(alphaAnimation);
        view.setVisibility(View.GONE);
    }

    @Override
    public int getCoverViewLayoutId() {
        return R.layout.load_activity;
    }
}
/**默认给加载失败页面添加了点击事件,回调到 retry 方法,开始重试,重试时显示加载中 Adapter 对应的页面*/
public class NetErrorAdapterForActivity extends BaseLoadRetryAdapter{
    @Override
    public void onLoadStart(View view) {
        ((TextView)view.findViewById(R.id.tv_retry)).setText("点击重新加载");
    }

    @Override
    public void onFalied(View view, Object object) {
        ((ImageView) view.findViewById(R.id.iv_head)).setImageResource(R.mipmap.timeout);
        ((TextView)view.findViewById(R.id.tv_text)).setText((String)object);
        ((TextView)view.findViewById(R.id.tv_retry)).setVisibility(View.VISIBLE);
    }

    @Override
    public void onSuccess(View view) {

    }

    @Override
    public int getCoverViewLayoutId() {
        return R.layout.retry_activity;
    }
}

示例代码(Demo 中 normal 用法):建议在 Application 的 onCreate 中进行初始化,有多少个 Adapter 就添加多少个,这里统一了入口是方便管理。

LoadRetryManager.getInstance().addAdapter(new LoadAdapterForActivity());
LoadRetryManager.getInstance().addAdapter(new NetErrorAdapterForActivity());.......

最常规用法,针对一个 View(针对多个 View 和针对一个 View 用法一致,流程都是针对某个 View 的)

方法 参数 作用
register View,LoadRetryRefreshListener 注册
startLoad View,Class<? extends BaseLoadRetryAdapter> 开始加载,加载中类 Adapter.Calss
unRegister View 解除绑定
unRegister List 解除多个 View 的绑定
onSuccess View 加载成功,会调用 View 对应加载中 Adapter 的 onSuccess 方法
onFailed View,Class<? extends BaseLoadRetryAdapter>,Object 加载失败时显示加载失败类 Adapter.Class 对应的页面,并调用该 Adapter 的 onFalied()方法,Object 为加载失败原因

1、注册,一般在 onCreate 中调用

LoadRetryManager.getInstance().register(view, new LoadRetryListener() {
            @Override
            public void load() {
                //执行你的网络请求
               //dosomething();
            }

            @Override
            public void reTry() {
                //执行你的重试请求
                //dosomethingRetry();
            }
        });

2、开始加载

LoadRetryManager.getInstance().load(view, LoadAdapterForActivity.class);

3、加载结果回调,在你的请求成功和失败的回调中加入加载结果回调

            @Override
            public void onSuccess(Integer value) {
            //加载成功你要做的事.....

                //加载结果回调
                LoadRetryManager.getInstance().onSuccess(view);
            }

            @Override
            public void onFailed(Throwable e) {
            //加载失败你要做的事.....

                //加载结果回调
             LoadRetryManager.getInstance().onFailed(view, NetErrorAdapterForActivity.class, "请检查网络连接");

            }

4、解除绑定

//在 Activity 中
Override
    protected void onDestroy() {
        super.onDestroy();
        LoadRetryManager.getInstance().unRegister(view);

    //加载多个 View 时建议在 BaseActivity 中封装方法,维护一个 List<View>,每注册一个加载 View 就 Add 一次,解绑时方便操作
        LoadRetryManager.getInstance().unRegister(list); 
    }
//在 Fragment 中
@Override
    public void onDestroyView() {
        super.onDestroyView();
        LoadRetryManager.getInstance().unRegister(view);

    //加载多个 View 时建议在 BaseFragment 中封装方法,维护一个 List<View>,每注册一个加载 View 就 Add 一次,解绑时方便操作
        LoadRetryManager.getInstance().unRegister(list); 
    }

为何要造这个看起来重复的轮子

目前好多开源的加载反馈框架大多是针对 Activity 和 Fragment 的,原理都是从根上替换加载布局,但是有个缺点,反馈布局的作用域太大了,不够灵活,现在闲的蛋疼造这个轮子也是为了灵活性,比如说 Sample 中的同一个页面要加载 3 块内容的时候,这个轮子的优势就显示出来了,而且原 View 具有的基本特性加载反馈页面依然包含。

反馈与建议

License

Copyright (c) [2018] [static]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools