Skip to content

Commit 07c967c

Browse files
committed
Improve alternative for implement start shimmer
1 parent 38ca02d commit 07c967c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

app/src/main/java/com/ramanaptr/sample/MainActivity.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class MainActivity : AppCompatActivity() {
107107
// if you want use view binding you should to cast the object like the example below
108108
rvSample = binding.rvSample as EzRecyclerView<SampleData>
109109

110+
// set empty object like example "SampleData" when you use for shimmer effect, to avoid exception
111+
rvSample.setData(SampleData())
112+
110113
// set example function for pagination on Ez-RecyclerView
111114
// init the pagination after bind the view and declare it into field
112115
initPaginationEzRecyclerView()
@@ -133,6 +136,9 @@ class MainActivity : AppCompatActivity() {
133136
// if you want use view binding you should to cast the object like the example below
134137
rvSample = binding.rvSample as EzRecyclerView<SampleData>
135138

139+
// set empty object like example "SampleData" when you use for shimmer effect, to avoid exception
140+
rvSample.setData(SampleData())
141+
136142
// example function for pagination on Ez-RecyclerView
137143
// init the pagination after bind the view and declare it into field
138144
initPaginationEzRecyclerView()
@@ -178,7 +184,8 @@ class MainActivity : AppCompatActivity() {
178184
private fun exampleDataForSingleLayout(size: Int) {
179185
// start shimmer when load the data
180186
// please to use your empty object like SampleData()
181-
rvSample.startShimmer(size, SampleData())
187+
// rvSample.startShimmer(size, SampleData()) // Alternative
188+
rvSample.startShimmer(size)
182189

183190
// start load the data
184191
subscribe = Flowable.create<List<SampleData>>({
@@ -215,7 +222,8 @@ class MainActivity : AppCompatActivity() {
215222
private fun exampleDataForMultipleLayout(size: Int) {
216223
// start shimmer when load the data
217224
// please to use your empty object like SampleData()
218-
rvSample.startShimmer(size, SampleData())
225+
// rvSample.startShimmer(size, SampleData()) // Alternative
226+
rvSample.startShimmer(size)
219227

220228
// start load the data
221229
subscribe = Flowable.create<List<SampleData>>({

ez-recyleview/src/main/java/com/ramanaptr/widget/EzRecyclerView.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class EzRecyclerView<Data extends EzBaseData> extends RecyclerView {
4444
private int offset = 0;
4545
private int limit = 0;
4646
private int currentPage = 0;
47+
private Data data;
4748

4849
public EzRecyclerView(@NonNull Context context) {
4950
super(context);
@@ -76,6 +77,7 @@ public void destroy() {
7677
ezPaginationListener = null;
7778
baseAdapter = null;
7879
listener = null;
80+
data = null;
7981
tempShimmerSize = 0;
8082
startShimmerSize = 0;
8183
endShimmerSize = 0;
@@ -84,6 +86,10 @@ public void destroy() {
8486
currentPage = 0;
8587
}
8688

89+
public void setData(Data data) {
90+
this.data = data;
91+
}
92+
8793
public void setViewHolderLayout(@NonNull Listener<Data> listener) {
8894
this.listener = listener;
8995
baseAdapter.setListener(listener::setDataOnViewHolder);
@@ -140,12 +146,32 @@ public void setFlexBoxLayoutManager(@FlexDirection int flexDirection) {
140146
setLayoutManager(flexboxLayoutManager);
141147
}
142148

149+
public void startShimmer(int shimmerSize) {
150+
if (data == null) {
151+
throw new IllegalArgumentException("Please construct your empty object extended from EzBaseData into the function #setData(data) or you can use #startShimmer(size, data)");
152+
}
153+
if (isFirstLoadEzRecyclerView) {
154+
this.startShimmerSize = baseAdapter.getItemCount();
155+
this.endShimmerSize = shimmerSize;
156+
this.tempShimmerSize = shimmerSize;
157+
final List<Data> shimmer = new ArrayList<>();
158+
for (int i = 0; i < shimmerSize; i++) {
159+
data.setEzViewType(EzViewType.SHIMMER_EFFECT);
160+
shimmer.add(data);
161+
}
162+
addAll(shimmer);
163+
flagEzRecyclerViewFirstLoadDone();
164+
flagOnLoading();
165+
}
166+
}
167+
143168
public void startShimmer(int shimmerSize, Data data) {
144169
if (isFirstLoadEzRecyclerView) {
170+
this.data = data;
145171
this.startShimmerSize = baseAdapter.getItemCount();
146172
this.endShimmerSize = shimmerSize;
147173
this.tempShimmerSize = shimmerSize;
148-
List<Data> shimmer = new ArrayList<>();
174+
final List<Data> shimmer = new ArrayList<>();
149175
for (int i = 0; i < shimmerSize; i++) {
150176
data.setEzViewType(EzViewType.SHIMMER_EFFECT);
151177
shimmer.add(data);

0 commit comments

Comments
 (0)