-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTableView.cs
More file actions
928 lines (760 loc) · 33.2 KB
/
TableView.cs
File metadata and controls
928 lines (760 loc) · 33.2 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
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
/*
* Copyright (c) 2018. Evren Coşkun
*
* Licensed 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.
*
*/
using System;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.OS;
using Android.Support.Annotation;
using Android.Support.V4.Content;
using Android.Support.V7.Widget;
using Android.Util;
using Android.Views;
using Android.Widget;
using Com.Evrencoskun.Tableview.Adapter;
using Com.Evrencoskun.Tableview.Adapter.Recyclerview;
using Com.Evrencoskun.Tableview.Adapter.Recyclerview.Holder;
using Com.Evrencoskun.Tableview.Handler;
using Com.Evrencoskun.Tableview.Layoutmanager;
using Com.Evrencoskun.Tableview.Listener;
using Com.Evrencoskun.Tableview.Listener.Itemclick;
using Com.Evrencoskun.Tableview.Listener.Scroll;
using Com.Evrencoskun.Tableview.Preference;
using Com.Evrencoskun.Tableview.Sort;
using Com.Evrencoskun.Tableview.Util;
using TableViewSharp;
namespace Com.Evrencoskun.Tableview
{
/// <summary>Created by evrencoskun on 11/06/2017.</summary>
public class TableView : FrameLayout, ITableView, ITableViewListener
{
private static readonly string LogTag = typeof(Com.Evrencoskun.Tableview.TableView).Name;
protected internal CellRecyclerView mCellRecyclerView;
protected internal CellRecyclerView mColumnHeaderRecyclerView;
protected internal CellRecyclerView mRowHeaderRecyclerView;
protected internal AbstractTableAdapter mTableAdapter;
private ITableViewListener mTableViewListener;
private VerticalRecyclerViewListener mVerticalRecyclerListener;
private HorizontalRecyclerViewListener mHorizontalRecyclerViewListener;
private ColumnHeaderRecyclerViewItemClickListener mColumnHeaderRecyclerViewItemClickListener;
private RowHeaderRecyclerViewItemClickListener mRowHeaderRecyclerViewItemClickListener;
private ColumnHeaderLayoutManager mColumnHeaderLayoutManager;
private LinearLayoutManager mRowHeaderLayoutManager;
private CellLayoutManager mCellLayoutManager;
private DividerItemDecoration mVerticalItemDecoration;
private DividerItemDecoration mHorizontalItemDecoration;
private SelectionHandler mSelectionHandler;
private ColumnSortHandler mColumnSortHandler;
private VisibilityHandler mVisibilityHandler;
private ScrollHandler mScrollHandler;
private FilterHandler mFilterHandler;
private PreferencesHandler mPreferencesHandler;
private ColumnWidthHandler mColumnWidthHandler;
private int mRowHeaderWidth;
private int mColumnHeaderHeight;
private int mSelectedColor;
private int mUnSelectedColor;
private int mShadowColor;
private int mSeparatorColor = -1;
private bool mHasFixedWidth;
private bool mIgnoreSelectionColors;
private bool mShowHorizontalSeparators = true;
private bool mShowVerticalSeparators = true;
private bool mIsSortable;
public TableView(Context context) : base(context)
{
InitialDefaultValues(null);
Initialize();
}
public TableView(Context context, IAttributeSet attrs) : base(context, attrs)
{
InitialDefaultValues(attrs);
Initialize();
}
public TableView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
InitialDefaultValues(null);
Initialize();
}
private void InitialDefaultValues(IAttributeSet attrs)
{
// Dimensions
mRowHeaderWidth = (int) Resources.GetDimension(Resource.Dimension.default_row_header_width);
mColumnHeaderHeight = (int) Resources.GetDimension(Resource.Dimension.default_column_header_height);
// Colors
mSelectedColor =
ContextCompat.GetColor(Context, Resource.Color.table_view_default_selected_background_color);
mUnSelectedColor =
ContextCompat.GetColor(Context, Resource.Color.table_view_default_unselected_background_color);
mShadowColor = ContextCompat.GetColor(Context, Resource.Color.table_view_default_shadow_background_color);
if (attrs == null)
{
// That means TableView is created programmatically.
return;
}
// Get values from xml attributes
TypedArray a = Context.Theme.ObtainStyledAttributes(attrs, Resource.Styleable.TableView, 0, 0);
try
{
// Dimensions
mRowHeaderWidth = (int) a.GetDimension(Resource.Styleable.TableView_row_header_width, mRowHeaderWidth);
mColumnHeaderHeight = (int) a.GetDimension(Resource.Styleable.TableView_column_header_height,
mColumnHeaderHeight);
// Colors
mSelectedColor = a.GetColor(Resource.Styleable.TableView_selected_color, mSelectedColor);
mUnSelectedColor = a.GetColor(Resource.Styleable.TableView_unselected_color, mUnSelectedColor);
mShadowColor = a.GetColor(Resource.Styleable.TableView_shadow_color, mShadowColor);
mSeparatorColor = a.GetColor(Resource.Styleable.TableView_separator_color,
ContextCompat.GetColor(Context, Resource.Color.table_view_default_separator_color));
// Booleans
mShowVerticalSeparators = a.GetBoolean(Resource.Styleable.TableView_show_vertical_separator,
mShowVerticalSeparators);
mShowHorizontalSeparators = a.GetBoolean(Resource.Styleable.TableView_show_horizontal_separator,
mShowHorizontalSeparators);
}
finally
{
a.Recycle();
}
}
private void Initialize()
{
// Create Views
mColumnHeaderRecyclerView = CreateColumnHeaderRecyclerView();
mRowHeaderRecyclerView = CreateRowHeaderRecyclerView();
mCellRecyclerView = CreateCellRecyclerView();
// Add Views
AddView(mColumnHeaderRecyclerView);
AddView(mRowHeaderRecyclerView);
AddView(mCellRecyclerView);
// Create Handlers
mSelectionHandler = new SelectionHandler(this);
mVisibilityHandler = new VisibilityHandler(this);
mScrollHandler = new ScrollHandler(this);
mPreferencesHandler = new PreferencesHandler(this);
mColumnWidthHandler = new ColumnWidthHandler(this);
InitializeListeners();
}
protected internal virtual void InitializeListeners()
{
// --- Listeners to help Scroll synchronously ---
// It handles Vertical scroll listener
mVerticalRecyclerListener = new VerticalRecyclerViewListener(this);
// Set this listener both of Cell RecyclerView and RowHeader RecyclerView
mRowHeaderRecyclerView.AddOnItemTouchListener(mVerticalRecyclerListener);
mCellRecyclerView.AddOnItemTouchListener(mVerticalRecyclerListener);
// It handles Horizontal scroll listener
mHorizontalRecyclerViewListener = new HorizontalRecyclerViewListener(this);
// Set scroll listener to be able to scroll all rows synchrony.
mColumnHeaderRecyclerView.AddOnItemTouchListener(mHorizontalRecyclerViewListener);
// --- Listeners to help item clicks ---
// Create item click listeners
mColumnHeaderRecyclerViewItemClickListener =
new ColumnHeaderRecyclerViewItemClickListener(mColumnHeaderRecyclerView, this);
mRowHeaderRecyclerViewItemClickListener =
new RowHeaderRecyclerViewItemClickListener(mRowHeaderRecyclerView, this);
// Add item click listeners for both column header & row header recyclerView
mColumnHeaderRecyclerView.AddOnItemTouchListener(mColumnHeaderRecyclerViewItemClickListener);
mRowHeaderRecyclerView.AddOnItemTouchListener(mRowHeaderRecyclerViewItemClickListener);
// Add Layout change listener both of Column Header & Cell recyclerView to detect
// changing size
// For some case, it is pretty necessary.
TableViewLayoutChangeListener layoutChangeListener = new TableViewLayoutChangeListener(this);
mColumnHeaderRecyclerView.AddOnLayoutChangeListener(layoutChangeListener);
mCellRecyclerView.AddOnLayoutChangeListener(layoutChangeListener);
SetTableViewListener(this);
}
protected internal virtual CellRecyclerView CreateColumnHeaderRecyclerView()
{
CellRecyclerView recyclerView = new CellRecyclerView(Context);
// Set layout manager
recyclerView.SetLayoutManager(GetColumnHeaderLayoutManager());
// Set layout params
FrameLayout.LayoutParams layoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WrapContent, mColumnHeaderHeight);
layoutParams.LeftMargin = mRowHeaderWidth;
recyclerView.LayoutParameters = layoutParams;
if (IsShowHorizontalSeparators())
{
// Add vertical item decoration to display column line
recyclerView.AddItemDecoration(GetHorizontalItemDecoration());
}
return recyclerView;
}
protected internal virtual CellRecyclerView CreateRowHeaderRecyclerView()
{
CellRecyclerView recyclerView = new CellRecyclerView(Context);
// Set layout manager
recyclerView.SetLayoutManager(GetRowHeaderLayoutManager());
// Set layout params
FrameLayout.LayoutParams layoutParams =
new FrameLayout.LayoutParams(mRowHeaderWidth, FrameLayout.LayoutParams.WrapContent);
layoutParams.TopMargin = mColumnHeaderHeight;
recyclerView.LayoutParameters = layoutParams;
if (IsShowVerticalSeparators())
{
// Add vertical item decoration to display row line
recyclerView.AddItemDecoration(GetVerticalItemDecoration());
}
return recyclerView;
}
protected internal virtual CellRecyclerView CreateCellRecyclerView()
{
CellRecyclerView recyclerView = new CellRecyclerView(Context);
// Disable multitouch
recyclerView.MotionEventSplittingEnabled = false;
// Set layout manager
recyclerView.SetLayoutManager(GetCellLayoutManager());
// Set layout params
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WrapContent,
FrameLayout.LayoutParams.WrapContent);
layoutParams.LeftMargin = mRowHeaderWidth;
layoutParams.TopMargin = mColumnHeaderHeight;
recyclerView.LayoutParameters = layoutParams;
if (IsShowVerticalSeparators())
{
// Add vertical item decoration to display row line on center recycler view
recyclerView.AddItemDecoration(GetVerticalItemDecoration());
}
return recyclerView;
}
public virtual void SetAdapter(AbstractTableAdapter tableAdapter)
{
if (tableAdapter != null)
{
this.mTableAdapter = tableAdapter;
this.mTableAdapter.SetRowHeaderWidth(mRowHeaderWidth);
this.mTableAdapter.SetColumnHeaderHeight(mColumnHeaderHeight);
this.mTableAdapter.SetTableView(this);
// set adapters
if (mColumnHeaderRecyclerView != null)
{
mColumnHeaderRecyclerView.SetAdapter(mTableAdapter.ColumnHeaderRecyclerViewAdapter);
}
if (mRowHeaderRecyclerView != null)
{
mRowHeaderRecyclerView.SetAdapter(mTableAdapter.RowHeaderRecyclerViewAdapter);
}
if (mCellRecyclerView != null)
{
mCellRecyclerView.SetAdapter(mTableAdapter.GetCellRecyclerViewAdapter());
// Create Sort Handler
mColumnSortHandler = new ColumnSortHandler(this);
// Create Filter Handler
mFilterHandler = new FilterHandler(this);
}
}
}
public bool FixedWidth
{
get => HasFixedWidth();
set => SetHasFixedWidth(value);
}
public virtual bool HasFixedWidth()
{
return mHasFixedWidth;
}
public virtual void SetHasFixedWidth(bool hasFixedWidth)
{
this.mHasFixedWidth = hasFixedWidth;
// RecyclerView has also the same control to provide better performance.
mColumnHeaderRecyclerView.HasFixedSize = hasFixedWidth;
}
public bool IgnoreSelectionColors
{
get => IsIgnoreSelectionColors();
set => SetIgnoreSelectionColors(value);
}
public virtual bool IsIgnoreSelectionColors()
{
return mIgnoreSelectionColors;
}
public virtual void SetIgnoreSelectionColors(bool ignoreSelectionColor)
{
this.mIgnoreSelectionColors = ignoreSelectionColor;
}
public bool ShowHorizontalSeparators
{
get => IsShowHorizontalSeparators();
set => SetShowHorizontalSeparators( value);
}
public virtual bool IsShowHorizontalSeparators()
{
return mShowHorizontalSeparators;
}
public bool Sortable
{
get => IsSortable();
}
public virtual bool IsSortable()
{
return mIsSortable;
}
public virtual void SetShowHorizontalSeparators(bool showSeparators)
{
this.mShowHorizontalSeparators = showSeparators;
}
public virtual bool ShowVerticalSeparators
{
get => IsShowVerticalSeparators();
set => SetShowVerticalSeparators(value);
}
public virtual bool IsShowVerticalSeparators()
{
return mShowVerticalSeparators;
}
public virtual void SetShowVerticalSeparators(bool showSeparators)
{
this.mShowVerticalSeparators = showSeparators;
}
public CellRecyclerView CellRecyclerView
{
get => GetCellRecyclerView();
}
public virtual CellRecyclerView GetCellRecyclerView()
{
return mCellRecyclerView;
}
public CellRecyclerView ColumnHeaderRecyclerView => GetColumnHeaderRecyclerView();
public virtual CellRecyclerView GetColumnHeaderRecyclerView()
{
return mColumnHeaderRecyclerView;
}
public CellRecyclerView RowHeaderRecyclerView => GetRowHeaderRecyclerView();
public virtual CellRecyclerView GetRowHeaderRecyclerView()
{
return mRowHeaderRecyclerView;
}
public ColumnHeaderLayoutManager ColumnHeaderLayoutManager => GetColumnHeaderLayoutManager();
public virtual ColumnHeaderLayoutManager GetColumnHeaderLayoutManager()
{
if (mColumnHeaderLayoutManager == null)
{
mColumnHeaderLayoutManager = new ColumnHeaderLayoutManager(Context, this);
}
return mColumnHeaderLayoutManager;
}
public CellLayoutManager CellLayoutManager => GetCellLayoutManager();
public virtual CellLayoutManager GetCellLayoutManager()
{
if (mCellLayoutManager == null)
{
mCellLayoutManager = new CellLayoutManager(Context, this);
}
return mCellLayoutManager;
}
public LinearLayoutManager RowHeaderLayoutManager => GetRowHeaderLayoutManager();
public virtual LinearLayoutManager GetRowHeaderLayoutManager()
{
if (mRowHeaderLayoutManager == null)
{
mRowHeaderLayoutManager = new LinearLayoutManager(Context, LinearLayoutManager.Vertical, false);
}
return mRowHeaderLayoutManager;
}
public HorizontalRecyclerViewListener HorizontalRecyclerViewListener => GetHorizontalRecyclerViewListener();
public virtual HorizontalRecyclerViewListener GetHorizontalRecyclerViewListener()
{
return mHorizontalRecyclerViewListener;
}
public virtual VerticalRecyclerViewListener VerticalRecyclerViewListener => GetVerticalRecyclerViewListener();
public virtual VerticalRecyclerViewListener GetVerticalRecyclerViewListener()
{
return mVerticalRecyclerListener;
}
public virtual ITableViewListener TableViewListener
{
get => GetTableViewListener();
set => SetTableViewListener(value);
}
public virtual ITableViewListener GetTableViewListener()
{
return mTableViewListener;
}
public virtual void SetTableViewListener(ITableViewListener tableViewListener)
{
this.mTableViewListener = tableViewListener;
}
public virtual void SortColumn(int columnPosition, SortState sortState)
{
mIsSortable = true;
mColumnSortHandler.Sort(columnPosition, sortState);
}
public virtual void SortRowHeader(SortState sortState)
{
mIsSortable = true;
mColumnSortHandler.SortByRowHeader(sortState);
}
public virtual void RemeasureColumnWidth(int column)
{
// Remove calculated width value to be ready for recalculation.
GetColumnHeaderLayoutManager().RemoveCachedWidth(column);
// Recalculate of the width values of the columns
GetCellLayoutManager().FitWidthSize(column, false);
}
public AbstractTableAdapter Adapter
{
get => GetAdapter();
set => SetAdapter(value);
}
public virtual AbstractTableAdapter GetAdapter()
{
return mTableAdapter;
}
public virtual void Filter(Com.Evrencoskun.Tableview.Filter.Filter filter)
{
mFilterHandler.Filter(filter);
}
public FilterHandler FilterHandler => GetFilterHandler();
public virtual FilterHandler GetFilterHandler()
{
return mFilterHandler;
}
public virtual SortState GetSortingStatus(int column)
{
return mColumnSortHandler.GetSortingStatus(column);
}
public virtual SortState RowHeaderSortingStatus => GetRowHeaderSortingStatus();
public virtual SortState GetRowHeaderSortingStatus()
{
return mColumnSortHandler.GetRowHeaderSortingStatus();
}
public virtual void ScrollToColumnPosition(int column)
{
mScrollHandler.ScrollToColumnPosition(column);
}
public virtual void ScrollToColumnPosition(int column, int offset)
{
mScrollHandler.ScrollToColumnPosition(column, offset);
}
public virtual void ScrollToRowPosition(int row)
{
mScrollHandler.ScrollToRowPosition(row);
}
public virtual void ScrollToRowPosition(int row, int offset)
{
mScrollHandler.ScrollToRowPosition(row, offset);
}
public virtual ScrollHandler ScrollHandler => GetScrollHandler();
public virtual ScrollHandler GetScrollHandler()
{
return mScrollHandler;
}
public virtual void ShowRow(int row)
{
mVisibilityHandler.ShowRow(row);
}
public virtual void HideRow(int row)
{
mVisibilityHandler.HideRow(row);
}
public virtual void ShowAllHiddenRows()
{
mVisibilityHandler.ShowAllHiddenRows();
}
public virtual void ClearHiddenRowList()
{
mVisibilityHandler.ClearHideRowList();
}
public virtual void ShowColumn(int column)
{
mVisibilityHandler.ShowColumn(column);
}
public virtual void HideColumn(int column)
{
mVisibilityHandler.HideColumn(column);
}
public virtual bool IsColumnVisible(int column)
{
return mVisibilityHandler.IsColumnVisible(column);
}
public virtual void ShowAllHiddenColumns()
{
mVisibilityHandler.ShowAllHiddenColumns();
}
public virtual void ClearHiddenColumnList()
{
mVisibilityHandler.ClearHideColumnList();
}
public virtual bool IsRowVisible(int row)
{
return mVisibilityHandler.IsRowVisible(row);
}
public int SelectedRow
{
get => GetSelectedRow();
set => SetSelectedRow(value);
}
/// <summary>Returns the index of the selected row, -1 if no row is selected.</summary>
public virtual int GetSelectedRow()
{
return mSelectionHandler.GetSelectedRowPosition();
}
public virtual void SetSelectedRow(int row)
{
// Find the row header view holder which is located on row position.
AbstractViewHolder rowViewHolder =
(AbstractViewHolder) GetRowHeaderRecyclerView().FindViewHolderForAdapterPosition(row);
mSelectionHandler.SetSelectedRowPosition(rowViewHolder, row);
}
/// <summary>Returns the index of the selected column, -1 if no column is selected.</summary>
public int SelectedColumn
{
get => GetSelectedColumn();
set => SetSelectedColumn(value);
}
public virtual int GetSelectedColumn()
{
return mSelectionHandler.GetSelectedColumnPosition();
}
public virtual void SetSelectedColumn(int column)
{
// Find the column view holder which is located on column position .
AbstractViewHolder columnViewHolder =
(AbstractViewHolder) GetColumnHeaderRecyclerView().FindViewHolderForAdapterPosition(column);
mSelectionHandler.SetSelectedColumnPosition(columnViewHolder, column);
}
public virtual void SetSelectedCell(int column, int row)
{
// Find the cell view holder which is located on x,y (column,row) position.
AbstractViewHolder cellViewHolder = GetCellLayoutManager().GetCellViewHolder(column, row);
mSelectionHandler.SetSelectedCellPositions(cellViewHolder, column, row);
}
public SelectionHandler SelectionHandler
{
get => GetSelectionHandler();
}
public virtual SelectionHandler GetSelectionHandler()
{
return mSelectionHandler;
}
public ColumnSortHandler ColumnSortHandler => GetColumnSortHandler();
public ColumnSortHandler GetColumnSortHandler()
{
return mColumnSortHandler;
}
public virtual DividerItemDecoration HorizontalItemDecoration => GetHorizontalItemDecoration();
public virtual DividerItemDecoration GetHorizontalItemDecoration()
{
if (mHorizontalItemDecoration == null)
{
mHorizontalItemDecoration = CreateItemDecoration(DividerItemDecoration.Horizontal);
}
return mHorizontalItemDecoration;
}
public virtual DividerItemDecoration VerticalItemDecoration => GetVerticalItemDecoration();
public virtual DividerItemDecoration GetVerticalItemDecoration()
{
if (mVerticalItemDecoration == null)
{
mVerticalItemDecoration = CreateItemDecoration(DividerItemDecoration.Vertical);
}
return mVerticalItemDecoration;
}
protected internal virtual DividerItemDecoration CreateItemDecoration(int orientation)
{
Android.Graphics.Drawables.Drawable divider =
ContextCompat.GetDrawable(Context, Resource.Drawable.cell_line_divider);
// That means; There is a custom separator color from user.
if (mSeparatorColor != -1)
{
// Change its color
divider.SetColorFilter(new Color(mSeparatorColor), PorterDuff.Mode.SrcAtop);
}
DividerItemDecoration itemDecoration = new DividerItemDecoration(Context, orientation);
itemDecoration.SetDrawable(divider);
return itemDecoration;
}
/// <summary>This method helps to change default selected color programmatically.</summary>
/// <param name="selectedColor">It must be Color int.</param>
public virtual void SetSelectedColor(int selectedColor)
{
this.mSelectedColor = selectedColor;
}
public int SelectedColor => GetSelectedColor();
[ColorInt]
public virtual int GetSelectedColor()
{
return mSelectedColor;
}
public int SeparatorColor
{
get => GetSeparatorColor();
set => SetSeparatorColor(value);
}
public virtual void SetSeparatorColor(int mSeparatorColor)
{
this.mSeparatorColor = mSeparatorColor;
}
[ColorInt]
public virtual int GetSeparatorColor()
{
return mSeparatorColor;
}
/// <summary>This method helps to change default unselected color programmatically.</summary>
/// <param name="unSelectedColor">It must be Color int.</param>
public int UnSelectedColor
{
get => GetUnSelectedColor();
set => SetUnSelectedColor(value);
}
public virtual void SetUnSelectedColor(int unSelectedColor)
{
this.mUnSelectedColor = unSelectedColor;
}
[ColorInt]
public virtual int GetUnSelectedColor()
{
return mUnSelectedColor;
}
public int ShadowColor
{
get => GetShadowColor();
set => SetShadowColor(value);
}
public virtual void SetShadowColor(int shadowColor)
{
this.mShadowColor = shadowColor;
}
[ColorInt]
public virtual int GetShadowColor()
{
return mShadowColor;
}
/// <summary>get row header width</summary>
/// <returns>size in pixel</returns>
public int RowHeaderWidth
{
get => GetRowHeaderWidth();
set => SetRowHeaderWidth(value);
}
public virtual int GetRowHeaderWidth()
{
return mRowHeaderWidth;
}
/// <summary>set RowHeaderWidth</summary>
/// <param name="rowHeaderWidth">in pixel</param>
public virtual void SetRowHeaderWidth(int rowHeaderWidth)
{
this.mRowHeaderWidth = rowHeaderWidth;
if (mRowHeaderRecyclerView != null)
{
// Update RowHeader layout width
ViewGroup.LayoutParams layoutParams = mRowHeaderRecyclerView.LayoutParameters;
layoutParams.Width = rowHeaderWidth;
mRowHeaderRecyclerView.LayoutParameters = layoutParams;
mRowHeaderRecyclerView.RequestLayout();
}
if (mColumnHeaderRecyclerView != null)
{
// Update ColumnHeader left margin
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams) mColumnHeaderRecyclerView.LayoutParameters;
layoutParams.LeftMargin = rowHeaderWidth;
mColumnHeaderRecyclerView.LayoutParameters = layoutParams;
mColumnHeaderRecyclerView.RequestLayout();
}
if (mCellRecyclerView != null)
{
// Update Cells left margin
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mCellRecyclerView.LayoutParameters;
layoutParams.LeftMargin = rowHeaderWidth;
mCellRecyclerView.LayoutParameters = layoutParams;
mCellRecyclerView.RequestLayout();
}
if (GetAdapter() != null)
{
// update CornerView size
GetAdapter().SetRowHeaderWidth(rowHeaderWidth);
}
}
public virtual void SetColumnWidth(int columnPosition, int width)
{
mColumnWidthHandler.SetColumnWidth(columnPosition, width);
}
protected override IParcelable OnSaveInstanceState()
{
SavedState state = new SavedState(base.OnSaveInstanceState());
// Save all preferences of The TableView before the configuration changed.
state.preferences = mPreferencesHandler.SavePreferences();
return state;
}
protected override void OnRestoreInstanceState(IParcelable state)
{
if (!(state is SavedState))
{
base.OnRestoreInstanceState(state);
return;
}
SavedState savedState = (SavedState) state;
base.OnRestoreInstanceState(savedState.SuperState);
// Reload the preferences
mPreferencesHandler.LoadPreferences(savedState.preferences);
}
/// <summary>
/// ITableViewListener Implementation
/// </summary>
public event EventHandler<CellEventArgs> CellClicked;
public event EventHandler<CellEventArgs> CellLongPressed;
public event EventHandler<ColumnEventArgs> ColumnHeaderClicked;
public event EventHandler<ColumnEventArgs> ColumnHeaderLongPressed;
public event EventHandler<RowEventArgs> RowHeaderClicked;
public event EventHandler<RowEventArgs> RowHeaderLongPressed;
public class ColumnEventArgs : EventArgs
{
public RecyclerView.ViewHolder ColumnHeaderView { get; set; }
public int Column { get; set; }
}
public class RowEventArgs : EventArgs
{
public RecyclerView.ViewHolder RowHeaderView { get; set; }
public int Row { get; set; }
}
public class CellEventArgs : EventArgs
{
public RecyclerView.ViewHolder CellView { get; set; }
public int Column { get; set; }
public int Row { get; set; }
}
public void OnCellClicked(RecyclerView.ViewHolder cellView, int column, int row)
{
CellClicked.SafeFire(this, new CellEventArgs {CellView = cellView, Column = column, Row = row});
}
public void OnCellLongPressed(RecyclerView.ViewHolder cellView, int column, int row)
{
CellLongPressed.SafeFire(this, new CellEventArgs {CellView = cellView, Column = column, Row = row});
}
public void OnColumnHeaderClicked(RecyclerView.ViewHolder columnHeaderView, int column)
{
ColumnHeaderClicked.SafeFire(this,
new ColumnEventArgs {ColumnHeaderView = columnHeaderView, Column = column});
}
public void OnColumnHeaderLongPressed(RecyclerView.ViewHolder columnHeaderView, int column)
{
ColumnHeaderLongPressed.SafeFire(this,
new ColumnEventArgs {ColumnHeaderView = columnHeaderView, Column = column});
}
public void OnRowHeaderClicked(RecyclerView.ViewHolder rowHeaderView, int row)
{
RowHeaderClicked.SafeFire(this, new RowEventArgs {RowHeaderView = rowHeaderView, Row = row});
}
public void OnRowHeaderLongPressed(RecyclerView.ViewHolder rowHeaderView, int row)
{
RowHeaderLongPressed.SafeFire(this, new RowEventArgs {RowHeaderView = rowHeaderView, Row = row});
}
}
}