-
Notifications
You must be signed in to change notification settings - Fork 451
/
RecyclerViewSkeletonScreen.java
147 lines (128 loc) · 4.87 KB
/
RecyclerViewSkeletonScreen.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
138
139
140
141
142
143
144
145
146
147
package com.ethanhua.skeleton;
import android.support.annotation.ArrayRes;
import android.support.annotation.ColorRes;
import android.support.annotation.IntRange;
import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
/**
* Created by ethanhua on 2017/7/29.
*/
public class RecyclerViewSkeletonScreen implements SkeletonScreen {
private final RecyclerView mRecyclerView;
private final RecyclerView.Adapter mActualAdapter;
private final SkeletonAdapter mSkeletonAdapter;
private final boolean mRecyclerViewFrozen;
private RecyclerViewSkeletonScreen(Builder builder) {
mRecyclerView = builder.mRecyclerView;
mActualAdapter = builder.mActualAdapter;
mSkeletonAdapter = new SkeletonAdapter();
mSkeletonAdapter.setItemCount(builder.mItemCount);
mSkeletonAdapter.setLayoutReference(builder.mItemResID);
mSkeletonAdapter.setArrayOfLayoutReferences(builder.mItemsResIDArray);
mSkeletonAdapter.shimmer(builder.mShimmer);
mSkeletonAdapter.setShimmerColor(builder.mShimmerColor);
mSkeletonAdapter.setShimmerAngle(builder.mShimmerAngle);
mSkeletonAdapter.setShimmerDuration(builder.mShimmerDuration);
mRecyclerViewFrozen = builder.mFrozen;
}
@Override
public void show() {
mRecyclerView.setAdapter(mSkeletonAdapter);
if (!mRecyclerView.isComputingLayout() && mRecyclerViewFrozen) {
mRecyclerView.setLayoutFrozen(true);
}
}
@Override
public void hide() {
mRecyclerView.setAdapter(mActualAdapter);
}
public static class Builder {
private RecyclerView.Adapter mActualAdapter;
private final RecyclerView mRecyclerView;
private boolean mShimmer = true;
private int mItemCount = 10;
private int mItemResID = R.layout.layout_default_item_skeleton;
private int[] mItemsResIDArray;
private int mShimmerColor;
private int mShimmerDuration = 1000;
private int mShimmerAngle = 20;
private boolean mFrozen = true;
public Builder(RecyclerView recyclerView) {
this.mRecyclerView = recyclerView;
this.mShimmerColor = ContextCompat.getColor(recyclerView.getContext(), R.color.shimmer_color);
}
/**
* @param adapter the target recyclerView actual adapter
*/
public Builder adapter(RecyclerView.Adapter adapter) {
this.mActualAdapter = adapter;
return this;
}
/**
* @param itemCount the child item count in recyclerView
*/
public Builder count(int itemCount) {
this.mItemCount = itemCount;
return this;
}
/**
* @param shimmer whether show shimmer animation
*/
public Builder shimmer(boolean shimmer) {
this.mShimmer = shimmer;
return this;
}
/**
* the duration of the animation , the time it will take for the highlight to move from one end of the layout
* to the other.
*
* @param shimmerDuration Duration of the shimmer animation, in milliseconds
*/
public Builder duration(int shimmerDuration) {
this.mShimmerDuration = shimmerDuration;
return this;
}
/**
* @param shimmerColor the shimmer color
*/
public Builder color(@ColorRes int shimmerColor) {
this.mShimmerColor = ContextCompat.getColor(mRecyclerView.getContext(), shimmerColor);
return this;
}
/**
* @param shimmerAngle the angle of the shimmer effect in clockwise direction in degrees.
*/
public Builder angle(@IntRange(from = 0, to = 30) int shimmerAngle) {
this.mShimmerAngle = shimmerAngle;
return this;
}
/**
* @param skeletonLayoutResID the loading skeleton layoutResID
*/
public Builder load(@LayoutRes int skeletonLayoutResID) {
this.mItemResID = skeletonLayoutResID;
return this;
}
/**
* @param skeletonLayoutResIDs the loading array of skeleton layoutResID
*/
public Builder loadArrayOfLayouts(@ArrayRes int[] skeletonLayoutResIDs) {
this.mItemsResIDArray = skeletonLayoutResIDs;
return this;
}
/**
* @param frozen whether frozen recyclerView during skeleton showing
* @return
*/
public Builder frozen(boolean frozen) {
this.mFrozen = frozen;
return this;
}
public RecyclerViewSkeletonScreen show() {
RecyclerViewSkeletonScreen recyclerViewSkeleton = new RecyclerViewSkeletonScreen(this);
recyclerViewSkeleton.show();
return recyclerViewSkeleton;
}
}
}