/
CircleRecyclerView.java
291 lines (244 loc) · 9.14 KB
/
CircleRecyclerView.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package me.khrystal.library.widget;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import java.lang.ref.WeakReference;
import me.khrystal.library.R;
/**
* usage:
* author: kHRYSTAL
* create time: 16/9/14
* update time:
* email: 723526676@qq.com
*/
public class CircleRecyclerView extends RecyclerView implements View.OnClickListener {
private static final String TAG = "CircleRecyclerView";
private static final int DEFAULT_SELECTION = Integer.MAX_VALUE >> 1;
private boolean mIsForceCentering;
private final CenterRunnable mCenterRunnable = new CenterRunnable();
private ItemViewMode mViewMode;
private boolean mNeedCenterForce;
private boolean mNeedLoop = true;
private OnCenterItemClickListener mCenterItemClickListener;
private View mCurrentCenterChildView;
private OnScrollListener mOnScrollListener;
private boolean mFirstOnLayout;
private boolean mFirstSetAdapter = true;
private Handler mPostHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
scrollToPosition(DEFAULT_SELECTION);
}
};
public CircleRecyclerView(Context context) {
this(context, null);
}
public CircleRecyclerView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOverScrollMode(OVER_SCROLL_NEVER);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (mNeedLoop) {
// scrollToPosition Notwork use delay
if (!mFirstOnLayout) {
mFirstOnLayout = true;
mPostHandler.sendEmptyMessage(0);
}
// scrollToPosition(DEFAULT_SELECTION);
mCurrentCenterChildView = findViewAtCenter();
smoothScrollToView(mCurrentCenterChildView);
} else if (!mNeedLoop && mNeedCenterForce) {
LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
if (layoutManager.canScrollHorizontally())
setPadding(getWidth() / 2, 0, getWidth() / 2, 0);
else if (layoutManager.canScrollVertically())
setPadding(0, getHeight() / 2, 0, getHeight() / 2);
setClipToPadding(false);
setClipChildren(false);
mCurrentCenterChildView = findViewAtCenter();
smoothScrollToView(mCurrentCenterChildView);
} else {
setClipToPadding(false);
setClipChildren(false);
}
if (mCurrentCenterChildView != null)
mCurrentCenterChildView.setOnClickListener(this);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mViewMode != null) {
final int count = getChildCount();
for (int i = 0; i < count; ++i) {
View v = getChildAt(i);
if (v != mCurrentCenterChildView && mCenterItemClickListener != null)
v.setOnClickListener(null);
if (v == mCurrentCenterChildView)
v.setTag(R.string.tag_is_center, true);
else
v.setTag(R.string.tag_is_center, false);
mViewMode.applyToView(v, this);
}
}
if (mOnScrollListener != null)
mOnScrollListener.onScrollChanged(l, t, oldl, oldt);
}
@Override
public void requestLayout() {
super.requestLayout();
if (mViewMode != null && getLayoutManager() != null) {
int count = getLayoutManager().getChildCount();
for (int i = 0; i < count; ++i) {
View v = getChildAt(i);
if (v != mCurrentCenterChildView && mCenterItemClickListener != null)
v.setOnClickListener(null);
if (v == mCurrentCenterChildView)
v.setTag(R.string.tag_is_center, true);
else
v.setTag(R.string.tag_is_center, false);
mViewMode.applyToView(v, this);
}
}
}
public void smoothScrollToView(View v) {
int distance = 0;
if (getLayoutManager() instanceof LinearLayoutManager) {
if (getLayoutManager().canScrollVertically()) {
final float y = v.getY() + v.getHeight() * 0.5f;
final float halfHeight = getHeight() * 0.5f;
distance = (int) (y - halfHeight);
} else if (getLayoutManager().canScrollHorizontally()) {
final float x = v.getX() + v.getWidth() * 0.5f;
final float halfWidth = getWidth() * 0.5f;
distance = (int) (x - halfWidth);
}
} else
throw new IllegalArgumentException("CircleRecyclerView just support T extend LinearLayoutManager!");
smoothScrollBy(distance,distance);
}
@Override
public void onScrolled(int dx, int dy) {
super.onScrolled(dx, dy);
if (mOnScrollListener != null)
mOnScrollListener.onScrolled(dx, dy);
}
@Override
public void onScrollStateChanged(int state) {
if (state == SCROLL_STATE_IDLE) {
if (mNeedCenterForce && !mIsForceCentering) {
mIsForceCentering = true;
mCurrentCenterChildView = findViewAtCenter();
if (mCurrentCenterChildView != null && mCenterItemClickListener != null)
mCurrentCenterChildView.setOnClickListener(this);
mCenterRunnable.setView(mCurrentCenterChildView);
ViewCompat.postOnAnimation(this, mCenterRunnable);
}
}
if (mOnScrollListener != null)
mOnScrollListener.onScrollStateChanged(state);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
removeCallbacks(mCenterRunnable);
mIsForceCentering = false;
return super.onTouchEvent(e);
}
public View findViewAt(int x, int y) {
final int count = getChildCount();
for (int i = 0; i < count; ++i) {
final View v = getChildAt(i);
final int x0 = v.getLeft();
final int y0 = v.getTop();
final int x1 = v.getWidth() + x0;
final int y1 = v.getHeight() + y0;
if (x >= x0 && x <= x1 && y >= y0 && y <= y1) {
return v;
}
}
return null;
}
public View findViewAtCenter() {
if (getLayoutManager().canScrollVertically()) {
return findViewAt(0, getHeight() / 2);
}else if (getLayoutManager().canScrollHorizontally()) {
return findViewAt(getWidth() / 2, 0);
}
return null;
}
@Override
public void onClick(View v) {
if (mCenterItemClickListener != null)
mCenterItemClickListener.onCenterItemClick(v);
}
public class CenterRunnable implements Runnable {
private WeakReference<View> mView;
public void setView(View v) {
mView = new WeakReference<View>(v);
}
@Override
public void run() {
smoothScrollToView(mView.get());
if (mNeedCenterForce)
mIsForceCentering = true;
}
}
public void setNeedCenterForce(boolean needCenterForce) {
mNeedCenterForce = needCenterForce;
}
/**
* default needLoop is true
* if not needLoop && centerForce
* will setPadding your layoutManger direction half width or height
* and setClipPadding(false), setClipChildren(false)
* @param needLoop default true
*/
public void setNeedLoop(boolean needLoop) {
mNeedLoop = needLoop;
}
/**
* set the center item clickListener
*
* @param listener
*/
public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
mCenterItemClickListener = listener;
}
public void setViewMode(ItemViewMode mode) {
mViewMode = mode;
}
public void setOnScrollListener(OnScrollListener listener) {
mOnScrollListener = listener;
}
public interface OnCenterItemClickListener {
void onCenterItemClick(View v);
}
public interface OnScrollListener {
void onScrollChanged(int l, int t, int oldl, int oldt);
void onScrollStateChanged(int state);
void onScrolled(int dx, int dy);
}
@Override
public void setAdapter(Adapter adapter) {
super.setAdapter(adapter);
if (mFirstSetAdapter) {
mFirstSetAdapter = false;
} else {
if (adapter != null && mNeedCenterForce)
mPostHandler.sendEmptyMessage(0);
}
}
}