Skip to content

Usage

Quick start

<com.ave.vastgui.netstatelayout.NetStateLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/net_state_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--content-->

</com.ave.vastgui.netstatelayout.NetStateLayout>

Change page state

  • Loading state

    mNetStateLayout.showLoading()
    
  • Empty data state

    mNetStateLayout.showEmptyData()
    
  • Loading error state

    Version 1.1.1

    mNetStateLayout.showLoadingError()
    

    Starting from version 1.1.1, you can set the response code and message to the showLoadingError , for example:

    mNetStateLayout.showLoadingError(404, "The address does not exist.")
    
  • Net error state

    Version 1.1.1

    mNetStateLayout.showNetError()
    

    Starting from version 1.1.1, you can set the requested exception to the showNetError , for example:

    mNetStateLayout.showNetError(RuntimeException("Network Unavailable"))
    
  • When loading is successful, you can call showSuccess to display the page.

    mNetStateLayout.showSuccess()
    

Update page content

Version 1.1.1

The OnXXXListener of NetStateLayout provides the onXXXInflate method, where you can modify the content displayed on the page. Take modifying the information displayed on the default page default_empty_data as an example:

val netStateMgr = NetStateMgr(this).apply {
    setOnEmptyDataListener(object : OnEmptyDataListener {
        override fun onEmptyDataInflate(view: View) {
            val textView: MaterialTextView =
                (view as ConstraintLayout).children.first() as MaterialTextView
            textView.text = "The response data is empty"
        }
    })
}
mNetStateLayout.setNetStateMgr(netStateMgr)

Set page click event

Version 1.1.1

The OnXXXListener interface of NetStateLayout provides the onXXXClick , where you can modify the click event of the page. Take the network unavailability as an example:

private val wifiSetting =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        ... 
    }

val netStateMgr = NetStateMgr(this).apply {
    setOnNetErrorListener(object : OnNetErrorListener {
        override fun onNetErrorClick() {
            wifiSetting.launch(Intent(Settings.ACTION_WIFI_SETTINGS))
        }
    })
}
mNetStateLayout.setNetStateMgr(netStateMgr)

Customize state page

NetStateLayout allows you to customize the state page. Take a custom empty data page as an example:

val netStateMgr = NetStateMgr(this).apply {
    setEmptyDataView(R.layout.page_empty_data_state)
}
mNetStateLayout.setNetStateMgr(netStateMgr)

Sample code

Sample code