123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /*
- * 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 <http://www.gnu.org/licenses/>.
- */
- 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;
- }
- }
|