【Android】使RecyclerView 支持setEmptyView

自定义一个RecyclerView 的子类,使其支持EmptyView机制。

public class EmptyRecyclerView extends RecyclerView {
    private View emptyView;
    final private 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 EmptyRecyclerView(Context context) {
        super(context);
    }

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

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

    void checkIfEmpty() {
        if (emptyView != null && getAdapter() != null) {
            final boolean emptyViewVisible = getAdapter().getItemCount() == 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();

        if (emptyView.getParent() == null) {
            //如果emptyView没有父布局,则添加至当前RecyclerView的父View中
            ViewParent parent = getParent();
            if (parent instanceof RelativeLayout) {
                //相对布局
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                ViewGroup parentView = (ViewGroup) getParent();
                layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
                parentView.addView(emptyView, layoutParams);
            } else if (getParent() instanceof ViewGroup) {
                ViewGroup parentView = (ViewGroup) getParent();
                parentView.addView(emptyView);
            }
        }
    }
}

如果你使用的IRecyclerView,那么如果要支持setEmptyView,可以参照如下代码:
IRecyclerView项目地址在这里,IRecyclerView on GitHUb

package com.xxx;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.RelativeLayout;

import com.aspsine.irecyclerview.IRecyclerView;

/**
 * Created by Administrator on 2016/7/28.
 */
public class EmptyRecyclerView extends IRecyclerView {
    private View                      emptyView;
    final private 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 EmptyRecyclerView(Context context) {
        super(context);
    }

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

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

    void checkIfEmpty() {
        Adapter adapter = getMyAdapter();
        if (emptyView != null && adapter != null) {
            final boolean emptyViewVisible = adapter.getItemCount() == 0;
            emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
            setVisibility(emptyViewVisible ? GONE : VISIBLE);
        }
    }

    /**
     * 对getIAdapter做些简单处理,避免跟getAdapter重名,叫getMyAdapter。
     *
     * @return
     */
    private Adapter getMyAdapter() {
        Adapter adapter = null;
        try {
            adapter = getIAdapter();
        } catch (Exception e) {

        }
        return adapter;
    }

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

        checkIfEmpty();
    }

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

        if (emptyView.getParent() == null) {
            //如果emptyView没有父布局,则添加至当前RecyclerView的父View中
            ViewParent parent = getParent();
            if (parent instanceof RelativeLayout) {
                //相对布局
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                ViewGroup parentView = (ViewGroup) getParent();
                layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
                parentView.addView(emptyView, layoutParams);
            } else if (getParent() instanceof ViewGroup) {
                ViewGroup parentView = (ViewGroup) getParent();
                parentView.addView(emptyView);
            }
        }
    }
}

你可能感兴趣的:(Android,RecyclerVi)