This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
/
WXListComponent.java
268 lines (235 loc) · 9.85 KB
/
WXListComponent.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.taobao.weex.ui.component.list;
import android.content.Context;
import android.util.Pair;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.annotation.Component;
import com.taobao.weex.common.Constants;
import com.taobao.weex.common.WXThread;
import com.taobao.weex.dom.WXDomObject;
import com.taobao.weex.dom.WXRecyclerDomObject;
import com.taobao.weex.dom.flex.Spacing;
import com.taobao.weex.ui.component.WXBaseRefresh;
import com.taobao.weex.ui.component.WXBasicComponentType;
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.ui.component.WXComponentProp;
import com.taobao.weex.ui.component.WXLoading;
import com.taobao.weex.ui.component.WXRefresh;
import com.taobao.weex.ui.component.WXVContainer;
import com.taobao.weex.ui.view.listview.WXRecyclerView;
import com.taobao.weex.ui.view.listview.adapter.ListBaseViewHolder;
import com.taobao.weex.ui.view.refresh.wrapper.BounceRecyclerView;
import com.taobao.weex.utils.WXUtils;
import java.util.Map;
/**
* Unlike other components, there is immutable bi-directional association between View and
* ViewHolder, while only mutable and temporal uni-directional association between view and
* components. The association only exist from {@link #onBindViewHolder(ListBaseViewHolder, int)} to
* {@link #onViewRecycled(ListBaseViewHolder)}. In other situations, the association may not valid
* or not even exist.
*/
@Component(lazyload = false)
public class WXListComponent extends BasicListComponent<BounceRecyclerView> {
private String TAG = "WXListComponent";
private WXRecyclerDomObject mRecyclerDom;
private float mPaddingLeft;
private float mPaddingRight;
@Deprecated
public WXListComponent(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, String instanceId, boolean isLazy) {
this(instance, dom, parent, isLazy);
}
public WXListComponent(WXSDKInstance instance, WXDomObject node, WXVContainer parent, boolean lazy) {
super(instance, node, parent);
if (node != null && node instanceof WXRecyclerDomObject) {
mRecyclerDom = (WXRecyclerDomObject) node;
mRecyclerDom.preCalculateCellWidth();
if(WXBasicComponentType.WATERFALL.equals(node.getType())){
mLayoutType = WXRecyclerView.TYPE_STAGGERED_GRID_LAYOUT;
}else{
mLayoutType = mRecyclerDom.getLayoutType();
}
updateRecyclerAttr();
}
}
@Override
protected BounceRecyclerView generateListView(Context context, int orientation) {
BounceRecyclerView bounceRecyclerView = new BounceRecyclerView(context,mLayoutType,mColumnCount,mColumnGap,orientation);
if(bounceRecyclerView.getSwipeLayout() != null){
if(WXUtils.getBoolean(getDomObject().getAttrs().get(Constants.Name.NEST_SCROLLING_ENABLED), false)) {
bounceRecyclerView.getSwipeLayout().setNestedScrollingEnabled(true);
}
}
if(mRecyclerDom != null && mRecyclerDom.getSpanOffsets() != null){
bounceRecyclerView.getInnerView().addItemDecoration(new GapItemDecoration(this));
}
return bounceRecyclerView;
}
@Override
public void addChild(final WXComponent child, int index) {
super.addChild(child, index);
if (child == null || index < -1) {
return;
}
if (child instanceof WXRefresh && getHostView() != null) {
getHostView().setOnRefreshListener((WXRefresh) child);
getHostView().postDelayed(WXThread.secure(new Runnable() {
@Override
public void run() {
getHostView().setHeaderView(child);
}
}), 100);
}
if (child instanceof WXLoading && getHostView() != null) {
getHostView().setOnLoadingListener((WXLoading) child);
getHostView().postDelayed(WXThread.secure(new Runnable() {
@Override
public void run() {
getHostView().setFooterView(child);
}
}), 100);
}
// Synchronize DomObject's attr to Component and Native View
if(mRecyclerDom != null && getHostView() != null && (mColumnWidth != mRecyclerDom.getColumnWidth() ||
mColumnCount != mRecyclerDom.getColumnCount() ||
mColumnGap != mRecyclerDom.getColumnGap())) {
updateRecyclerAttr();
getHostView().getInnerView().initView(getContext(), mLayoutType,mColumnCount,mColumnGap,getOrientation());
}
}
private void updateRecyclerAttr(){
if(mRecyclerDom != null) {
mColumnCount = mRecyclerDom.getColumnCount();
mColumnGap = mRecyclerDom.getColumnGap();
mColumnWidth = mRecyclerDom.getColumnWidth();
mPaddingLeft = mRecyclerDom.getPadding().get(Spacing.LEFT);
mPaddingRight = mRecyclerDom.getPadding().get(Spacing.RIGHT);
mLeftGap = mRecyclerDom.getLeftGap();
mRightGap = mRecyclerDom.getRightGap();
mRecyclerDom.preCalculateCellWidth();
}
}
@WXComponentProp(name = Constants.Name.LEFT_GAP)
public void setLeftGap(float leftGap) {
if(mRecyclerDom != null && mRecyclerDom.getLeftGap() != mLeftGap){
markComponentUsable();
mRecyclerDom.preCalculateCellWidth();
updateRecyclerAttr();
WXRecyclerView wxRecyclerView = getHostView().getInnerView();
wxRecyclerView.initView(getContext(), mLayoutType,mColumnCount,mColumnGap,getOrientation());
}
}
@WXComponentProp(name = Constants.Name.RIGHT_GAP)
public void setRightGap(float rightGap) {
if(mRecyclerDom != null && mRecyclerDom.getRightGap() != mRightGap){
markComponentUsable();
mRecyclerDom.preCalculateCellWidth();
updateRecyclerAttr();
WXRecyclerView wxRecyclerView = getHostView().getInnerView();
wxRecyclerView.initView(getContext(), mLayoutType,mColumnCount,mColumnGap,getOrientation());
}
}
@WXComponentProp(name = Constants.Name.COLUMN_WIDTH)
public void setColumnWidth(int columnCount) {
if(mRecyclerDom != null && mRecyclerDom.getColumnWidth() != mColumnWidth){
markComponentUsable();
updateRecyclerAttr();
WXRecyclerView wxRecyclerView = getHostView().getInnerView();
wxRecyclerView.initView(getContext(), mLayoutType,mColumnCount,mColumnGap,getOrientation());
}
}
@WXComponentProp(name = Constants.Name.COLUMN_COUNT)
public void setColumnCount(int columnCount){
if(mRecyclerDom != null && mRecyclerDom.getColumnCount() != mColumnCount){
markComponentUsable();
updateRecyclerAttr();
WXRecyclerView wxRecyclerView = getHostView().getInnerView();
wxRecyclerView.initView(getContext(), mLayoutType,mColumnCount,mColumnGap,getOrientation());
}
}
@WXComponentProp(name = Constants.Name.COLUMN_GAP)
public void setColumnGap(float columnGap) throws InterruptedException {
if(mRecyclerDom != null && mRecyclerDom.getColumnGap() != mColumnGap) {
markComponentUsable();
updateRecyclerAttr();
WXRecyclerView wxRecyclerView = getHostView().getInnerView();
wxRecyclerView.initView(getContext(), mLayoutType, mColumnCount, mColumnGap, getOrientation());
}
}
@WXComponentProp(name = Constants.Name.SCROLLABLE)
public void setScrollable(boolean scrollable) {
WXRecyclerView inner = getHostView().getInnerView();
inner.setScrollable(scrollable);
}
@Override
public void updateProperties(Map<String, Object> props) {
super.updateProperties(props);
if(props.containsKey(Constants.Name.PADDING)
||props.containsKey(Constants.Name.PADDING_LEFT)
|| props.containsKey(Constants.Name.PADDING_RIGHT)){
if(mRecyclerDom != null && (mPaddingLeft != mRecyclerDom.getPadding().get(Spacing.LEFT)
|| mPaddingRight != mRecyclerDom.getPadding().get(Spacing.RIGHT))) {
markComponentUsable();
updateRecyclerAttr();
WXRecyclerView wxRecyclerView = getHostView().getInnerView();
wxRecyclerView.initView(getContext(), mLayoutType, mColumnCount, mColumnGap, getOrientation());
}
}
}
@Override
public void createChildViewAt(int index) {
Pair<WXComponent, Integer> ret = rearrangeIndexAndGetChild(index);
if(ret.first != null) {
final WXComponent child = getChild(ret.second);
if (child instanceof WXBaseRefresh) {
child.createView();
if (child instanceof WXRefresh) {
getHostView().setOnRefreshListener((WXRefresh) child);
getHostView().postDelayed(WXThread.secure(new Runnable() {
@Override
public void run() {
getHostView().setHeaderView(child);
}
}), 100);
} else if (child instanceof WXLoading) {
getHostView().setOnLoadingListener((WXLoading) child);
getHostView().postDelayed(WXThread.secure(new Runnable() {
@Override
public void run() {
getHostView().setFooterView(child);
}
}), 100);
}
} else {
super.createChildViewAt(ret.second);
}
}
}
public WXRecyclerDomObject getRecyclerDom() {
return mRecyclerDom;
}
public void remove(WXComponent child, boolean destroy) {
super.remove(child, destroy);
if (child instanceof WXLoading) {
getHostView().removeFooterView(child);
} else if (child instanceof WXRefresh) {
getHostView().removeHeaderView(child);
}
}
}