0

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?

Community
  • 1
  • 1
David Lasry
  • 1,407
  • 4
  • 26
  • 43

1 Answers1

0

It looks like you haven't added your emptyView to the hierarchy anywhere, when you inflate it you set the parent as null and while you toggle its visibility when it is in the listView you never actually add it as a child of any view. As a result, it exists but it isn't displayed on the screen. To get it show up you can inflate it with a parent view or add it to the parent view after inflation:

getLayoutInflater().inflate(R.layout.empty_layout, contentView);

or

contentView.addView(emptyView);

(The contentView is the view that is holding your recyclerView)

Dr. Nitpick
  • 1,662
  • 1
  • 12
  • 16