I am trying to implement EmptyView on my RecyclerView Adapter , but I am not getting any result.
I followed this tutorial and this tip , but no one worked for me.
I implemented:
if (viewType == EMPTY_VIEW) { v = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_view, parent, false); EmptyViewHolder evh = new EmptyViewHolder(v); return evh; } v = LayoutInflater.from(parent.getContext()).inflate(R.layout.data_row, parent, false); ViewHolder vh = new ViewHolder(v); return vh;
But this does not allow me to compile, because they are different ViewHolder , because I created two ViewHolder classes, but they extends Recycler.ViewHolder , so I do not get it ...
I am trying to do this because I have SearchView and when I want the list to be empty, it shows EmptyView , I do it programmatically, but I prefer to add as layout because I donโt know how to make TextViews and Buttons programmatically.
Also if I put
return dataList.size() > 0 ? dataList.size() : 1;
It gives me an error because the index is 0.
I debugged viewType and is always 1, then it will not join the if condition ...
Deep on Android I found this:
public int getItemViewType(int position) { return 0; }
But the fact is that no changes are changed.
EDIT
I almost did it, I did this:
@Override public int getItemViewType(int position) { return list.size() > 0 ? list.size() : 1; }
But sometimes it returns 0 when size () is 0 ... I donโt understand, I use this SearchView , and sometimes when I print a letter that does not correspond to any element of the list, it is not displayed, and sometimes it ...
The following also happens: when the popup windows of the layout displayed on the left screen on the screen, when I put it on the center , but I think this is a problem with the RecyclerView , because the layout is placed inside it.
RecyclerView Layout:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:id="@+id/rtpew" android:layout_centerInParent="true" app:layout_behavior="@string/appbar_scrolling_view_behavior" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearpew"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>
And this is my emptylayout :
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ImageViewSearchFail" android:src="@drawable/sadface" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_gravity="center" android:textSize="@dimen/15dp" android:layout_marginTop="4dp" android:text="foo" android:layout_below="@+id/ImageViewSearchFail" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ButtonAddEntity" android:text="foo" android:background="?android:selectableItemBackground" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Another way that I thought was to programmatically execute it as follows:
@Override public boolean onQueryTextChange(String query) { final ArrayList<List> filteredModelList = filter(mModel, query); mAdapter.animateTo(filteredModelList); rv.scrollToPosition(0); if(query.isEmpty()){
AND:
private ArrayList<List> filter(ArrayList<List> models, String query) { query = query.toLowerCase(); final ArrayList<List> filteredModelList = new ArrayList<List>(); for (List model : models) { final String text = model.getRedName().toLowerCase(); if (text.contains(query)) { filteredModelList.add(model); } } if (filteredModelList.size()<0) {
PROBLEMS
-I only add the view using the @Jimeux answer , but I would like to do it on the Adapter , I got it, but it does not always show view , even if list empty.
- At the time when putting emptyview.xml places inside the RecyclerView , since I put all this xml in the center, it shows on the right. I tried to add xml programmatically, but it's like chaos ....