布局状态库
implementation 'com.smallraw.library:SmallStatusLayout:0.1.0'
注意如果要注入的子布局或 View 的父布局是 ConstraintLayout 等需要根据 ID 约束或依赖的局部,不推荐使用低入侵方式。
val smallStatus = SmallStatus.bindTarget(view)
smallStatus.show<StateSuccess>()
smallStatus.show<StateError>()
// or
val smallStatus = SmallStatus.bindTarget(view)
smallStatus.show(StateSuccess::class.java)
smallStatus.show(StateError::class.java)
val smallStatus = SmallStatus.bindTarget(this)
smallStatus.show<StateSuccess>()
smallStatus.show<StateError>()
private lateinit var mRootView: SmallStatusContainer
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val inflate = inflater.inflate(R.layout.fragment_inject_fragment_status, container, false)
mRootView = SmallStatus.bindTarget(inflate) {
// 重试事件
it.show<StateSuccess>()
}
return mRootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
mRootView.show<StateSuccess>()
mRootView.show<StateError>()
}
注意如果要注入的子布局或 View 的父布局是 ConstraintLayout 等需要根据 ID 约束或依赖的局部,推荐使用该方法。
<com.smallraw.widget.SmallStatusLayout
android:id="@+id/layoutStatus"
android:layout_width="match_parent"
android:layout_height="350dp">
<TextView
android:id="@+id/tvTargetView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Success"
android:textSize="24sp"
android:textStyle="bold" />
</com.smallraw.widget.SmallStatusLayout>
SmallStatus.bindTarget(view,{
// 点击重试
},{
// 点击关闭按钮(默认布局为提供此按钮)
})
val layoutStatus = findById<>(xxxxx);
//or
val layoutStatus = SmallStatus.bindTarget(view);
layoutStatus.setOnRetryEventListener {
it.show<StateSuccess>()
}
layoutStatus.setOnCloseEventListener {
it.show<StateSuccess>()
}
val layoutStatus = findById<>(xxxxx);
//or
val layoutStatus = SmallStatus.bindTarget(view);
layoutStatus.show<StateError> {
it.tvErrorMsg.text = "error"
}
可以在 Application 里配置
val config = SmallStateConfig.Builder()
.alphaDuration(300)
.errorIcon(R.mipmap.ic_state_error)
.emptyIcon(R.mipmap.ic_state_empty)
.emptyMsg(R.string.empty_msg)
.loadingMsg(R.string.loading_msg)
.errorMsg(R.string.error_msg)
.build()
SmallStatus.globalConfig(config)
val config = SmallStateConfig.Builder()
.alphaDuration(300)
.errorIcon(R.mipmap.ic_state_error)
.emptyIcon(R.mipmap.ic_state_empty)
.emptyMsg(R.string.empty_msg)
.loadingMsg(R.string.loading_msg)
.errorMsg(R.string.error_msg)
.build()
val layoutStatus = SmallStatus.bindTarget(view,config = config)
//or
val layoutStatus = findById<>(xxxxx)
layoutStatus.setConfig(config)
继承 StatePage 即可
class StateCustom : StatePage() {
lateinit var customText: TextView
lateinit var customImg: ImageView
// 创建布局
override fun onCreateStatePageView(
context: Context,
inflater: LayoutInflater,
container: SmallStatusContainer
): View {
return inflater.inflate(R.layout.small_state_custom, container, false)
}
// 创建布局
override fun onConfigStateView(view: View, config: SmallStateConfig?) {
customText = view.findById(R.id.xxx)
customImg = view.findById(R.id.xxx)
}
// 是否开始点击重试功能
override fun enableReload(): Boolean = false
// 非强制重写,如果 enableReload() 开启重试功能,不重写或返回 null,则点击页面就会出发重试事件。
open fun bindRetryView(): View? = customText
// 非强制重写,如果不返回 null 则会注册点击关闭的事件。
open fun bindCloseView(): View? = customImg
}
val layoutStatus = SmallStatus.bindTarget(view)
// 直接即可使用自定义的状态布局
layoutStatus.show<StateCustom>()