-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
TiListView.java
1125 lines (1009 loc) Β· 35.9 KB
/
TiListView.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2015-2017 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.ui.widget.listview;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import android.util.TypedValue;
import android.view.MotionEvent;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiColorHelper;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiRHelper;
import org.appcelerator.titanium.util.TiRHelper.ResourceNotFoundException;
import org.appcelerator.titanium.view.TiCompositeLayout;
import org.appcelerator.titanium.view.TiCompositeLayout.LayoutArrangement;
import org.appcelerator.titanium.view.TiCompositeLayout.LayoutParams;
import org.appcelerator.titanium.view.TiUIView;
import ti.modules.titanium.ui.RefreshControlProxy;
import ti.modules.titanium.ui.SearchBarProxy;
import ti.modules.titanium.ui.UIModule;
import ti.modules.titanium.ui.android.SearchViewProxy;
import ti.modules.titanium.ui.widget.searchbar.TiUISearchBar;
import ti.modules.titanium.ui.widget.searchbar.TiUISearchBar.OnSearchChangeListener;
import ti.modules.titanium.ui.widget.searchview.TiUISearchView;
import ti.modules.titanium.ui.widget.TiSwipeRefreshLayout;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class TiListView extends TiUIView implements OnSearchChangeListener
{
private TiNestedListView listView;
private TiBaseAdapter adapter;
private ArrayList<ListSectionProxy> sections;
private AtomicInteger itemTypeCount;
private String defaultTemplateBinding;
private ListViewWrapper wrapper;
private HashMap<String, TiListViewTemplate> templatesByBinding;
private int listItemId;
public static int listContentId;
public static int isCheck;
public static int hasChild;
public static int disclosure;
public static int accessory;
private int headerFooterId;
public LayoutInflater inflater;
private int titleId;
private int dividerHeight;
private ArrayList<Pair<Integer, Integer>> markers = new ArrayList<Pair<Integer, Integer>>();
private View headerView;
private View footerView;
private String searchText;
private boolean caseInsensitive;
private RelativeLayout searchLayout;
private static final String TAG = "TiListView";
/* We cache properties that already applied to the recycled list item in ViewItem.java
* However, since Android randomly selects a cached view to recycle, our cached properties
* will not be in sync with the native view's properties when user changes those values via
* User Interaction - i.e click. For this reason, we create a list that contains the properties
* that must be reset every time a view is recycled, to ensure synchronization. Currently, only
* "value" is in this list to correctly update the value of Ti.UI.Switch.
*/
public static List<String> MUST_SET_PROPERTIES =
Arrays.asList(TiC.PROPERTY_VALUE, TiC.PROPERTY_AUTO_LINK, TiC.PROPERTY_TEXT, TiC.PROPERTY_HTML,
TiC.PROPERTY_WIDTH, TiC.PROPERTY_HEIGHT, TiC.PROPERTY_IMAGE);
public static final String MIN_SEARCH_HEIGHT = "50dp";
public static final int HEADER_FOOTER_WRAP_ID = View.generateViewId();
public static final int HEADER_FOOTER_VIEW_TYPE = 0;
public static final int HEADER_FOOTER_TITLE_TYPE = 1;
public static final int BUILT_IN_TEMPLATE_ITEM_TYPE = 2;
public static final int CUSTOM_TEMPLATE_ITEM_TYPE = 3;
private class ListViewWrapper extends TiSwipeRefreshLayout
{
private boolean viewFocused = false;
private boolean selectionSet = false;
public ListViewWrapper(Context context)
{
super(context);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
// To prevent undesired "focus" and "blur" events during layout caused
// by ListView temporarily taking focus, we will disable focus events until
// layout has finished.
// First check for a quick exit. listView can be null, such as if window closing.
// Starting with API 18, calling requestFocus() will trigger another layout pass of the listview,
// resulting in an infinite loop. Here we check if the view is already focused, and stop the loop.
if (listView == null || (Build.VERSION.SDK_INT >= 18 && listView != null && !changed && viewFocused)) {
viewFocused = false;
super.onLayout(changed, left, top, right, bottom);
return;
}
// Starting with API 21, setSelection() triggers another layout pass, so we need to end it here to prevent
// an infinite loop
if (Build.VERSION.SDK_INT >= 21 && selectionSet) {
selectionSet = false;
return;
}
OnFocusChangeListener focusListener = null;
View focusedView = listView.findFocus();
int cursorPosition = -1;
if (focusedView != null) {
OnFocusChangeListener listener = focusedView.getOnFocusChangeListener();
if (listener != null && listener instanceof TiUIView) {
// Before unfocus the current editText, store cursor position so
// we can restore it later
if (focusedView instanceof EditText) {
cursorPosition = ((EditText) focusedView).getSelectionStart();
}
focusedView.setOnFocusChangeListener(null);
focusListener = listener;
}
}
// We are temporarily going to block focus to descendants
// because LinearLayout on layout will try to find a focusable descendant
if (focusedView != null) {
listView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
}
super.onLayout(changed, left, top, right, bottom);
// Now we reset the descendant focusability
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
TiViewProxy viewProxy = proxy;
if (viewProxy != null && viewProxy.hasListeners(TiC.EVENT_POST_LAYOUT)) {
viewProxy.fireEvent(TiC.EVENT_POST_LAYOUT, null);
}
// Layout is finished, re-enable focus events.
if (focusListener != null) {
// If the configuration changed, we manually fire the blur event
if (changed) {
focusedView.setOnFocusChangeListener(focusListener);
focusListener.onFocusChange(focusedView, false);
} else {
// Ok right now focus is with listView. So set it back to the focusedView
viewFocused = true;
focusedView.requestFocus();
focusedView.setOnFocusChangeListener(focusListener);
// Restore cursor position
if (cursorPosition != -1) {
((EditText) focusedView).setSelection(cursorPosition);
selectionSet = true;
}
}
}
}
}
public class TiBaseAdapter extends BaseAdapter
{
Activity context;
public TiBaseAdapter(Activity activity)
{
context = activity;
}
@Override
public int getCount()
{
int count = 0;
for (int i = 0; i < sections.size(); i++) {
ListSectionProxy section = sections.get(i);
count += section.getItemCount();
}
return count;
}
@Override
public Object getItem(int arg0)
{
// not using this method
return arg0;
}
@Override
public long getItemId(int position)
{
// not using this method
return position;
}
// One type for header/footer title, one for header/footer view, one for built-in template, and one type per
// custom template.
@Override
public int getViewTypeCount()
{
return 3 + templatesByBinding.size();
}
@Override
public int getItemViewType(int position)
{
Pair<ListSectionProxy, Pair<Integer, Integer>> info = getSectionInfoByEntryIndex(position);
ListSectionProxy section = info.first;
int sectionItemIndex = info.second.second;
if (section.isHeaderTitle(sectionItemIndex) || section.isFooterTitle(sectionItemIndex))
return HEADER_FOOTER_TITLE_TYPE;
if (section.isHeaderView(sectionItemIndex) || section.isFooterView(sectionItemIndex)) {
return HEADER_FOOTER_VIEW_TYPE;
}
return section.getTemplateByIndex(sectionItemIndex).getType();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// Get section info from index
Pair<ListSectionProxy, Pair<Integer, Integer>> info = getSectionInfoByEntryIndex(position);
ListSectionProxy section = info.first;
int sectionItemIndex = info.second.second;
int sectionIndex = info.second.first;
View content = convertView;
// Handles header/footer views and titles.
if (section.isHeaderView(sectionItemIndex) || section.isFooterView(sectionItemIndex)) {
return section.getHeaderOrFooterView(sectionItemIndex);
} else if (section.isHeaderTitle(sectionItemIndex) || section.isFooterTitle(sectionItemIndex)) {
// No content to reuse, so we create a new view
if (content == null) {
content = inflater.inflate(headerFooterId, null);
}
TextView title = (TextView) content.findViewById(titleId);
title.setText(section.getHeaderOrFooterTitle(sectionItemIndex));
return content;
}
// check marker and fire appropriate events
checkMarker(sectionIndex, sectionItemIndex, section.hasHeader());
// Handling templates
KrollDict data = section.getListItemData(sectionItemIndex);
TiListViewTemplate template = section.getTemplateByIndex(sectionItemIndex);
if (content != null) {
TiBaseListViewItem itemContent = (TiBaseListViewItem) content.findViewById(listContentId);
section.populateViews(data, itemContent, template, sectionItemIndex, sectionIndex, content);
// Manually add drawable for ripple touch feedback
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
itemContent.setClickable(true);
itemContent.setForeground(
context.getResources().getDrawable(typedValue.resourceId, context.getTheme()));
} catch (Exception ex) {
ex.printStackTrace();
}
}
} else {
content = inflater.inflate(listItemId, null);
TiBaseListViewItem itemContent = (TiBaseListViewItem) content.findViewById(listContentId);
LayoutParams params = new LayoutParams();
params.autoFillsWidth = true;
itemContent.setLayoutParams(params);
section.generateCellContent(sectionIndex, data, template, itemContent, sectionItemIndex, content);
// Manually add drawable for ripple touch feedback
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
itemContent.setClickable(true);
itemContent.setForeground(
context.getResources().getDrawable(typedValue.resourceId, context.getTheme()));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return content;
}
}
public TiListView(TiViewProxy proxy, Activity activity)
{
super(proxy);
// initializing variables
sections = new ArrayList<ListSectionProxy>();
itemTypeCount = new AtomicInteger(CUSTOM_TEMPLATE_ITEM_TYPE);
templatesByBinding = new HashMap<String, TiListViewTemplate>();
defaultTemplateBinding = UIModule.LIST_ITEM_TEMPLATE_DEFAULT;
caseInsensitive = true;
// handling marker
ArrayList<HashMap<String, Integer>> preloadMarkers = ((ListViewProxy) proxy).getPreloadMarkers();
if (preloadMarkers != null) {
setMarkers(preloadMarkers);
}
// initializing listView and adapter
ListViewWrapper wrapper = new ListViewWrapper(activity);
wrapper.setSwipeRefreshEnabled(false);
wrapper.setFocusable(false);
wrapper.setFocusableInTouchMode(false);
listView = TiNestedListView.createUsing(activity);
listView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
wrapper.addView(listView);
adapter = new TiBaseAdapter(activity);
// init inflater
inflater = LayoutInflater.from(activity);
listView.setCacheColorHint(Color.TRANSPARENT);
getLayoutParams().autoFillsHeight = true;
getLayoutParams().autoFillsWidth = true;
listView.setFocusable(true);
listView.setFocusableInTouchMode(true);
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
final TiViewProxy fProxy = proxy;
listView.setOnScrollListener(new OnScrollListener() {
private int _firstVisibleItem = 0;
private int _visibleItemCount = 0;
private boolean canFireScrollStart = true;
private boolean canFireScrollEnd = false;
private int mInitialScroll = 0;
private int scrollUp = 0;
private int newScrollUp = 0;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
String eventName;
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && canFireScrollEnd) {
eventName = TiC.EVENT_SCROLLEND;
canFireScrollEnd = false;
canFireScrollStart = true;
newScrollUp = 0;
} else if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL && canFireScrollStart) {
eventName = TiC.EVENT_SCROLLSTART;
canFireScrollEnd = true;
canFireScrollStart = false;
} else {
return;
}
KrollDict eventArgs = new KrollDict();
Pair<ListSectionProxy, Pair<Integer, Integer>> info = getSectionInfoByEntryIndex(_firstVisibleItem);
if (info == null) {
return;
}
int visibleItemCount = _visibleItemCount;
int itemIndex = info.second.second;
ListSectionProxy section = info.first;
if (section.getHeaderTitle() == null || section.getHeaderView() == null) {
if (itemIndex > 0) {
itemIndex -= 1;
}
visibleItemCount -= 1;
}
eventArgs.put("firstVisibleSection", section);
eventArgs.put("firstVisibleSectionIndex", info.second.first);
eventArgs.put("firstVisibleItem", section.getItemAt(itemIndex));
eventArgs.put("firstVisibleItemIndex", itemIndex);
eventArgs.put("visibleItemCount", visibleItemCount);
fProxy.fireEvent(eventName, eventArgs, false);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
_firstVisibleItem = firstVisibleItem;
_visibleItemCount = visibleItemCount;
int scrolledOffset = listView.getVerticalScrollOffset();
if (scrolledOffset != mInitialScroll) {
if (scrolledOffset > mInitialScroll) {
scrollUp = 1;
} else {
scrollUp = -1;
}
if (scrollUp != newScrollUp) {
KrollDict eventArgs = new KrollDict();
eventArgs.put("direction", (scrollUp > 0) ? "up" : "down");
eventArgs.put("velocity", 0);
eventArgs.put("targetContentOffset", 0);
fProxy.fireEvent(TiC.EVENT_SCROLLING, eventArgs, false);
newScrollUp = scrollUp;
}
mInitialScroll = scrolledOffset;
}
}
});
try {
headerFooterId = TiRHelper.getResource("layout.titanium_ui_list_header_or_footer");
listItemId = TiRHelper.getResource("layout.titanium_ui_list_item");
titleId = TiRHelper.getResource("id.titanium_ui_list_header_or_footer_title");
listContentId = TiRHelper.getResource("id.titanium_ui_list_item_content");
isCheck = TiRHelper.getImageRessource("drawable.btn_check_buttonless_on");
hasChild = TiRHelper.getImageRessource("drawable.btn_more");
disclosure = TiRHelper.getImageRessource("drawable.disclosure");
accessory = TiRHelper.getResource("id.titanium_ui_list_item_accessoryType");
} catch (ResourceNotFoundException e) {
Log.e(TAG, "XML resources could not be found!!!", Log.DEBUG_MODE);
}
this.wrapper = wrapper;
setNativeView(wrapper);
}
public String getSearchText()
{
return searchText;
}
public boolean getCaseInsensitive()
{
return caseInsensitive;
}
public void setHeaderTitle(String title)
{
TextView textView = (TextView) headerView.findViewById(titleId);
textView.setText(title);
if (textView.getVisibility() == View.GONE) {
textView.setVisibility(View.VISIBLE);
}
}
public void setFooterTitle(String title)
{
TextView textView = (TextView) footerView.findViewById(titleId);
textView.setText(title);
if (textView.getVisibility() == View.GONE) {
textView.setVisibility(View.VISIBLE);
}
}
@Override
public void registerForTouch()
{
registerForTouch(listView);
}
public void setMarker(HashMap<String, Integer> markerItem)
{
markers.clear();
addMarker(markerItem);
}
public void setMarkers(ArrayList<HashMap<String, Integer>> markerItems)
{
markers.clear();
for (int i = 0; i < markerItems.size(); ++i) {
HashMap<String, Integer> markerItem = markerItems.get(i);
addMarker(markerItem);
}
}
public void checkMarker(int sectionIndex, int sectionItemIndex, boolean hasHeader)
{
if (markers.isEmpty()) {
return;
}
if (hasHeader) {
sectionItemIndex--;
}
Iterator<Pair<Integer, Integer>> iterator = markers.iterator();
while (iterator.hasNext()) {
Pair<Integer, Integer> marker = iterator.next();
if (sectionIndex == marker.first && sectionItemIndex == marker.second) {
KrollDict data = new KrollDict();
data.put(TiC.PROPERTY_SECTION_INDEX, sectionIndex);
data.put(TiC.PROPERTY_ITEM_INDEX, sectionItemIndex);
if (proxy != null && proxy.hasListeners(TiC.EVENT_MARKER)) {
proxy.fireEvent(TiC.EVENT_MARKER, data, false);
}
iterator.remove();
}
}
}
public void addMarker(HashMap<String, Integer> markerItem)
{
int sectionIndex = markerItem.get(TiC.PROPERTY_SECTION_INDEX);
int itemIndex = markerItem.get(TiC.PROPERTY_ITEM_INDEX);
markers.add(new Pair<Integer, Integer>(sectionIndex, itemIndex));
}
public void processProperties(KrollDict d)
{
if (d.containsKey(TiC.PROPERTY_TEMPLATES)) {
Object templates = d.get(TiC.PROPERTY_TEMPLATES);
if (templates != null) {
processTemplates(new KrollDict((HashMap) templates));
}
}
if (d.containsKey(TiC.PROPERTY_REFRESH_CONTROL)) {
Object object = d.get(TiC.PROPERTY_REFRESH_CONTROL);
if (object instanceof RefreshControlProxy) {
((RefreshControlProxy) object).assignTo(this.wrapper);
}
}
if (d.containsKey(TiC.PROPERTY_SEARCH_TEXT)) {
this.searchText = TiConvert.toString(d, TiC.PROPERTY_SEARCH_TEXT);
}
if (d.containsKey(TiC.PROPERTY_SEARCH_VIEW)) {
TiViewProxy searchView = (TiViewProxy) d.get(TiC.PROPERTY_SEARCH_VIEW);
if (isSearchViewValid(searchView)) {
TiUIView search = searchView.getOrCreateView();
setSearchListener(searchView, search);
layoutSearchView(searchView);
} else {
Log.e(TAG, "Searchview type is invalid");
}
}
if (d.containsKey(TiC.PROPERTY_CASE_INSENSITIVE_SEARCH)) {
this.caseInsensitive = TiConvert.toBoolean(d, TiC.PROPERTY_CASE_INSENSITIVE_SEARCH, true);
}
if (d.containsKey(TiC.PROPERTY_SEPARATOR_HEIGHT)) {
TiDimension dHeight = TiConvert.toTiDimension(d.get(TiC.PROPERTY_SEPARATOR_HEIGHT), -1);
int height = dHeight.getAsPixels(listView);
if (height >= 0) {
dividerHeight = height;
listView.setDividerHeight(height);
}
}
if (d.containsKey(TiC.PROPERTY_SEPARATOR_COLOR)) {
String color = TiConvert.toString(d, TiC.PROPERTY_SEPARATOR_COLOR);
setSeparatorColor(color);
}
if (d.containsKey(TiC.PROPERTY_SEPARATOR_STYLE)) {
setSeparatorStyle(TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_SEPARATOR_STYLE),
UIModule.TABLE_VIEW_SEPARATOR_STYLE_NONE));
}
if (d.containsKey(TiC.PROPERTY_FOOTER_DIVIDERS_ENABLED)) {
boolean enabled = TiConvert.toBoolean(d, TiC.PROPERTY_FOOTER_DIVIDERS_ENABLED, false);
listView.setFooterDividersEnabled(enabled);
} else {
listView.setFooterDividersEnabled(false);
}
if (d.containsKey(TiC.PROPERTY_HEADER_DIVIDERS_ENABLED)) {
boolean enabled = TiConvert.toBoolean(d, TiC.PROPERTY_HEADER_DIVIDERS_ENABLED, false);
listView.setHeaderDividersEnabled(enabled);
} else {
listView.setHeaderDividersEnabled(false);
}
if (d.containsKey(TiC.PROPERTY_SHOW_VERTICAL_SCROLL_INDICATOR)) {
listView.setVerticalScrollBarEnabled(
TiConvert.toBoolean(d, TiC.PROPERTY_SHOW_VERTICAL_SCROLL_INDICATOR, true));
}
if (d.containsKey(TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE)) {
defaultTemplateBinding = TiConvert.toString(d, TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE);
}
ListViewProxy listProxy = (ListViewProxy) proxy;
if (d.containsKey(TiC.PROPERTY_SECTIONS)) {
// if user didn't append/modify/delete sections before this is called, we process sections
// as usual. Otherwise, we process the preloadSections, which should also contain the section(s)
// from this dictionary as well as other sections that user append/insert/deleted prior to this.
if (!listProxy.getPreload()) {
processSections((Object[]) d.get(TiC.PROPERTY_SECTIONS));
} else {
processSections(listProxy.getPreloadSections().toArray());
}
} else if (listProxy.getPreload()) {
// if user didn't specify 'sections' property upon creation of listview but append/insert it afterwards
// we process them instead.
processSections(listProxy.getPreloadSections().toArray());
}
listProxy.clearPreloadSections();
listProxy.setPreload(false);
if (d.containsKey(TiC.PROPERTY_HEADER_VIEW)) {
Object viewObj = d.get(TiC.PROPERTY_HEADER_VIEW);
setHeaderOrFooterView(viewObj, true);
} else if (d.containsKey(TiC.PROPERTY_HEADER_TITLE)) {
headerView = inflater.inflate(headerFooterId, null);
setHeaderTitle(TiConvert.toString(d, TiC.PROPERTY_HEADER_TITLE));
}
if (d.containsKey(TiC.PROPERTY_FOOTER_VIEW)) {
Object viewObj = d.get(TiC.PROPERTY_FOOTER_VIEW);
setHeaderOrFooterView(viewObj, false);
} else if (d.containsKey(TiC.PROPERTY_FOOTER_TITLE)) {
footerView = inflater.inflate(headerFooterId, null);
setFooterTitle(TiConvert.toString(d, TiC.PROPERTY_FOOTER_TITLE));
}
// Check to see if headerView and footerView are specified. If not, we hide the views
if (headerView == null) {
headerView = inflater.inflate(headerFooterId, null);
headerView.findViewById(titleId).setVisibility(View.GONE);
}
if (footerView == null) {
footerView = inflater.inflate(headerFooterId, null);
footerView.findViewById(titleId).setVisibility(View.GONE);
}
if (d.containsKeyAndNotNull(TiC.PROPERTY_CAN_SCROLL)) {
this.listView.setTouchScrollable(TiConvert.toBoolean(d.get(TiC.PROPERTY_CAN_SCROLL), true));
}
if (d.containsKeyAndNotNull(TiC.PROPERTY_FAST_SCROLL)) {
listView.setFastScrollEnabled(TiConvert.toBoolean(d.get(TiC.PROPERTY_FAST_SCROLL), false));
}
// Have to add header and footer before setting adapter
listView.addHeaderView(headerView, null, false);
listView.addFooterView(footerView, null, false);
listView.setAdapter(adapter);
super.processProperties(d);
}
private void layoutSearchView(TiViewProxy searchView)
{
TiUIView search = searchView.getOrCreateView();
RelativeLayout layout = new RelativeLayout(proxy.getActivity());
layout.setGravity(Gravity.NO_GRAVITY);
layout.setPadding(0, 0, 0, 0);
addSearchLayout(layout, searchView, search);
setNativeView(layout);
}
private void addSearchLayout(RelativeLayout layout, TiViewProxy searchView, TiUIView search)
{
RelativeLayout.LayoutParams p = createBasicSearchLayout();
p.addRule(RelativeLayout.ALIGN_PARENT_TOP);
TiDimension rawHeight;
if (searchView.hasProperty(TiC.PROPERTY_HEIGHT)) {
rawHeight = TiConvert.toTiDimension(searchView.getProperty(TiC.PROPERTY_HEIGHT), 0);
} else {
rawHeight = TiConvert.toTiDimension(MIN_SEARCH_HEIGHT, 0);
}
p.height = rawHeight.getAsPixels(layout);
View nativeView = search.getNativeView();
layout.addView(nativeView, p);
p = createBasicSearchLayout();
p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
p.addRule(RelativeLayout.BELOW, nativeView.getId());
ViewParent parentWrapper = wrapper.getParent();
if (parentWrapper != null && parentWrapper instanceof ViewGroup) {
// get the previous layout params so we can reset with new layout
ViewGroup.LayoutParams lp = wrapper.getLayoutParams();
ViewGroup parentView = (ViewGroup) parentWrapper;
// remove view from parent
parentView.removeView(wrapper);
// add new layout
layout.addView(wrapper, p);
parentView.addView(layout, lp);
} else {
layout.addView(wrapper, p);
}
this.searchLayout = layout;
}
private RelativeLayout.LayoutParams createBasicSearchLayout()
{
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
return p;
}
private void setHeaderOrFooterView(Object viewObj, boolean isHeader)
{
if (viewObj instanceof TiViewProxy) {
TiViewProxy viewProxy = (TiViewProxy) viewObj;
View view = layoutHeaderOrFooterView(viewProxy);
if (view != null) {
if (isHeader) {
headerView = view;
} else {
footerView = view;
}
}
}
}
private void reFilter(String searchText)
{
int numResults = 0;
if (searchText != null) {
for (int i = 0; i < sections.size(); ++i) {
ListSectionProxy section = sections.get(i);
numResults += section.applyFilter(searchText);
}
}
if (numResults == 0) {
fireEvent(TiC.EVENT_NO_RESULTS, null);
}
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
private boolean isSearchViewValid(TiViewProxy proxy)
{
if (proxy instanceof SearchBarProxy || proxy instanceof SearchViewProxy) {
return true;
} else {
return false;
}
}
public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy)
{
if (key.equals(TiC.PROPERTY_HEADER_TITLE)) {
setHeaderTitle(TiConvert.toString(newValue));
} else if (key.equals(TiC.PROPERTY_FOOTER_TITLE)) {
setFooterTitle(TiConvert.toString(newValue));
} else if (key.equals(TiC.PROPERTY_SECTIONS) && newValue instanceof Object[]) {
processSectionsAndNotify((Object[]) newValue);
} else if (key.equals(TiC.PROPERTY_REFRESH_CONTROL)) {
if (newValue == null) {
RefreshControlProxy.unassignFrom(this.wrapper);
} else if (newValue instanceof RefreshControlProxy) {
((RefreshControlProxy) newValue).assignTo(this.wrapper);
} else {
Log.e(TAG, "Invalid value assigned to property '" + key + "'. Must be of type 'RefreshControl'.");
}
} else if (key.equals(TiC.PROPERTY_SEARCH_TEXT)) {
this.searchText = TiConvert.toString(newValue);
if (this.searchText != null) {
reFilter(this.searchText);
}
} else if (key.equals(TiC.PROPERTY_CASE_INSENSITIVE_SEARCH)) {
this.caseInsensitive = TiConvert.toBoolean(newValue, true);
if (this.searchText != null) {
reFilter(this.searchText);
}
} else if (key.equals(TiC.PROPERTY_SEARCH_VIEW)) {
TiViewProxy searchView = (TiViewProxy) newValue;
if (isSearchViewValid(searchView)) {
TiUIView search = searchView.getOrCreateView();
setSearchListener(searchView, search);
if (searchLayout != null) {
searchLayout.removeAllViews();
addSearchLayout(searchLayout, searchView, search);
} else {
layoutSearchView(searchView);
}
} else {
Log.e(TAG, "Searchview type is invalid");
}
} else if (key.equals(TiC.PROPERTY_SHOW_VERTICAL_SCROLL_INDICATOR) && newValue != null) {
listView.setVerticalScrollBarEnabled(TiConvert.toBoolean(newValue));
} else if (key.equals(TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE) && newValue != null) {
defaultTemplateBinding = TiConvert.toString(newValue);
refreshItems();
} else if (key.equals(TiC.PROPERTY_SEPARATOR_COLOR)) {
String color = TiConvert.toString(newValue);
setSeparatorColor(color);
} else if (key.equals(TiC.PROPERTY_SEPARATOR_HEIGHT)) {
TiDimension dHeight = TiConvert.toTiDimension(newValue, -1);
int height = dHeight.getAsPixels(listView);
if (height >= 0) {
dividerHeight = height;
listView.setDividerHeight(height);
}
} else if (key.equals(TiC.PROPERTY_CAN_SCROLL)) {
this.listView.setTouchScrollable(TiConvert.toBoolean(newValue, true));
} else if (key.equals(TiC.PROPERTY_FAST_SCROLL)) {
listView.setFastScrollEnabled(TiConvert.toBoolean(newValue, false));
} else {
super.propertyChanged(key, oldValue, newValue, proxy);
}
}
private void setSearchListener(TiViewProxy searchView, TiUIView search)
{
if (searchView instanceof SearchBarProxy) {
((TiUISearchBar) search).setOnSearchChangeListener(this);
} else if (searchView instanceof SearchViewProxy) {
((TiUISearchView) search).setOnSearchChangeListener(this);
}
}
private void setSeparatorColor(String color)
{
int sepColor = TiColorHelper.parseColor(color);
int dHeight = 0;
if (dividerHeight == 0) {
dHeight = listView.getDividerHeight();
} else {
dHeight = dividerHeight;
}
listView.setDivider(new ColorDrawable(sepColor));
listView.setDividerHeight(dHeight);
}
public void setSeparatorStyle(int style)
{
if (style == UIModule.TABLE_VIEW_SEPARATOR_STYLE_NONE) {
listView.setDividerHeight(0);
} else if (style == UIModule.TABLE_VIEW_SEPARATOR_STYLE_SINGLE_LINE) {
listView.setDividerHeight(dividerHeight);
}
}
private void refreshItems()
{
for (int i = 0; i < sections.size(); i++) {
ListSectionProxy section = sections.get(i);
section.refreshItems();
}
}
protected void processTemplates(KrollDict templates)
{
for (String key : templates.keySet()) {
// Here we bind each template with a key so we can use it to look up later
KrollDict properties = new KrollDict((HashMap) templates.get(key));
TiListViewTemplate template = new TiListViewTemplate(key, properties);
// Set type to template, for recycling purposes.
template.setType(getItemType());
templatesByBinding.put(key, template);
// set parent of root item
template.setRootParent(proxy);
}
}
public View layoutHeaderOrFooterView(TiViewProxy viewProxy)
{
TiUIView tiView = viewProxy.peekView();
if (tiView != null) {
TiViewProxy parentProxy = viewProxy.getParent();
// Remove parent view if possible
if (parentProxy != null) {
TiUIView parentView = parentProxy.peekView();
if (parentView != null) {
parentView.remove(tiView);
}
}
} else {
TiViewProxy listViewProxy = getProxy();
if ((listViewProxy != null) && (listViewProxy.getActivity() != null)) {
viewProxy.setActivity(listViewProxy.getActivity());
}
tiView = viewProxy.forceCreateView();
}
View outerView = tiView.getOuterView();
ViewGroup parentView = (ViewGroup) outerView.getParent();
if (parentView != null && parentView.getId() == HEADER_FOOTER_WRAP_ID) {
return parentView;
} else {
// add a wrapper so layout params such as height, width takes in effect.
TiCompositeLayout wrapper = new TiCompositeLayout(viewProxy.getActivity(), LayoutArrangement.DEFAULT, null);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.WRAP_CONTENT);
wrapper.setLayoutParams(params);
outerView = tiView.getOuterView();
wrapper.addView(outerView, tiView.getLayoutParams());
wrapper.setId(HEADER_FOOTER_WRAP_ID);
return wrapper;
}
}
protected void processSections(Object[] sections)
{
for (ListSectionProxy listSectionProxy : this.sections) {
listSectionProxy.releaseViews();
}
this.sections.clear();
for (int i = 0; i < sections.length; i++) {
processSection(sections[i], -1);
}
}
protected void processSectionsAndNotify(Object[] sections)
{
processSections(sections);
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
protected void processSection(Object sec, int index)
{
if (sec instanceof ListSectionProxy) {
ListSectionProxy section = (ListSectionProxy) sec;
if (this.sections.contains(section)) {
return;
}
if (index == -1 || index >= sections.size()) {
this.sections.add(section);
} else {
this.sections.add(index, section);
}
section.setAdapter(adapter);
section.setListView(this);
// Attempts to set type for existing templates.
section.setTemplateType();
// Process preload data if any
section.processPreloadData();
// Apply filter if necessary
if (searchText != null) {
section.applyFilter(searchText);
}
}
}
protected Pair<ListSectionProxy, Pair<Integer, Integer>> getSectionInfoByEntryIndex(int index)
{
if (index < 0) {
return null;
}
for (int i = 0; i < sections.size(); i++) {
ListSectionProxy section = sections.get(i);
int sectionItemCount = section.getItemCount();
if (index <= sectionItemCount - 1) {
return new Pair<ListSectionProxy, Pair<Integer, Integer>>(section,
new Pair<Integer, Integer>(i, index));
} else {
index -= sectionItemCount;
}
}
return null;
}
public int getItemType()
{
return itemTypeCount.getAndIncrement();
}
public TiListViewTemplate getTemplateByBinding(String binding)
{
return templatesByBinding.get(binding);
}
public String getDefaultTemplateBinding()
{
return defaultTemplateBinding;
}
public int getSectionCount()
{
return sections.size();
}
public void appendSection(Object section)
{
if (section instanceof Object[]) {
Object[] secs = (Object[]) section;