EmptyRecyclerView.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2018 Tobias Kaminsky
  6. * Copyright (C) 2018 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui;
  22. import android.content.Context;
  23. import android.util.AttributeSet;
  24. import android.view.View;
  25. import androidx.annotation.Nullable;
  26. import androidx.recyclerview.widget.RecyclerView;
  27. /**
  28. * Extends RecyclerView to show a custom view if no data is available
  29. * Inspired by http://alexzh.com/tutorials/how-to-setemptyview-to-recyclerview
  30. */
  31. public class EmptyRecyclerView extends RecyclerView {
  32. private View mEmptyView;
  33. private boolean hasFooter = false;
  34. public EmptyRecyclerView(Context context) {
  35. super(context);
  36. }
  37. public EmptyRecyclerView(Context context, @Nullable AttributeSet attrs) {
  38. super(context, attrs);
  39. }
  40. public EmptyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
  41. super(context, attrs, defStyle);
  42. }
  43. @Override
  44. public void setAdapter(Adapter adapter) {
  45. Adapter oldAdapter = getAdapter();
  46. super.setAdapter(adapter);
  47. if (oldAdapter != null) {
  48. oldAdapter.unregisterAdapterDataObserver(observer);
  49. }
  50. if (adapter != null) {
  51. adapter.registerAdapterDataObserver(observer);
  52. }
  53. }
  54. public void setEmptyView(View view) {
  55. this.mEmptyView = view;
  56. initEmptyView();
  57. }
  58. private void initEmptyView() {
  59. if (mEmptyView != null) {
  60. int emptyCount = hasFooter ? 1 : 0;
  61. boolean empty = getAdapter() == null || getAdapter().getItemCount() == emptyCount;
  62. mEmptyView.setVisibility(empty ? VISIBLE : GONE);
  63. this.setVisibility(empty ? GONE : VISIBLE);
  64. }
  65. }
  66. private final AdapterDataObserver observer = new AdapterDataObserver() {
  67. @Override
  68. public void onChanged() {
  69. super.onChanged();
  70. initEmptyView();
  71. }
  72. @Override
  73. public void onItemRangeInserted(int positionStart, int itemCount) {
  74. super.onItemRangeInserted(positionStart, itemCount);
  75. initEmptyView();
  76. }
  77. @Override
  78. public void onItemRangeRemoved(int positionStart, int itemCount) {
  79. super.onItemRangeRemoved(positionStart, itemCount);
  80. initEmptyView();
  81. }
  82. };
  83. public void setHasFooter(boolean bool) {
  84. hasFooter = bool;
  85. }
  86. }