android - 如何在android中向Recyclerview添加一个emptyview?

标签 android android-recyclerview

我已经尝试使用“registerAdapterDataObserver”来监听对 recyclerview 适配器的更改。从 recyclerview 中删除项目时,数据更改不会被监听器捕获。

最佳答案

Copy and paste the following code,

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

public class EmptyRecyclerView extends RecyclerView {
    private View mEmptyView;

    /** If lazy, it means the first time the adapter attached with zero items, the empty view(if exist) won' t show */
    private boolean mLazy = true;

    private boolean mObserverAttached = false;

    public EmptyRecyclerView(Context context) {
        this(context, null);
    }

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

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

    public void setEmptyView(View emptyView) {
        mEmptyView = emptyView;
    }

    public void setLazy(boolean lazy) {
        mLazy = lazy;
    }

    public void setEager(boolean eager) {
        mLazy = !eager;
    }

    public boolean isLazy() {
        return mLazy;
    }

    public boolean isEager() {
        return !mLazy;
    }

    private void updateEmptyStatus(boolean empty) {
        if (empty) {
            setVisibility(GONE);
            if (mEmptyView != null) {
                mEmptyView.setVisibility(VISIBLE);
            }
        } else {
            setVisibility(VISIBLE);
            if (mEmptyView != null) {
                mEmptyView.setVisibility(GONE);
            }
        }
    }

    public boolean isEmpty() {
        Adapter adapter = getAdapter();
        return adapter == null ? true : adapter.getItemCount() == 0;
    }

    @Override protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Adapter adapter = getAdapter();
        if (adapter != null && mObserverAttached) {
            adapter.unregisterAdapterDataObserver(mObserver);
            mObserverAttached = false;
        }
    }

    @Override public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);

        adapter.registerAdapterDataObserver(mObserver);
        mObserverAttached = true;

        if (isEager()) {
            updateEmptyStatus(isEmpty());
        }
    }

    private AdapterDataObserver mObserver = new AdapterDataObserver() {
        @Override public void onChanged() {
            super.onChanged();
            updateEmptyStatus(isEmpty());
        }

        @Override public void onItemRangeChanged(int positionStart, int itemCount) {
            super.onItemRangeChanged(positionStart, itemCount);
            updateEmptyStatus(isEmpty());
        }

        @Override public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            updateEmptyStatus(isEmpty());
        }

        @Override public void onItemRangeRemoved(int positionStart, int itemCount) {
            super.onItemRangeRemoved(positionStart, itemCount);
            updateEmptyStatus(isEmpty());
        }
    };
}


and then in your recyclerview class, just set the following line
mRecyclerView.setEmptyView(inflated);

关于android - 如何在android中向Recyclerview添加一个emptyview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32176266/

相关文章:

android - 从电子邮件附件安装 APK

android EditText 不自动换行文本

android - Kotlin 中的网格布局?

java - RecyclerView notifydatasetchanged 未从 fragment 调用

android - RecyclerView 慢速滚动

java - 带有部分的 Recyclerview,但每个部分仅带有页脚

javascript - 使用react-native克隆原生android闹钟屏幕

android - 编译时弃用警告

java - 找不到符号 STORAGE_PATH UPLOADS

android - 如何使用 RecyclerView 显示空 View ?