如何给RecyclerView设置一个emptyView?
最新澳门博彩导航
最新澳门博彩导航
当前位置 : 最新澳门博彩导航 > 澳门赌场开户

如何给RecyclerView设置一个emptyView?

网上这些资料好像比较少,有的我也不太懂,还请大神们指点一个简单易懂的。

设置一个Recyclerview的平级view,平时把它GONE掉,recyclerview的item为0时把它visible出来不久好了么

给楼主之前写的代码,将RecycleView 重写成如下:

public class RecyclerViewEmptySupport extends RecyclerView {

    private View emptyView;
    final private AdapterDataObserver observer = new AdapterDataObserver {
        @Override
        public void onChanged {
            checkIfEmpty;
        }

        @Override
        public void onItemRangeInsertedint positionStart, int itemCount {
            checkIfEmpty;
        }

        @Override
        public void onItemRangeRemovedint positionStart, int itemCount {
            checkIfEmpty;
        }
    };

    public RecyclerViewEmptySupportContext context {
        supercontext;
    }

    public RecyclerViewEmptySupportContext context, AttributeSet attrs {
        supercontext, attrs;
    }

    public RecyclerViewEmptySupportContext context, AttributeSet attrs, int defStyle {
        supercontext, attrs, defStyle;
    }

    @Override
    public void setAdapterAdapter adapter {
        final Adapter oldAdapter = getAdapter;
        if oldAdapter != null {
            oldAdapter.unregisterAdapterDataObserverobserver;
        }
        super.setAdapteradapter;
        if adapter != null {
            adapter.registerAdapterDataObserverobserver;
        }

        checkIfEmpty;
    }

    public void setEmptyViewView emptyView {
        this.emptyView = emptyView;
        checkIfEmpty;
    }

    void checkIfEmpty {
        if emptyView != null && getAdapter != null {
            final boolean emptyViewVisible = getAdapter.getItemCount == 0;
            emptyView.setVisibilityemptyViewVisible ? VISIBLE : GONE;
            setVisibilityemptyViewVisible ? GONE : VISIBLE;
        }
    }
}

外部调用的时候执行mRecyclerView.setEmptyViewmEmptyView;即可。

栏目列表

广告位

最新澳门博彩导航