Show no content indicator for RecyclerView,
This is a reactive model that observes the recyclerView adapter items. It show "no content " when the number of items within the recyclerView is zero. During loading process, it shows a progressBar with loading status. It is not a customView ๐
implementation 'com.nocontent.emptyrecyclerindicator:RecyclerViewEmptyIndicator:1.0'
Create an instance of the EmptyViewIndicator within the application class.
class MyApplication : Application() {
private lateinit var emptyViewIndicator: EmptyViewIndicator
override fun onCreate() {
super.onCreate()
emptyViewIndicator = EmptyViewIndicator.instance()!!
}
fun getEmptyIndicator(): RecyclerViewIndicator {
return emptyViewIndicator.build()
}
}
//at the top of activity
lateinit var emptyRecyclerIndicator: RecyclerViewIndicator
//anywhere
emptyRecyclerIndicator = (applicationContext as MyApplication).getEmptyIndicator()
recyclerView.layoutManager = LinearLayoutManager(this)
emptyRecyclerIndicator.show(this, recyclerView)
You need to override a method by registering DataObserver within the adapter as below.
override fun registerAdapterDataObserver(observer: AdapterDataObserver) {
super.registerAdapterDataObserver(observer)
val myApplication = context.applicationContext as MyApplication
myApplication.getEmptyIndicator().recyclerViewState(itemList.size)
}
The view need to be refreshed when new data is fetched.
emptyRecyclerIndicator.refresh()
Most of these methods are optional but essential for view customization.
setNoDataExplaination(explaination: String) | Used to set the message shown when there is no content |
setLoadingExplaination(loadingStatus: String) | Set the loading message |
recyclerViewState(count: Int) | Pass the items-count within the recyclerView adapter |
refresh() | Refresh the indicator state when fetching new data for the recyclerView |
setWidth(width: Int) | Set the width of the window that will be shown |
setHeight(height: Int) | Set the height of the window |
setCustomLayout(layout: Int) | Set the custom layout |
show(activity: Activity, recylerView: RecyclerView) | Used to attach the emptyViewIndicator to the recyclerView |