allprojects {
repositories {
maven { url 'https://www.jitpack.io' }
}
}
dependencies {
implementation 'com.github.ALguojian:StatusLayout:1.0.6'
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
<TextView
android:id="@+id/statusLayout"
android:gravity="center"
android:background="#ff0"
android:layout_width="300dp"
android:layout_height="299dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
//设置默认布局,DefaultStatusAdapter()为提供的默认加载布局,如果需要自定义,往下看
StatusLayout.setDefaultAdapter(StatusLayoutDefaultAdapter())
//开启debug日志,默认关闭
StatusLayout.setDebug()
var statusHelper=StatusLayout.attachView(this)//绑定activity
.onRetryClick { }//加载失败,点击重试的回调
注意::如果id-statusLayout,在fragment中为xml的根节点时,这是通过view的getParent(),是找不到父view的, 所以新建了一个新的FramLayout(),进行状态管理,所以,在onCreateView中需要返回StatusLayout.getInstance().getRootView()
val inflate: ViewDataBinding = DataBindingUtil.inflate<ViewDataBinding>(inflater, R.layout.fragment_simple, container, false)
StatusLayout
.attachView(inflate.root)//直接绑定引入的根view
.onRetryClick { }
//需要返回管理view
return StatusLayout.getRootView()
statusHelper.showLoading()
statusHelper.showSuccess()
statusHelper.showFailed()
statusHelper.showEmpty()
//设置一个新的adapter,后续操作都需要使用新的StatusLayout对象
StatusLayout statusLayout = StatusLayout.setNewAdapter(DefaultStatusAdapter())
statusLayout.attachView(this).onRetryClick {
}
//如果为某一个页面设置了新的适配器,需要添加
override fun onDestroy() {
super.onDestroy()
StatusLayout.clearNewAdapter()
}
class StatusLayoutDefaultAdapter : StatusAdapter {
override fun getView(statusHelper: StatusLayout.StatusHelper, statusView: View?, status: Int): View? {
var defaultLoadingView: DefaultLoadingView? = null
if (statusView != null && statusView is DefaultLoadingView) defaultLoadingView = statusView
if (defaultLoadingView == null) {
defaultLoadingView = DefaultLoadingView(statusHelper.context!!, statusHelper.click)
}
defaultLoadingView.setStatus(status)
return defaultLoadingView
}
}