自定义可以设置EmptyView的RecyclerView

自定义可以设置EmptyView的RecyclerView

ListView有个setEmptyView方法。当ListView数据为空或者0时,可以显示设置好的EmptyView,非常方便。然而RecyclerView没有这个方法。

以下是从网上找到的方法。

自定义EmptyRecyclerView

package com.xtion.switchlist.view;

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

/**
 * 可以setEmptyView的RecyclerView
 */

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, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public EmptyRecyclerView(Context context, @Nullable 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();
    }
}

然后在布局文件中引用这个EmptyRecyclerView:

      <com.xtion.switchlist.view.EmptyRecyclerView
                android:id="@+id/orderm_list_detail"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
       "@+id/no_data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/ic_empty"
                android:layout_gravity="center"
                android:text="暂无数据"
                android:visibility="gone"/>

这里的EmptyView为TextView,初始设置其visibility为gone。在代码中记得要setEmptyView即可:

emptyRecyclerView.setEmptyView(textView);

关于AdapterDataObserver

AdapterDataObserver是RecyclerView的抽象内部类。

public static abstract class AdapterDataObserver {
        public void onChanged() {
            // Do nothing
        }

        public void onItemRangeChanged(int positionStart, int itemCount) {
            // do nothing
        }

        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            // fallback to onItemRangeChanged(positionStart, itemCount) if app
            // does not override this method.
            onItemRangeChanged(positionStart, itemCount);
        }

        public void onItemRangeInserted(int positionStart, int itemCount) {
            // do nothing
        }

        public void onItemRangeRemoved(int positionStart, int itemCount) {
            // do nothing
        }

        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            // do nothing
        }
    }

RecyclerView通过AdapterDataObservable管理所有的AdapterObserver。当RecyclerView的数据发生变化时,就通知给AdapterDataObservable,再由AdapterDataObservable通知给注册了的AdapterDataObserver。

EmptyRecyclerView中,注册了一个的AdapterDataObserver。并重写了onChanged,onItemRangeInserted,onItemRangeRemoved三个方法,当RecyclerView的数据结构发生变化时,去检查数据是否为空,如果为空则显示EmptyView。

你可能感兴趣的:(Android)