Header Ads

RecyclerView setEmptyView

From Android Docs, RecyclerView is a more advanced and flexible version of ListView because it is more efficient and it doesnt care about the visuals. From the looks of it, it expects that the RecyclerView must always have data but thats not always possible, as i search how empty views is done in RecyclerView i cant seem to find one (If you know one or his better then the following codes, please comment below).
So here is what i did
private void checkAdapterIsEmpty () {
if (mAdapter.getItemCount() == 0) {
mEmptyView.setVisibility(View.VISIBLE);
} else {
mEmptyView.setVisibility(View.GONE);
}
}

protected void setupRecyclerView() {
mAdapter = new MyAdapter(mItemList);
mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
checkAdapterIsEmpty();
}
});

mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);

mRecyclerView.setAdapter(mAdapter);
checkAdapterIsEmpty();
}

No comments:

Powered by Blogger.