-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathInternalAbstract.java
More file actions
199 lines (177 loc) · 7.7 KB
/
InternalAbstract.java
File metadata and controls
199 lines (177 loc) · 7.7 KB
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
package com.scwang.smartrefresh.layout.internal;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshInternal;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.impl.RefreshFooterWrapper;
import com.scwang.smartrefresh.layout.impl.RefreshHeaderWrapper;
import com.scwang.smartrefresh.layout.listener.OnStateChangedListener;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
/**
* Internal 初步实现
* 实现 Header 和 Footer 时,继承 InternalAbstract 的话可以少写很多接口方法
* Created by scwang on 2018/2/6.
*/
public abstract class InternalAbstract extends RelativeLayout implements RefreshInternal {
protected View mWrappedView;
protected SpinnerStyle mSpinnerStyle;
protected RefreshInternal mWrappedInternal;
protected boolean mIsAutoRefresh;
protected InternalAbstract(@NonNull View wrapped) {
this(wrapped, wrapped instanceof RefreshInternal ? (RefreshInternal) wrapped : null);
}
protected InternalAbstract(@NonNull View wrappedView, @Nullable RefreshInternal wrappedInternal) {
super(wrappedView.getContext(), null, 0);
this.mWrappedView = wrappedView;
this.mWrappedInternal = wrappedInternal;
if (this instanceof RefreshFooterWrapper && mWrappedInternal instanceof RefreshHeader && mWrappedInternal.getSpinnerStyle() == SpinnerStyle.MatchLayout) {
wrappedInternal.getView().setScaleY(-1);
} else if (this instanceof RefreshHeaderWrapper && mWrappedInternal instanceof RefreshFooter && mWrappedInternal.getSpinnerStyle() == SpinnerStyle.MatchLayout) {
wrappedInternal.getView().setScaleY(-1);
}
}
protected InternalAbstract(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean equals(Object obj) {
if (!super.equals(obj)) {
if (obj instanceof RefreshInternal) {
final RefreshInternal thisView = this;
return thisView.getView() == ((RefreshInternal)obj).getView();
}
return false;
}
return true;
}
@NonNull
public View getView() {
return mWrappedView == null ? this : mWrappedView;
}
@Override
public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
if (mWrappedInternal != null && mWrappedInternal != this) {
return mWrappedInternal.onFinish(refreshLayout, success);
}
return 0;
}
@Override
public void setPrimaryColors(@ColorInt int ... colors) {
if (mWrappedInternal != null && mWrappedInternal != this) {
mWrappedInternal.setPrimaryColors(colors);
}
}
@NonNull
@Override
public SpinnerStyle getSpinnerStyle() {
if (mSpinnerStyle != null) {
return mSpinnerStyle;
}
if (mWrappedInternal != null && mWrappedInternal != this) {
return mWrappedInternal.getSpinnerStyle();
}
if (mWrappedView != null) {
ViewGroup.LayoutParams params = mWrappedView.getLayoutParams();
if (params instanceof SmartRefreshLayout.LayoutParams) {
mSpinnerStyle = ((SmartRefreshLayout.LayoutParams) params).spinnerStyle;
if (mSpinnerStyle != null) {
return mSpinnerStyle;
}
}
if (params != null) {
if (params.height == 0 || params.height == MATCH_PARENT) {
for (SpinnerStyle style : SpinnerStyle.values) {
if (style.scale) {
return mSpinnerStyle = style;
}
}
}
}
}
return mSpinnerStyle = SpinnerStyle.Translate;
}
@Override
public void onAutoRefresh(boolean isAutoRefresh) {
mIsAutoRefresh = isAutoRefresh;
}
@Override
public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
if (mWrappedInternal != null && mWrappedInternal != this) {
mWrappedInternal.onInitialized(kernel, height, maxDragHeight);
} else if (mWrappedView != null) {
ViewGroup.LayoutParams params = mWrappedView.getLayoutParams();
if (params instanceof SmartRefreshLayout.LayoutParams) {
kernel.requestDrawBackgroundFor(this, ((SmartRefreshLayout.LayoutParams) params).backgroundColor);
}
}
}
@Override
public boolean isSupportHorizontalDrag() {
return mWrappedInternal != null && mWrappedInternal != this && mWrappedInternal.isSupportHorizontalDrag();
}
@Override
public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
if (mWrappedInternal != null && mWrappedInternal != this) {
mWrappedInternal.onHorizontalDrag(percentX, offsetX, offsetMax);
}
}
@Override
public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
if (mWrappedInternal != null && mWrappedInternal != this) {
mWrappedInternal.onMoving(isDragging, percent, offset, height, maxDragHeight);
}
}
@Override
public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
if (mWrappedInternal != null && mWrappedInternal != this) {
mWrappedInternal.onReleased(refreshLayout, height, maxDragHeight);
}
}
@Override
public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
if (mWrappedInternal != null && mWrappedInternal != this) {
mWrappedInternal.onStartAnimator(refreshLayout, height, maxDragHeight);
}
}
@Override
public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
if (mWrappedInternal != null && mWrappedInternal != this) {
if (this instanceof RefreshFooterWrapper && mWrappedInternal instanceof RefreshHeader) {
if (oldState.isFooter) {
oldState = oldState.toHeader();
}
if (newState.isFooter) {
newState = newState.toHeader();
}
} else if (this instanceof RefreshHeaderWrapper && mWrappedInternal instanceof RefreshFooter) {
if (oldState.isHeader) {
oldState = oldState.toFooter();
}
if (newState.isHeader) {
newState = newState.toFooter();
}
}
final OnStateChangedListener listener = mWrappedInternal;
if (listener != null) {
listener.onStateChanged(refreshLayout, oldState, newState);
}
}
}
@SuppressLint("RestrictedApi")
public boolean setNoMoreData(boolean noMoreData) {
return mWrappedInternal instanceof RefreshFooter && ((RefreshFooter) mWrappedInternal).setNoMoreData(noMoreData);
}
}