AndroidAndroid常用框架结构

多状态加载框架EasyLoad(AndroidX+Kotlin)

2020-08-25  本文已影响0人  言吾許

EasyLoad

作者 言吾
简书 Github

EasyLoad是一个基于Koltin、专注于AndroidX简单易用的页面状态加载框架,支持ActivityFragmentView
对布局文件实现零侵入。

使用EasyLoad

添加依赖

implementation 'com.xu.easyload:easyload:0.0.1'

一、全局配置

全局配置只能初始化一次,多次配置会报错。

class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        //初始化方式①,
        EasyLoad.initGlobal()
                .addGlobalState(ErrorState())//添加错误布局状态
                .addGlobalState(EmptyState())//添加空布局状态
                .addGlobalState(LoadingState())//添加加载布局状态
                .setGlobalDefaultState(LoadingState::class.java)//设置默认全局

        //初始化方式②,两种只能选一种,不能初始化两次,否则报错
        initEasyLoad {
            addGlobalState(ErrorState())
            addGlobalState(EmptyState())
            addGlobalState(LoadingState())
            addGlobalState(NoInternetState())
            setGlobalDefaultState(LoadingState::class.java)
        }
        
    }
}

二、自定义状态类

class EmptyState : BaseState() {
    /**
     * 设置布局
     */
    override fun onCreateView(): Int {
        return R.layout.view_easy_load_empty
    }
    /**
    *是否允许重新加载 
    */
    override fun canReloadable(): Boolean {
        return true
    }

}

三、页面配置

最简单的注入到Activity

  val service = EasyLoad.initLocal()
                .inject(this)
//或更简单通过扩展函数的方式注入
 val service =inject(this)
//展示状态
 service.showState(xxxState::class.java)
 service.showSuccess()

完整用法

        val service = EasyLoad.initLocal()
                //添加LocalState,只在本target中生效,其他target调用会报错
                .addLocalState(PlaceHolderState())
                //是否展示默认
                .showDefault(true)
                //LocalDefault会覆盖GlobalDefault
                .setLocalDefaultState(PlaceHolderState::class.java)
                //设置重新加载监听方式①
                //.setOnReloadListener { iLoadService, clickState, view ->
                //do something
                //}
                .inject(this) {
                    //设置重新加载监听方式②
                    setOnReloadListener { iLoadService, clickState, view ->
                        when (clickState) {
                            is PlaceHolderState ->
                                Log.d("TAG","PlaceHolderState")
                            is ErrorState ->
                                Log.d("TAG","ErrorState")
                            is SuccessState ->
                                Log.d("TAG","SuccessState")
                        }

                        //可以在子线程中使用
                        Thread(Runnable {
                            iLoadService.showState(PlaceHolderState::class.java)
                            SystemClock.sleep(4000)
                            iLoadService.showState(SuccessState::class.java)
                        }).start()
                    }
                    //设置状态变更监听
                    setOnStateChangeListener { view, currentState ->
                        when (currentState) {
                            is PlaceHolderState ->
                                Log.d(tag, "PlaceHolderState")
                            is ErrorState ->
                                Log.d(tag, "ErrorState")
                            is SuccessState ->
                                Log.d(tag, "SuccessState")
                        }
                    }
                }

注入到Fragment

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_a, container, false)
        service = EasyLoad.initLocal()
                .addLocalState(PlaceHolderState())
                .inject(view) {
                    setOnReloadListener { iLoadService, clickState, view ->
                        iLoadService.showState(PlaceHolderState::class.java)
                        Handler().postDelayed({
                            iLoadService.showState(SuccessState::class.java)
                        }, 3000)
                    }
                }
        return service.getParentView()
    }

注入到View

        val sView = EasyLoad.initLocal()
                .addLocalState(LoadingState2())
                .setLocalDefaultState(LoadingState2::class.java)
                //ConstraintLayout 2.0版本以上,会出现不显示的问题,可以通过设置specialSupport为true来支持,会损失性能
                //.specialSupport(true)
                .inject(cl_child)

        //扩展函数的方式注入
        val service= inject(xxView) {
            addLocalState(LoadingState2())
            setLocalDefaultState(LoadingState2::class.java)
            specialSupport(true)
            setOnReloadListener { iLoadService, clickState, view ->

            }
        }

Github传送门

有什么好的建议或bug,欢迎提issue~

别人都在过七夕,我在写代码,嘤嘤嘤~2020.08.25

上一篇下一篇

猜你喜欢

热点阅读