Skip to content

🔥🔥🔥 - 一个超赞的页面状态切换库(加载中,空布局,网络错误,重试,自定义类型)

License

Notifications You must be signed in to change notification settings

simplepeng/NiceStateView

Repository files navigation

MIT Androidx Api Kotlin

一个超赞的页面状态切换库(加载中,空布局,网络错误,重试,自定义类型)

Loading Empty Error Retry

导入依赖

maven { url 'https://jitpack.io' }
implementation 'com.github.simplepeng:NiceStateView:v1.0.6'

使用默认样式

  1. 在代码中使用
//初始化注册不同的状态布局
private val niceStateView: NiceStateView by lazy {
    NiceStateView.newBuilder()
        .registerLoading(R.layout.sample_loading_view)
        .registerEmpty(R.layout.sample_empty_view)
        .registerError(R.layout.sample_error_view)
        .registerRetry(R.layout.sample_retry_view)
        .registerCustom("login", R.layout.layout_login)
        .wrapContent(view_content)
}
  1. 在xml中使用
<me.simple.nsv.view.NiceStateLayout
    android:id="@+id/stateLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    
    app:empty_layout_res="@layout/sample_empty_view"
    app:error_layout_res="@layout/sample_error_view"
    app:loading_layout_res="@layout/sample_loading_view"
    app:retry_layout_res="@layout/sample_retry_view">

上面两种方法任选其一,不可混用

//切换状态布局
niceStateView.showLoading()
niceStateView.showEmpty()
niceStateView.showError()
niceStateView.showRetry()
niceStateView.showContent()
niceStateView.showCustom(key:String)

设置点击事件

//showLoading(),showEmpty(),showError(),showRetry()都可以设置点击事件
niceStateView.showRetry().setOnViewClickListener(R.id.iv_retry) {
}

重设图片和文字

有时候两个页面的状态图样式差不多,只是文字或图片有细微差异,所以在IStateView中新增了setTextsetImage等方法。

//所以在showLoading(),showEmpty(),showError(),showRetry()后都可以重设样式
niceStateView.showEmpty()
             .setText(R.id.tv_empty, "这里空空如也~")
             .setImage(R.id.iv_empty, R.drawable.nsv_empty)

特殊场景

有些场景需要初始化耗时的动画,或者切换布局后需要释放掉动画资源,那么就需要继承IStateView类,重写需要用到的方法。可以在onAttchonDetch方法中初始化资源或者释放资源

class NiceLoadingView : IStateView() {

    /**
     * 设置填充的布局文件
     */
    override fun setLayoutRes() = R.layout.sample_loading_view

    /**
     * 初始化一些耗时的动画资源
     */
    override fun onAttach(view: View) {
        super.onAttach(view)

    }

    /**
     * 释放一些耗时的动画资源,避免内存泄露
     */
    override fun onDetach(view: View) {
        super.onDetach(view)

    }
}
private val niceStateView: NiceStateView by lazy {
    NiceStateView.newBuilder()
        .registerLoading(NiceLoadingView())
        .registerEmpty(NiceEmptyView())
        .registerError(NiceErrorView())
        .registerRetry(NiceRetryView())
        .wrapContent(view_content)
}

注册自定义的Type

NiceStateView.newBuilder()
 .registerCustom(key:String,custom)
 .wrapContent(view_content)
//
niceStateView.showCustom(key:String)

赞助

如果您觉得NiceStateView帮助到了您,可选择精准扶贫,要是10.24作者就在这里🙇🙇🙇啦!

您的支持是作者继续努力创作的动力😁😁😁

萌戳下方链接精准扶贫⤵️⤵️⤵️

扶贫方式

问题反馈Q群:1078185041

版本迭代

  • v1.0.6:支持直接在xml中定义NiceStateLayout
  • v1.0.5:迁移到jitpack
  • v1.0.4 新增直接注册layout布局的方法,修改Api调用。
  • v1.0.3 新增注册,显示自定义type的方法
  • v1.0.2 fix ConstraintLayout LayoutParams 0dp bug
  • v1.0.1 新增setTextsetImage等方法
  • v1.0.0 首次上传

About

🔥🔥🔥 - 一个超赞的页面状态切换库(加载中,空布局,网络错误,重试,自定义类型)

Resources

License

Stars

Watchers

Forks

Packages

No packages published