Skip to content

Loading, Content, Error

Yogesh Choudhary Paliyal edited this page Oct 12, 2021 · 1 revision

example : Hitting an api for loading showing a shimmer and then when API response came show the data or getting an error

Initialize Adapter

private val mAdapter by lazy {
        UniversalRecyclerAdapter.Builder<UserModel>(
            lifecycleOwner = this,
            content = UniversalAdapterViewType.Content(R.layout.item_user,
                object : BasicListener<UserModel> {
                override fun onClick(model: UserModel) {
                    Toast.makeText(this@LoadingListingActivity, model.name, Toast.LENGTH_SHORT)
                        .show()
                }
            }),
            loading = UniversalAdapterViewType.Loading(R.layout.layout_loading_full_page, defaultLoadingItems = 1),
            error = UniversalAdapterViewType.Error(R.layout.item_error)).build()
    }

Attach to adapter

binding.recyclerView.adapter = mAdapter.getAdapter()

Update Data

mViewModel.data.observe(this, Observer { data -> 
   // The data type is like Resource<List<T>>
   // if data.status == Status.LOADING thenn it will show the loading view
   // if data.status == Status.SUCCESS then it will show the content list
   // if data.status == Status.ERROR then it will show the error View and data.message will be passed to view as message variable
          mAdapter.updateContent(it)
})