4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.takeoffandroid.shimmercontactsview.views.shimmer;
18 |
19 | import android.content.Context;
20 | import android.content.res.TypedArray;
21 | import android.support.annotation.Nullable;
22 | import android.support.v7.widget.GridLayoutManager;
23 | import android.support.v7.widget.LinearLayoutManager;
24 | import android.support.v7.widget.RecyclerView;
25 | import android.util.AttributeSet;
26 |
27 | import com.takeoffandroid.shimmercontactsview.R;
28 |
29 |
30 | /**
31 | * Created by sharish on 22/11/16.
32 | */
33 |
34 | public class ShimmerRecyclerView extends RecyclerView {
35 |
36 | public enum LayoutMangerType {
37 | LINEAR_VERTICAL, LINEAR_HORIZONTAL, GRID
38 | }
39 |
40 | private ShimmerAdapter mShimmerAdapter;
41 | private LayoutManager mShimmerLayoutManager;
42 |
43 | private LayoutManager mActualLayoutManager;
44 | private Adapter mActualAdapter;
45 |
46 |
47 | private int mLayoutReference = R.layout.shimmer_sample_view;
48 | private boolean mCanScroll;
49 | private LayoutMangerType mLayoutMangerType = LayoutMangerType.LINEAR_VERTICAL;
50 | private int mGridCount = 2;
51 |
52 | public ShimmerRecyclerView(Context context) {
53 | super(context);
54 | init(null);
55 | }
56 |
57 | public ShimmerRecyclerView(Context context, @Nullable AttributeSet attrs) {
58 | super(context, attrs);
59 | init(attrs);
60 | }
61 |
62 | public ShimmerRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
63 | super(context, attrs, defStyle);
64 | init(attrs);
65 | }
66 |
67 | private void init(AttributeSet attrs) {
68 |
69 | mShimmerAdapter = new ShimmerAdapter();
70 | TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ShimmerRecyclerView, 0, 0);
71 | try {
72 | if (a.hasValue(R.styleable.ShimmerRecyclerView_demo_layout)) {
73 | setDemoLayoutReference(a.getResourceId(R.styleable.ShimmerRecyclerView_demo_layout, R.layout.shimmer_sample_view));
74 | }
75 |
76 | if (a.hasValue(R.styleable.ShimmerRecyclerView_demo_child_count)) {
77 | setDemoChildCount(a.getInteger(R.styleable.ShimmerRecyclerView_demo_child_count, 1));
78 | }
79 |
80 | if (a.hasValue(R.styleable.ShimmerRecyclerView_demo_layout_manager_type)) {
81 | int value = a.getInteger(R.styleable.ShimmerRecyclerView_demo_layout_manager_type, 0);
82 | switch (value) {
83 | case 1:
84 | setDemoLayoutManager(LayoutMangerType.LINEAR_HORIZONTAL);
85 | break;
86 | case 2:
87 | setDemoLayoutManager(LayoutMangerType.GRID);
88 | break;
89 | case 0:
90 | default:
91 | setDemoLayoutManager(LayoutMangerType.LINEAR_VERTICAL);
92 | break;
93 |
94 | }
95 | }
96 |
97 | if (a.hasValue(R.styleable.ShimmerRecyclerView_demo_grid_child_count)) {
98 | setGridChildCount(a.getInteger(R.styleable.ShimmerRecyclerView_demo_grid_child_count, 2));
99 | }
100 |
101 | } finally {
102 | a.recycle();
103 | }
104 |
105 | showShimmerAdapter();
106 |
107 |
108 | }
109 |
110 | /**
111 | * Specifies the number of child should exist in any row of the grid layout.
112 | *
113 | * @param count - count specifying the number of child.
114 | */
115 | public void setGridChildCount(int count) {
116 | mGridCount = count;
117 | }
118 |
119 | /**
120 | * Sets the layout manager for the shimmer adapter.
121 | *
122 | * @param type layout manager reference
123 | */
124 | public void setDemoLayoutManager(LayoutMangerType type) {
125 | mLayoutMangerType = type;
126 |
127 | }
128 |
129 | /**
130 | * Sets the number of demo views should be shown in the shimmer adapter.
131 | *
132 | * @param count - number of demo views should be shown.
133 | */
134 | public void setDemoChildCount(int count) {
135 | mShimmerAdapter.setMinItemCount(count);
136 | }
137 |
138 | /**
139 | * Sets the shimmer adapter and shows the loading screen.
140 | */
141 | public void showShimmerAdapter() {
142 | mCanScroll = false;
143 |
144 | if (mShimmerLayoutManager == null) {
145 | initShimmerManager();
146 | }
147 |
148 | setLayoutManager(mShimmerLayoutManager);
149 | setAdapter(mShimmerAdapter);
150 |
151 | }
152 |
153 | private void initShimmerManager() {
154 |
155 | switch (mLayoutMangerType) {
156 | case LINEAR_VERTICAL:
157 | mShimmerLayoutManager = new LinearLayoutManager(getContext()) {
158 | public boolean canScrollVertically() {
159 | return mCanScroll;
160 | }
161 | };
162 | break;
163 | case LINEAR_HORIZONTAL:
164 | mShimmerLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false) {
165 | public boolean canScrollHorizontally() {
166 | return mCanScroll;
167 | }
168 | };
169 | break;
170 | case GRID:
171 | mShimmerLayoutManager = new GridLayoutManager(getContext(), mGridCount) {
172 | public boolean canScrollVertically() {
173 | return mCanScroll;
174 | }
175 | };
176 | break;
177 |
178 |
179 | }
180 | }
181 |
182 | /**
183 | * Hides the shimmer adapter
184 | */
185 | public void hideShimmerAdapter() {
186 | mCanScroll = true;
187 | setLayoutManager(mActualLayoutManager);
188 | setAdapter(mActualAdapter);
189 | }
190 |
191 |
192 | public void setLayoutManager(LayoutManager manager) {
193 |
194 | if (manager == null) {
195 | mActualLayoutManager = null;
196 | } else if (manager != mShimmerLayoutManager) {
197 | mActualLayoutManager = manager;
198 | }
199 |
200 | super.setLayoutManager(manager);
201 | }
202 |
203 |
204 | public void setAdapter(Adapter adapter) {
205 |
206 | if (adapter == null) {
207 | mActualAdapter = null;
208 | } else if (adapter != mShimmerAdapter) {
209 | mActualAdapter = adapter;
210 | }
211 |
212 | super.setAdapter(adapter);
213 |
214 | }
215 |
216 |
217 | public int getLayoutReference() {
218 | return mLayoutReference;
219 | }
220 |
221 | /**
222 | * Sets the demo layout reference
223 | *
224 | * @param mLayoutReference layout resource id of the layout which should be shown as demo.
225 | */
226 | public void setDemoLayoutReference(int mLayoutReference) {
227 | this.mLayoutReference = mLayoutReference;
228 | mShimmerAdapter.setLayoutReference(getLayoutReference());
229 | }
230 | }
231 |
--------------------------------------------------------------------------------
/app/src/main/java/com/takeoffandroid/shimmercontactsview/views/shimmer/ShimmerViewHolder.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | * Copyright 2017 Harish Sridharan
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.takeoffandroid.shimmercontactsview.views.shimmer;
19 |
20 | import android.support.v7.widget.RecyclerView;
21 | import android.view.LayoutInflater;
22 | import android.view.View;
23 | import android.view.ViewGroup;
24 |
25 | import com.takeoffandroid.shimmercontactsview.R;
26 |
27 |
28 | /**
29 | * Created by sharish on 22/11/16.
30 | */
31 |
32 | public class ShimmerViewHolder extends RecyclerView.ViewHolder {
33 |
34 | public ShimmerViewHolder(LayoutInflater inflater, ViewGroup parent, int innerViewResId) {
35 | super(inflater.inflate(R.layout.lib_shimmer_viewholder, parent, false));
36 | ShimmerFrameLayout layout = (ShimmerFrameLayout) itemView;
37 |
38 | View innerView = inflater.inflate(innerViewResId, layout, false);
39 | layout.addView(innerView);
40 | layout.setAutoStart(false);
41 | }
42 |
43 | /**
44 | * Binds the view
45 | */
46 | public void bind() {
47 |
48 | ShimmerFrameLayout layout = (ShimmerFrameLayout) itemView;
49 | layout.startShimmerAnimation();
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/java/com/takeoffandroid/shimmercontactsview/views/typefacehelper/DialogUtils.java:
--------------------------------------------------------------------------------
1 | package com.takeoffandroid.shimmercontactsview.views.typefacehelper;
2 |
3 | import android.app.AlertDialog;
4 | import android.app.Dialog;
5 | import android.app.ProgressDialog;
6 | import android.content.DialogInterface;
7 | import android.widget.Button;
8 | import android.widget.TextView;
9 |
10 | final class DialogUtils {
11 | public static