MultiStatePage

Download the Demo

Github project address

Activity Fragment View ViewPager2
Lottie Extension (Custom State) Refresh the State Network request mock

Functions and features of MultiStatePage

  • You don’t need to add view code to the layout
  • Can display custom status view, arbitrary expansion
  • Can be used for activities, fragments, or specified views
  • Custom rerequest listening
  • View styles can be updated dynamically
  • It can be used in conjunction with third-party controls
  • Support for status callback listening
  • Kotlin develops apis that are easier to use

start

Add the dependent

Add the JitPack repository to your build file

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
Copy the code

Add the dependency

Dependencies {implementation 'com.github.Zhao-Yan :MultiStatePage:1.0.0'}Copy the code

1. Generate MultiStateContainer

Use it on View

Basic usage

val multiStateContainer = MultiStatePage.multiState(view)
Copy the code

Kotlin extension method

val multiStateContainer = view.multiState()
Copy the code

Use in the Activity root View

Basic usage

class MultiStateActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.xxx)
       val multiStateContainer = MultiStatePage.multiStateActivity(this)}}Copy the code

Kotlin extension method

class MultiStateActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.xxx)
        val multiStateContainer = multiStateActivityRoot()
    }
}
Copy the code

Use in the Fragment root View

class MultiStateFragment : BaseFragment<FragmentMultiStateBinding>() {
    private lateinit var multiStateContainer: MultiStateContainer
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup? , savedInstanceState:Bundle?).: View? {
        val root = inflater.inflate(R.layout.activity_api, container, false)
        multiStateContainer = MultiStatePage.multiState(root)
        return multiStateContainer
    }
}
Copy the code

2. Switch status

Call the multiStatecontainer.show

() method

There are three built-in states by default

val multiStateContainer = MultiStatePage.multiState(view)
/ / success
multiStateContainer.show<SuccessState>()
/ / error page
multiStateContainer.show<ErrorState>()
/ / page
multiStateContainer.show<EmptyState>()
// Load the status page
multiStateContainer.show<LoadingState>()
Copy the code

Dynamically set state

multiStateContainer.show<ErrorState>{errorState->
    errorState.setErrorMsg("XXX is wrong")}Copy the code

Set the retry callback

val multiStateContainer = MultiStatePage.multiState(view){
    Toast.makeText(context, "retry", Toast.LENGTH_SHORT).show()
}
Copy the code

The custom State

1. The inheritanceMultiState

class LottieWaitingState : MultiState() {
    override fun onCreateMultiStateView(
        context: Context,
        inflater: LayoutInflater,
        container: MultiStateContainer
    ): View {
        return inflater.inflate(R.layout.multi_lottie_waiting, container, false)}override fun onMultiStateViewCreate(view: View) {
        // Logical processing
    }

    override fun enableReload(a): Boolean = false
}
Copy the code

EnableReload () Whether to allow retry callback False No

2. Register before use

class LottieExtActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        MultiStatePage.register(LottieWaitingState())
        val multiStateContainer = multiStateActivityRoot()
        multiStateContainer.show<LottieWaitingState>()
    }
}
Copy the code

You can also register in the Application (recommended)

class App : Application() {
    override fun onCreate(a) {
        super.onCreate()
        MultiStatePage.register(CustomState(), OtherState())
    }
}
Copy the code

tip

You can use Kotlin’s extension functions to encapsulate common states

fun MultiStateContainer.showSuccess(callBack: (SuccessState) - >Unit = {}) {
    show<SuccessState> {
        callBack.invoke(it)
    }
}

fun MultiStateContainer.showError(callBack: (ErrorState) - >Unit = {}) {
    show<ErrorState> {
        callBack.invoke(it)
    }
}

fun MultiStateContainer.showEmpty(callBack: (EmptyState) - >Unit = {}) {
    show<EmptyState> {
        callBack.invoke(it)
    }
}

fun MultiStateContainer.showLoading(callBack: (LoadingState) - >Unit = {}) {
    show<LoadingState> {
        callBack.invoke(it)
    }
}
Copy the code

call

val multiStateContainer = multiStateActivityRoot()
multiStateContainer.showLoading()
Copy the code

Thanks

  • DylanCaiCoding/LoadingHelper
  • KingJA/LoadSir
  • airbnb/lottie-android
  • Lottie Animation Resource Community