RecyclerView is populated with a list via LinearLayoutManager, the question is that when the list is empty, how does it display in RecyclerView that it is empty?
- Add one item that is different from the others and insert a TextView with the inscription "The list is empty" - Kirill Stoianov
- oneUse the 'setEmptyView' method. Here is an example: stackoverflow.com/questions/28217436/… - AndreyEKB
- If a custom adapter, then as for me it is best to create a separate look, and pull it out. or make an additional hidden block, and show it. - Andrey Shpileva
- @AndreyEKB example is really working, but where did you see the 'setEmptyView' method there? The setEmptyView method does not work with RecyclerView. - ZigZag
- @ZigZag I understand this is a custom method - Sharoff
|
1 answer
In general, this is what I invented - I created a custom adapter:
private class EmptyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); View view = layoutInflater.inflate(R.layout.list_empty_my, parent, false); return new RecyclerView.ViewHolder(view) {}; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { } @Override public int getItemCount() { return 1; } }
My layout:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/list_item_my_empty" android:layout_width="match_parent" android:layout_height="match_parent" android:hint="@string/empty_list" android:padding="4dp" android:gravity="center"/> </RelativeLayout>
And in the code inserted
List<MyModel> models = MyModelLab.get().getList(); if(models.size() == 0){ mMyRecyclerView.setAdapter(new EmptyAdapter()); return; }
The solution was created before this topic, it just did not work, as it turned out the whole problem was that in the getItemCount () method it returned 0, because of this nothing returned on the screen, replaced with 1 and the block was displayed
|