Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / view / search / components / EmptySupportRecyclerView.java
1 package com.moiseum.wolnelektury.view.search.components;
2
3 import android.content.Context;
4 import android.support.v7.widget.RecyclerView;
5 import android.util.AttributeSet;
6 import android.view.View;
7
8 /**
9  * Created by piotrostrowski on 17.07.2017.
10  */
11
12 public class EmptySupportRecyclerView extends RecyclerView {
13         private View emptyView;
14
15         private AdapterDataObserver emptyObserver = new AdapterDataObserver() {
16
17
18                 @Override
19                 public void onChanged() {
20                         RecyclerView.Adapter<?> adapter = getAdapter();
21                         if (adapter != null && emptyView != null) {
22                                 if (adapter.getItemCount() == 0) {
23                                         emptyView.setVisibility(View.VISIBLE);
24                                         EmptySupportRecyclerView.this.setVisibility(View.GONE);
25                                 } else {
26                                         emptyView.setVisibility(View.GONE);
27                                         EmptySupportRecyclerView.this.setVisibility(View.VISIBLE);
28                                 }
29                         }
30
31                 }
32         };
33
34         public EmptySupportRecyclerView(Context context) {
35                 super(context);
36         }
37
38         public EmptySupportRecyclerView(Context context, AttributeSet attrs) {
39                 super(context, attrs);
40         }
41
42         public EmptySupportRecyclerView(Context context, AttributeSet attrs, int defStyle) {
43                 super(context, attrs, defStyle);
44         }
45
46         @Override
47         public void setAdapter(Adapter adapter) {
48                 super.setAdapter(adapter);
49
50                 if (adapter != null) {
51                         adapter.registerAdapterDataObserver(emptyObserver);
52                 }
53
54                 emptyObserver.onChanged();
55         }
56
57         public void setEmptyView(View emptyView) {
58                 this.emptyView = emptyView;
59         }
60 }