### Start Default Page Animation Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/animation.md Define a reusable extension function to start a fade-in animation for a View. This function hides the view initially and then animates its alpha to 1 over 800 milliseconds. ```kotlin private fun View.startAnimation() { // 先将视图隐藏然后在800毫秒内渐变显示视图 animate().setDuration(0).alpha(0F).withEndAction { animate().setDuration(800).alpha(1F) } } ``` -------------------------------- ### Configure Global State Changed Handler Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/animation.md Set a custom StateChangedHandler globally for all StateLayout instances. This example shows how to assign an instance of FadeStateChangedHandler to StateConfig. ```kotlin StateConfig.stateChangedHandler = StateChangedHandler() ``` -------------------------------- ### Configure StateLayout Layouts in Code Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/index.md Configure the empty, error, and loading layouts for the StateLayout programmatically. Ensure the layout resources are defined. ```kotlin state.apply { emptyLayout = R.layout.layout_empty // 配置空布局 errorLayout = R.layout.layout_error // 配置错误布局 loadingLayout = R.layout.layout_loading // 配置加载中布局 } ``` -------------------------------- ### Configure StateLayout Layouts in XML Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/index.md Configure the empty, error, and loading layouts for the StateLayout using XML attributes. This is an alternative to programmatic configuration. ```xml ``` -------------------------------- ### Add JitPack Repository (Older Android Studio) Source: https://github.com/liangjingkanji/statelayout/blob/master/README.md For Android Studio versions older than Arctic Fox, add the JitPack repository to your project's root build.gradle file. ```groovy allprojects { repositories { // ... maven { url 'https://jitpack.io' } } } ``` -------------------------------- ### Add JitPack Repository (Newer Android Studio) Source: https://github.com/liangjingkanji/statelayout/blob/master/README.md For Android Studio Arctic Fox and newer, add the JitPack repository to your project's settings.gradle file. ```kotlin dependencyResolutionManagement { repositories { // ... maven { url 'https://jitpack.io' } } } ``` -------------------------------- ### Trigger Refresh and Show Loading Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/callback.md Use onRefresh to define actions like network requests, followed by showLoading to display the loading state. ```kotlin state.onRefresh { // 执行请求 }.showLoading() ``` -------------------------------- ### Show Different States Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/index.md Call these methods to display the loading, empty, error, or content view. An optional tag can be passed to customize display via lifecycle callbacks. ```kotlin state.showLoading() state.showEmpty() state.showError() state.showContent() `showXX()`都有一个Any参数(标签), 可以传递对象到[生命周期](callback.md)中, 进行定制化展示 ``` -------------------------------- ### Registering Multiple Callbacks Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/callback.md Chain multiple callbacks to handle different state transitions such as empty, error, loading, and refresh events. ```kotlin state.onEmpty { }.onError { }.onLoading { }.onRefresh { } ``` -------------------------------- ### Create StateLayout Programmatically Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/index.md This method is not recommended due to performance concerns and potential issues with repeated calls creating multiple StateLayout instances. ```kotlin stateCreate() // 可在 Activity/Fragment/View 中使用 ``` -------------------------------- ### Configure Global StateLayout Settings Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/config.md Apply global configurations for StateLayout, such as setting custom layouts for empty, error, and loading states. You can also define retry IDs and callbacks for state transitions. ```kotlin StateConfig.apply { emptyLayout = R.layout.layout_empty errorLayout = R.layout.layout_error loadingLayout = R.layout.layout_loading setRetryIds(R.id.msg) // 全局的重试Id onLoading { } onEmpty { } onError { } } ``` -------------------------------- ### Show Error with Tag Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/tag.md Use showXX() with a ResponseException to display an error state. The tag can be used in callbacks to differentiate error types. ```kotlin state.showError(ResponseException(code = 403, msg = "请求参数错误")) ``` -------------------------------- ### Add StateLayout Dependency Source: https://github.com/liangjingkanji/statelayout/blob/master/README.md Add the StateLayout library dependency to your module's build.gradle file. ```groovy implementation 'com.github.liangjingkanji:StateLayout:1.4.2' ``` -------------------------------- ### Custom Retry Logic in Lifecycle Callback Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/retry.md Implement custom retry logic by setting click listeners on views within the error state's lifecycle callback. This allows for more complex retry scenarios, such as manually showing the loading state. ```kotlin binding.state.onError { findViewById(R.id.image).setOnClickListener { binding.state.showLoading() } } ``` -------------------------------- ### Declare StateLayout in XML Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/index.md Use this XML to declare a StateLayout in your layout file. It wraps a content view and can be customized with attributes. ```xml ``` -------------------------------- ### Handle Errors with Global Listener Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/tag.md Configure a global onError listener using StateConfig to manage errors across the application. This allows for centralized error management. ```kotlin StateConfig.onError { if (it is ResponseException) { findViewById.text = "错误码: ${it.code}, 错误信息: ${it.msg}" } } ``` -------------------------------- ### Configuring Global View Callbacks for Errors Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/multi-state.md Configure global callbacks to handle specific error types, such as network errors. This allows for custom UI adjustments, like displaying a different image for network-related errors. ```kotlin StateConfig.onError { if (it is NetNetworkingException) { // 为无网络异常展示不同图片, 当然你也可以addView findViewById(R.id.iv_error).setImageResource(R.drawable.ic_networking_error) } } ``` -------------------------------- ### Configure Retry IDs for Single Instance Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/retry.md Set the IDs of views that should trigger a retry action when clicked in error or empty states for a specific StateLayout instance. This allows for targeted retry configurations. ```kotlin state.setRetryIds(R.id.msg) ``` -------------------------------- ### Custom Fade State Changed Handler Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/animation.md Implement a custom StateChangedHandler for fade transitions between states. This handler animates the alpha of views during removal and addition, with a configurable duration. It skips animation for the initial loading state. ```kotlin /** * 切换状态时使用渐变透明动画过渡 * @param duration 动画执行时间 */ open class FadeStateChangedHandler(var duration: Long = 400) : StateChangedHandler { private var stateLayout: WeakReference = WeakReference(null) /** * StateLayout删除缺省页, 此方法比[onAdd]先执行 * @param container StateLayout * @param state 将被删除缺省页视图对象 * @param status 当前状态 * @param tag 显示状态传入的tag */ override fun onRemove(container: StateLayout, state: View, status: Status, tag: Any?) { if (container != stateLayout.get() && status == Status.LOADING) { return super.onRemove(container, state, status, tag) } state.animate().setDuration(duration).alpha(0f).setListener(object : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator) { // 等待动画执行完毕后删除旧的缺省页视图 StateChangedHandler.onRemove(container, state, status, tag) } }).start() } /** * StateLayout添加缺省页 * @param container StateLayout * @param state 将被添加缺省页视图对象 * @param status 当前状态 * @param tag 显示状态传入的tag */ override fun onAdd(container: StateLayout, state: View, status: Status, tag: Any?) { // 初次加载不应用动画 if (container != stateLayout.get() && status == Status.LOADING) { stateLayout = WeakReference(container) return StateChangedHandler.onAdd(container, state, status, tag) } super.onAdd(container, state, status, tag) state.alpha = 0f state.animate().setDuration(duration).alpha(1f).start() } } ``` -------------------------------- ### Customizing State Handling with StateChangedHandler Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/callback.md Implement StateChangedHandler to completely control the state transition logic, including animations and layout parameters. This can be set globally or per instance. ```kotlin state.stateChangedHandler = StateChangedHandler() ``` ```kotlin StateConfig.stateChangedHandler = StateChangedHandler() ``` -------------------------------- ### Apply Animation to Default Pages Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/animation.md Apply the custom animation function to specific default page states (onError, onEmpty, onContent, onLoading) using StateConfig. ```kotlin StateConfig.apply { onError { startAnimation() } onEmpty { startAnimation() } onContent { startAnimation() } onLoading { startAnimation() } } ``` -------------------------------- ### Passing Tags to Differentiate States Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/multi-state.md Pass an exception object as a tag to differentiate error states. This allows for specific handling based on the type of exception encountered. ```kotlin showError(NetNetworkingException()) ``` -------------------------------- ### Configure Global Retry IDs Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/retry.md Set the IDs of views that should trigger a retry action globally for all StateLayout instances. This provides a convenient way to apply retry behavior across your application. ```kotlin StateConfig.setRetryIds(R.id.msg) ``` -------------------------------- ### Handle Errors with Single Instance Listener Source: https://github.com/liangjingkanji/statelayout/blob/master/docs/tag.md Implement an onError listener on the state object to handle specific error responses. This is useful for localized error handling. ```kotlin state.onError { if (it is ResponseException) { findViewById.text = "错误码: ${it.code}, 错误信息: ${it.msg}" } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.