I'm trying to implement a setEmptyView method by creating a custom recyclerview class with an AdapterDataObserver, like some users described here:
Equivalent of ListView.setEmptyView in RecyclerView
The problem is it's not working for me. When the list is full of data, I can see the list items perfectly but whenever the list is empty, I can't see my empty view.
Here is my custom recyclerview:
package bikurim.silverfix.com.bikurim.utils.general;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
* Created by David on 16/07/2016.
*
* A custom implementation of the RecyclerView class for supporting setEmptyView() method
*
* @author David Lasry
*/
public class FamilyRecyclerView extends RecyclerView {
private View emptyView;
private AdapterDataObserver emptyObserver = new AdapterDataObserver() {
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
checkIfEmpty();
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
checkIfEmpty();
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
checkIfEmpty();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
checkIfEmpty();
}
@Override
public void onChanged() {
checkIfEmpty();
}
};
public FamilyRecyclerView(Context context) {
super(context);
}
public FamilyRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public FamilyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setAdapter(Adapter adapter) {
super.setAdapter(adapter);
if(adapter != null) {
adapter.registerAdapterDataObserver(emptyObserver);
}
checkIfEmpty();
}
public void setEmptyView(View emptyView) {
this.emptyView = emptyView;
}
private void checkIfEmpty() {
if(emptyView != null && getAdapter() != null) {
final boolean isEmpty = getAdapter().getItemCount() == 0;
emptyView.setVisibility(isEmpty ? VISIBLE : GONE);
setVisibility(isEmpty ? GONE : VISIBLE);
}
}
}
And here is the part in my activity where I inflate a view from layout resource and set it to be my empty view:
FamilyRecyclerView recyclerView = (FamilyRecyclerView) findViewById(R.id.rv);
View emptyView = getLayoutInflater().inflate(R.layout.empty_layout, null);
recyclerView.setEmptyView(emptyView);
I debugged it a little bit and emptyView is not null, which means the inflation is successful.
What is the problem here?