This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMjolnirRecyclerView.java
137 lines (116 loc) · 4.59 KB
/
MjolnirRecyclerView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package co.infinum.mjolnirrecyclerview;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
/**
* Custom implementation of RecyclerView, which has support for Empty view.
*
* Created by Željko Plesac on 27/09/16.
*/
public class MjolnirRecyclerView extends RecyclerView {
private View emptyView;
private boolean showEmptyViewIfAdapterNotSet = false;
/**
* Custom AdapterDataObserver observer, which has two functions:
* 1. Update the empty view visibility depending on adapter's content.
* 2. Update the loading state of adapter, depending on whether a change of adapter's content has been detected.
*/
private final AdapterDataObserver observer = new AdapterDataObserver() {
@Override
public void onChanged() {
updateState();
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
updateState();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
updateState();
}
private void updateState() {
checkIfEmpty();
((MjolnirRecyclerAdapter) getAdapter()).setLoading(false);
}
};
public MjolnirRecyclerView(Context context) {
super(context);
}
public MjolnirRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MjolnirRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Checks if the adapter collection is empty. If it is, hide the RecyclerView content and show the empty view.
*/
private void checkIfEmpty() {
if (emptyView != null) {
if (getAdapter() == null && showEmptyViewIfAdapterNotSet) {
emptyView.setVisibility(View.VISIBLE);
setVisibility(View.GONE);
} else if (getAdapter() != null) {
final boolean emptyViewVisible = ((MjolnirRecyclerAdapter) getAdapter()).getCollectionCount() == 0;
emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
setVisibility(emptyViewVisible ? GONE : VISIBLE);
}
}
}
@Override
public void setLayoutManager(LayoutManager layout) {
super.setLayoutManager(layout);
if (getAdapter() != null && getAdapter() instanceof MjolnirRecyclerAdapter
&& ((MjolnirRecyclerAdapter) getAdapter()).getLayoutManager() == null) {
((MjolnirRecyclerAdapter) getAdapter()).setLayoutManager(getLayoutManager());
}
}
/**
* Sets the RecyclerView's adapter and registers adapter data observer, which is used to update empty view's visibility and loading
* state of the adapter.
*/
@Override
public void setAdapter(Adapter adapter) {
final Adapter oldAdapter = getAdapter();
if (oldAdapter != null) {
oldAdapter.unregisterAdapterDataObserver(observer);
}
super.setAdapter(adapter);
if (adapter != null) {
adapter.registerAdapterDataObserver(observer);
}
if (adapter instanceof MjolnirRecyclerAdapter && ((MjolnirRecyclerAdapter) getAdapter()).getLayoutManager() == null) {
((MjolnirRecyclerAdapter) getAdapter()).setLayoutManager(getLayoutManager());
}
checkIfEmpty();
}
/**
* Sets the empty view. RecyclerView can have only one empty view at the time.
*
* @param emptyView view which is used as RecyclerView's empty view.
*/
public void setEmptyView(View emptyView) {
setEmptyView(emptyView, false);
}
/**
* Sets the empty view to the RecyclerView. {@param showIfAdapterNotSet} determines should we show the empty view if adapter is
* still not set to the RecyclerView. Default value is set to false.
*
* @param emptyView view which is used as RecyclerView's empty view.
* @param showIfAdapterNotSet determines should we show empty view if adapter is still not set to the RecyclerView.
*/
public void setEmptyView(View emptyView, boolean showIfAdapterNotSet) {
this.emptyView = emptyView;
this.showEmptyViewIfAdapterNotSet = showIfAdapterNotSet;
checkIfEmpty();
}
/**
* Checks whether the RecyclerView is showing empty view.
*
* @return true is empty view is shown, false otherwise.
*/
public boolean isEmptyViewShown() {
return emptyView != null && emptyView.getVisibility() == VISIBLE;
}
}