java RecyclerView with provosion添加像列表视图的空视图。数据集为空时显示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java RecyclerView with provosion添加像列表视图的空视图。数据集为空时显示相关的知识,希望对你有一定的参考价值。

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



public class EmptyRecyclerView extends RecyclerView {
    @Nullable
    View emptyView;

    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) {
            emptyView.setVisibility(getAdapter().getItemCount() > 0 ? GONE : VISIBLE);
        }
    }

    final @NonNull AdapterDataObserver observer = new AdapterDataObserver() {

        @Override public void onChanged() {
            super.onChanged();
            checkIfEmpty();
        }

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

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

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

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

以上是关于java RecyclerView with provosion添加像列表视图的空视图。数据集为空时显示的主要内容,如果未能解决你的问题,请参考以下文章

RecyclerView Item Click Listener with DataBinding

android RecyclerView pinch zoom,ScaleGestureDetector&GridLayoutManager,with BigImageViewer,kotli

Android学习笔记:超能RecyclerView组件使用总结

Firebase:RecyclerView 跳过布局没有附加适配器

RecyclerView 中的 Android 调色板

Eclipse中使用recyclerview时出现Caused by: java.lang.NoClassDefFoundError: android.support.v7.recyclerview.