package co.infinum.mjolnirrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

/**
 * Custom implementation of RecyclerView, which has support for Empty view.
 *
 * Created by Željko Plesac on 27/09/16.
 */
public class MjolnirRecyclerView extends RecyclerView {

    private View emptyView;

    private final AdapterDataObserver observer = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            checkIfEmpty();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            checkIfEmpty();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            checkIfEmpty();
        }
    };

    public MjolnirRecyclerView(Context context) {
        super(context);
    }

    public MjolnirRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MjolnirRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private void checkIfEmpty() {
        if (emptyView != null && getAdapter() != null) {
            final boolean emptyViewVisible = ((MjolnirRecyclerAdapter) getAdapter()).getCollectionCount() == 0;
            emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
            setVisibility(emptyViewVisible ? GONE : VISIBLE);
        }
    }

    @Override
    public void setAdapter(Adapter adapter) {
        final Adapter oldAdapter = getAdapter();
        if (oldAdapter != null) {
            oldAdapter.unregisterAdapterDataObserver(observer);
        }
        super.setAdapter(adapter);
        if (adapter != null) {
            adapter.registerAdapterDataObserver(observer);
        }

        checkIfEmpty();
    }

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
        checkIfEmpty();
    }

    public boolean isEmptyViewShown() {
        return emptyView != null && emptyView.getVisibility() == VISIBLE;
    }
}

@@ -1145,24 +1145,155 @@
 le);%0A    %7D%0A%0A
+    /**%0A     * Checks if the adapter collection is empty. If it is, hide the RecyclerView content and show the empty view.%0A     */%0A
     private 
@@ -1630,24 +1630,263 @@
    %7D%0A    %7D%0A%0A
+    /**%0A     * Sets the RecyclerView's adapter. It also checks whether the adapter collection is empty - if it is, it will automatically show%0A     * empty view. Otherwise, the adapter's content will be shown.%0A     * @param adapter%0A     */%0A
     @Overrid
@@ -2265,117 +2265,379 @@
     
-public void setEmptyView(View emptyView) %7B%0A        this.emptyView = emptyView;%0A        checkIfEmpty();%0A    %7D%0A
+/**%0A     * Sets the empty view. RecyclerView can have only one empty view at the time.%0A     * @param emptyView%0A     */%0A    public void setEmptyView(View emptyView) %7B%0A        this.emptyView = emptyView;%0A        checkIfEmpty();%0A    %7D%0A%0A    /**%0A     * Checks whether the RecyclerView is showing empty view.%0A     * @return true is empty view is shown, false otherwise.%0A     */
 %0A   

Add comments for MjolnirRecyclerView