/* * Nextcloud Android client application * * @author Tobias Kaminsky * Copyright (C) 2018 Tobias Kaminsky * Copyright (C) 2018 Nextcloud GmbH. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package com.owncloud.android.ui; import android.content.Context; import android.util.AttributeSet; import android.view.View; import androidx.annotation.Nullable; import androidx.recyclerview.widget.RecyclerView; /** * Extends RecyclerView to show a custom view if no data is available * Inspired by http://alexzh.com/tutorials/how-to-setemptyview-to-recyclerview */ public class EmptyRecyclerView extends RecyclerView { private View mEmptyView; private boolean hasFooter = false; 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); } @Override public void setAdapter(Adapter adapter) { Adapter oldAdapter = getAdapter(); super.setAdapter(adapter); if (oldAdapter != null) { oldAdapter.unregisterAdapterDataObserver(observer); } if (adapter != null) { adapter.registerAdapterDataObserver(observer); } } public void setEmptyView(View view) { this.mEmptyView = view; initEmptyView(); } private void initEmptyView() { if (mEmptyView != null) { int emptyCount = hasFooter ? 1 : 0; boolean empty = getAdapter() == null || getAdapter().getItemCount() == emptyCount; mEmptyView.setVisibility(empty ? VISIBLE : GONE); this.setVisibility(empty ? GONE : VISIBLE); } } private final AdapterDataObserver observer = new AdapterDataObserver() { @Override public void onChanged() { super.onChanged(); initEmptyView(); } @Override public void onItemRangeInserted(int positionStart, int itemCount) { super.onItemRangeInserted(positionStart, itemCount); initEmptyView(); } @Override public void onItemRangeRemoved(int positionStart, int itemCount) { super.onItemRangeRemoved(positionStart, itemCount); initEmptyView(); } }; public void setHasFooter(boolean bool) { hasFooter = bool; } }