As mentioned in the comment, you can test emptiness of the list in the same LiveData
observer you use for .submitList()
.
Java Example:
I am assuming you are following something similar to this snippet found in the PagedListAdapter
document. I am simply adding emptiness check to that sample code.
class MyActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
MyViewModel viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
RecyclerView recyclerView = findViewById(R.id.user_list);
UserAdapter<User> adapter = new UserAdapter();
viewModel.usersList.observe(this, pagedList -> {
// Check if the list is empty
updateView(pagedList.size());
adapter.submitList(pagedList));
pagedList.addWeakCallback(null, new PagedList.Callback() {
@Override
public void onChanged(int position, int count) {
updateView(pagedList.size())
// updateView(adapter.getItemCount())
}
...
}
}
recyclerView.setAdapter(adapter);
}
private void updateView(int itemCount) {
if (itemCount > 0) {
// The list is not empty. Show the recycler view.
recyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
} else {
// The list is empty. Show the empty list view
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}
}
}
Kotlin Example:
The above Java example is actually just a Java translation of Kotlin example I found in this Android Paging codelab.