-
Notifications
You must be signed in to change notification settings - Fork 76
/
UIActionSheetDialog.java
1435 lines (1265 loc) · 48.5 KB
/
UIActionSheetDialog.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
package com.aries.ui.widget.action.sheet;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.aries.ui.util.DrawableUtil;
import com.aries.ui.util.FindViewUtil;
import com.aries.ui.view.alpha.AlphaTextView;
import com.aries.ui.widget.BasisDialog;
import com.aries.ui.widget.R;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author: AriesHoo on 2018/7/19 10:35
* @E-Mail: AriesHoo@126.com
* Function: UIActionSheet效果Dialog
* Description:
* 1、继承自Dialog 并封装不同Builder模式
* 2、修改Dialog继承关系并修改默认ListSheetBuilder模式属性设置
* 3、2018-3-29 13:40:24 新增设置ListBuilder默认imageView默认宽高及文字与图片间距属性控制
* 修改 getListView及getGridView返回对象错误问题
* 4、2018-4-3 09:10:18 新增view拖拽关闭交互效果{@link Builder#setDragEnable(boolean)}
* 5、2018-4-3 12:50:31 将控制虚拟导航栏功能从BasisDialog移至此处
* 6、2018-4-6 21:43:20 调整设置cancel 及margin逻辑
* 7、2018-5-23 16:54:27 调整Cancel背景处理逻辑避免因设置GridView 间隔造成问题
* 8、2018-5-28 16:39:05 修改兼容Android O以上版本导航栏问题
* 9、2018-5-31 16:48:38 删除TranslucentUtil控制导航栏透明度
* 10、2019-3-11 15:40:52 修改Drawable 设置逻辑避免因Item高度变化适配问题
* 11、2019-4-11 10:20:43 新增底部导航栏控制相关{@link #onAttachedToWindow()}
* 12、2019-4-18 11:32:04 修改{@link GridBuilder.GridAdapter#getView(int, View, ViewGroup)}txt.setLayoutParams 避免5.0以下 崩溃
* 13、2019-4-22 16:51:54 将view拖拽关闭交互效果移至BasisDialog{@link Builder#setDragEnable(boolean)}
*/
public class UIActionSheetDialog extends BasisDialog<UIActionSheetDialog> {
public interface OnItemClickListener {
/**
* Item点击事件
*
* @param dialog
* @param itemView
* @param position
*/
void onClick(BasisDialog dialog, View itemView, int position);
}
public UIActionSheetDialog(Context context) {
super(context, R.style.ActionSheetViewDialogStyle);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mContentView != null && mContentView.findViewById(R.id.lLayout_rootActionSheetDialog) != null) {
mNavigationBottomView = mContentView.findViewById(R.id.lLayout_rootActionSheetDialog);
}
}
/**
* 获取标题
*
* @return 未设置Title文本则返回空
*/
public TextView getTitle() {
return FindViewUtil.getTargetView(mContentView, R.id.tv_titleActionSheetDialog);
}
/**
* 获取底部取消按钮
*
* @return 未设置Cancel文本则返回空
*/
public TextView getCancel() {
return FindViewUtil.getTargetView(mContentView, R.id.tv_cancelActionSheetDialog);
}
/**
* 获取ListView
*
* @return 未设置Item或无
*/
public ListView getListView() {
return FindViewUtil.getTargetView(mContentView, R.id.lv_containerActionSheetDialog);
}
public GridView getGridView() {
return FindViewUtil.getTargetView(mContentView, R.id.gv_containerActionSheetDialog);
}
public interface ICreateContentView {
/**
* 创建ItemView
*
* @return
*/
View createItemView();
/**
* 设置默认适配器
*
* @return
*/
ListAdapter getDefaultAdapter();
}
public static class SheetItem {
CharSequence text;
ColorStateList textColor;
Drawable drawable;
public SheetItem() {
this("");
}
public SheetItem(CharSequence text) {
this(text, -1);
}
public SheetItem(CharSequence text, int color) {
this(text, color > 0 ? ColorStateList.valueOf(color) : null);
}
public SheetItem(CharSequence text, ColorStateList color) {
this(text, color, null);
}
public SheetItem(CharSequence text, ColorStateList color, Drawable drawable) {
this.text = text;
this.textColor = color;
this.drawable = drawable;
}
public SheetItem setText(CharSequence text) {
this.text = text;
return this;
}
public SheetItem setImageDrawable(Drawable drawable) {
this.drawable = drawable;
return this;
}
public SheetItem setTextColor(ColorStateList color) {
this.textColor = color;
return this;
}
public SheetItem setTextColor(int color) {
return setTextColor(ColorStateList.valueOf(color));
}
}
/**
* 类微信风格
*/
public static class ListWeChatBuilder extends ListBuilder<ListWeChatBuilder> {
public ListWeChatBuilder(Context context) {
super(context);
setItemsDividerHeight(0)
.setTitleGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL)
.setCancelGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL)
.setItemsTextGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL)
.setItemsGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL)
.setCancelMarginTop(0);
}
}
/**
* 类iOS风格
*/
public static class ListIOSBuilder extends ListBuilder<ListIOSBuilder> {
public ListIOSBuilder(Context context) {
super(context);
setBackground(null)
.setItemsTopPressedDrawableResource(R.drawable.action_sheet_top_pressed)
.setItemsTopDrawableResource(R.drawable.action_sheet_top_normal)
.setItemsCenterPressedDrawableResource(R.drawable.action_sheet_middle_pressed)
.setItemsCenterDrawableResource(R.drawable.action_sheet_middle_normal)
.setItemsBottomPressedDrawableResource(R.drawable.action_sheet_bottom_pressed)
.setItemsBottomDrawableResource(R.drawable.action_sheet_bottom_normal)
.setItemsSinglePressedDrawableResource(R.drawable.action_sheet_single_pressed)
.setItemsSingleDrawableResource(R.drawable.action_sheet_single_normal)
.setCancelTextColorResource(R.color.colorActionSheetItemText)
.setItemsTextColorResource(R.color.colorActionSheetItemText)
.setItemsDividerHeight(0)
.setPadding(dp2px(8));
}
}
/**
* 常见风格--类QQ/BottomSheet
*/
public static class ListSheetBuilder extends ListBuilder<ListSheetBuilder> {
public ListSheetBuilder(Context context) {
super(context);
setCancelTextColorResource(R.color.colorActionSheetNormalItemText)
.setItemsTextColorResource(R.color.colorActionSheetNormalItemText);
}
}
private static class ListBuilder<T extends ListBuilder> extends Builder<T> {
private List<View> mListHeaderViews;
private List<View> mListFooterViews;
private ListView mLvContainer;
private Drawable mItemsDivider;
private int mItemsDividerHeight;
public ListBuilder(Context context) {
super(context);
setItemDrawableResource(R.color.colorActionSheetEdge)
.setItemPressedDrawableResource(R.color.colorActionSheetEdgePressed)
.setItemsDividerResource(R.color.colorActionSheetEdgeLineGray)
.setItemsDividerHeightResource(R.dimen.dp_action_sheet_list_line_height)
.setItemsImageWidth(dp2px(26))
.setItemsImageHeight(dp2px(26))
.setCancelMarginTop(dp2px(8));
}
/**
* {@link ListView#addHeaderView(View)}
*
* @param view
* @return
*/
public T addHeaderView(View view) {
if (view != null) {
if (mListHeaderViews == null) {
mListHeaderViews = new ArrayList<>();
}
mListHeaderViews.add(view);
}
return backBuilder();
}
/**
* {@link ListView#addFooterView(View)}
*
* @param view
* @return
*/
public T addFooterView(View view) {
if (view != null) {
if (mListFooterViews == null) {
mListFooterViews = new ArrayList<>();
}
mListFooterViews.add(view);
}
return backBuilder();
}
public T setItemDrawable(Drawable drawable) {
setItemsSingleDrawable(drawable)
.setItemsTopDrawable(drawable)
.setItemsCenterDrawable(drawable)
.setItemsBottomDrawable(drawable);
return backBuilder();
}
public T setItemDrawableResource(int res) {
return setItemDrawable(mResourceUtil.getDrawable(res));
}
public T setItemDrawableColor(int color) {
return setItemDrawable(new ColorDrawable(color));
}
public T setItemPressedDrawable(Drawable drawable) {
setItemsSinglePressedDrawable(drawable).
setItemsTopPressedDrawable(drawable)
.setItemsCenterPressedDrawable(drawable)
.setItemsBottomPressedDrawable(drawable);
return backBuilder();
}
public T setItemPressedDrawableResource(int res) {
return setItemPressedDrawable(mResourceUtil.getDrawable(res));
}
public T setItemPressedDrawableColor(int color) {
return setItemPressedDrawable(new ColorDrawable(color));
}
public T setItemsTopDrawable(Drawable drawable) {
mTopDrawable = drawable;
return backBuilder();
}
public T setItemsTopDrawableResource(int res) {
return setItemsTopDrawable(mResourceUtil.getDrawable(res));
}
public T setItemsTopDrawableColor(int color) {
return setItemsTopDrawable(new ColorDrawable(color));
}
public T setItemsTopPressedDrawable(Drawable drawable) {
mTopPressedDrawable = drawable;
return backBuilder();
}
public T setItemsTopPressedDrawableResource(int res) {
return setItemsTopPressedDrawable(mResourceUtil.getDrawable(res));
}
public T setItemsTopPressedDrawableColor(int color) {
return setItemsTopPressedDrawable(new ColorDrawable(color));
}
public T setItemsCenterDrawable(Drawable drawable) {
mCenterDrawable = drawable;
return backBuilder();
}
public T setItemsCenterDrawableResource(int res) {
return setItemsCenterDrawable(mResourceUtil.getDrawable(res));
}
public T setItemsCenterDrawableColor(int color) {
return setItemsCenterDrawable(new ColorDrawable(color));
}
public T setItemsCenterPressedDrawable(Drawable drawable) {
mCenterPressedDrawable = drawable;
return backBuilder();
}
public T setItemsCenterPressedDrawableResource(int res) {
return setItemsCenterPressedDrawable(mResourceUtil.getDrawable(res));
}
public T setItemsCenterPressedDrawableColor(int color) {
return setItemsCenterPressedDrawable(new ColorDrawable(color));
}
public T setItemsBottomDrawable(Drawable drawable) {
mBottomDrawable = drawable;
return backBuilder();
}
public T setItemsBottomDrawableResource(int res) {
return setItemsBottomDrawable(mResourceUtil.getDrawable(res));
}
public T setItemsBottomDrawableColor(int color) {
return setItemsBottomDrawable(new ColorDrawable(color));
}
public T setItemsBottomPressedDrawable(Drawable drawable) {
mBottomPressedDrawable = drawable;
return backBuilder();
}
public T setItemsBottomPressedDrawableResource(int res) {
return setItemsBottomPressedDrawable(mResourceUtil.getDrawable(res));
}
public T setItemsBottomPressedDrawableColor(int color) {
return setItemsBottomPressedDrawable(new ColorDrawable(color));
}
public T setItemsDivider(Drawable drawable) {
mItemsDivider = drawable;
return backBuilder();
}
public T setItemsDividerColor(int color) {
return setItemsDivider(new ColorDrawable(color));
}
public T setItemsDividerResource(int res) {
return setItemsDivider(mResourceUtil.getDrawable(res));
}
public T setItemsDividerHeight(int height) {
mItemsDividerHeight = height;
return backBuilder();
}
public T setItemsDividerHeightResource(int res) {
return setItemsDividerHeight(mResourceUtil.getDimensionPixelSize(res));
}
/**
* 创建标题线框
*/
private void createLine(boolean isTitle) {
if (isTitle && (TextUtils.isEmpty(mTitleStr) || mItemsDividerHeight <= 0 || mItemsDivider == null ||
((mListItem == null || mListItem.size() == 0) && (mListHeaderViews == null)))) {
return;
}
if (!isTitle && (TextUtils.isEmpty(mCancelStr) || mItemsDividerHeight <= 0 || mItemsDivider == null
|| mCancelMarginTop > 0)) {
return;
}
Drawable back = DrawableUtil.getNewDrawable(mItemsDivider);
View viewLine = new View(mContext);
viewLine.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, mItemsDividerHeight));
mLLayoutRoot.addView(viewLine);
setViewBackground(viewLine, back);
}
private void createList() {
if (mListHeaderViews == null && mListFooterViews == null && mListItem == null) {
return;
}
mLvContainer = new ListView(mContext);
mLvContainer.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f));
mLLayoutRoot.addView(mLvContainer);
//设置ListView相关
mLvContainer.setId(R.id.lv_containerActionSheetDialog);
mLvContainer.setVerticalScrollBarEnabled(false);
mLvContainer.setOverScrollMode(View.OVER_SCROLL_NEVER);
if (mListHeaderViews != null) {
for (View v : mListHeaderViews) {
mLvContainer.addHeaderView(v);
}
}
if (mListFooterViews != null) {
for (View v : mListFooterViews) {
mLvContainer.addFooterView(v);
}
}
mLvContainer.setDivider(DrawableUtil.getNewDrawable(mItemsDivider));
mLvContainer.setDividerHeight(mItemsDividerHeight);
mLvContainer.setAdapter(mAdapter);
}
@Override
public View createItemView() {
createLine(true);
createList();
createLine(false);
return mLvContainer;
}
@Override
public ListAdapter getDefaultAdapter() {
return new ListAdapter();
}
private class ListAdapter extends SheetAdapter {
@Override
public View getView(final int i, View convertView, ViewGroup parent) {
final SheetItem data = getItem(i);
final ViewHolder holder;
Drawable background;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_action_sheet_list, null);
holder = new ViewHolder();
holder.imageView = convertView.findViewById(R.id.iv_iconActionSheetList);
holder.textView = convertView.findViewById(R.id.tv_msgActionSheetList);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (data.drawable != null) {
holder.imageView.setVisibility(View.VISIBLE);
holder.imageView.setImageDrawable(data.drawable);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) holder.imageView.getLayoutParams();
params.height = mItemsImageHeight;
params.width = mItemsImageWidth;
params.rightMargin = mTextDrawablePadding;
holder.imageView.setLayoutParams(params);
} else {
holder.imageView.setVisibility(View.GONE);
}
setTextView(holder, data, i);
((LinearLayout) convertView).setGravity(mItemsGravity);
convertView.setMinimumHeight(mItemsMinHeight);
convertView.setPadding(mItemsTextPaddingLeft, mItemsTextPaddingTop, mItemsTextPaddingRight, mItemsTextPaddingBottom);
int size = getCount();
int sizeHeader = mListHeaderViews != null ? mListHeaderViews.size() : 0;
boolean hasTitle = !TextUtils.isEmpty(mTitleStr);
boolean hasMargin = mCancelMarginTop > 0 || TextUtils.isEmpty(mCancelStr);
if (size == 1) {
if (hasTitle || sizeHeader > 0) {
background = hasMargin ? mStateDrawableBottom : mStateDrawableCenter;
} else {
background = hasMargin ? mStateDrawableSingle : mStateDrawableTop;
}
} else {
if (hasTitle || sizeHeader > 0) {
if (i >= 0 && i < size - 1) {
background = mStateDrawableCenter;
} else {
background = hasMargin ? mStateDrawableBottom : mStateDrawableCenter;
}
} else {
if (i == 0) {
background = mStateDrawableTop;
} else if (i < size - 1) {
background = mStateDrawableCenter;
} else {
background = hasMargin ? mStateDrawableBottom : mStateDrawableCenter;
}
}
}
background = DrawableUtil.getNewDrawable(background);
setViewBackground(convertView, background);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mItemsClickDismissEnable) {
mDialog.dismiss();
}
if (mOnItemClickListener != null) {
mOnItemClickListener.onClick(mDialog, view, i);
}
}
});
return convertView;
}
}
}
/**
* 网格布局
*/
public static class GridBuilder extends Builder<GridBuilder> {
private GridView mGvContainer;
private Drawable mGridBackground;
private int mGridPadding;
private int mNumColumns = GridView.AUTO_FIT;
private int mStretchMode = GridView.STRETCH_COLUMN_WIDTH;
private int mHorizontalSpacing;
private int mVerticalSpacing;
private boolean mItemsClickBackgroundEnable;
public GridBuilder(Context context) {
super(context);
setNumColumns(3)
.setGridBackgroundColor(Color.WHITE)
.setGridPadding(dp2px(12))
.setItemsTextSize(12)
.setItemsSingleDrawableResource(R.color.colorActionSheetEdge)
.setItemsSinglePressedDrawableResource(R.color.colorActionSheetEdgePressed);
}
/**
* 设置Grid 背景drawable
*
* @param drawable
* @return
*/
public GridBuilder setGridBackground(Drawable drawable) {
mGridBackground = drawable;
return this;
}
/**
* 设置Grid背景颜色
*
* @param color
* @return
*/
public GridBuilder setGridBackgroundColor(int color) {
return setGridBackground(new ColorDrawable(color));
}
/**
* 设置Grid 背景资源
*
* @param res
* @return
*/
public GridBuilder setGridBackgroundResource(int res) {
return setGridBackground(mResourceUtil.getDrawable(res));
}
/**
* 设置是否item点击/按下整个item背景效果--默认为透明度变化
*
* @param enable
* @return
*/
public GridBuilder setItemsClickBackgroundEnable(boolean enable) {
this.mItemsClickBackgroundEnable = enable;
return this;
}
/**
* 设置Grid padding
*
* @param padding
* @return
*/
public GridBuilder setGridPadding(int padding) {
mGridPadding = padding;
return this;
}
/**
* {@link GridView#setHorizontalSpacing(int)}
*
* @param spacing
* @return
*/
public GridBuilder setHorizontalSpacing(int spacing) {
this.mHorizontalSpacing = spacing;
return this;
}
/**
* {@link GridView#setVerticalSpacing(int)}
*
* @param spacing
* @return
*/
public GridBuilder setVerticalSpacing(int spacing) {
this.mVerticalSpacing = spacing;
return this;
}
/**
* {@link GridView#setNumColumns(int)} (int)}
*
* @param numColumns
* @return
*/
public GridBuilder setNumColumns(int numColumns) {
this.mNumColumns = numColumns;
return this;
}
/**
* {@link GridView#setStretchMode(int)}
*
* @param mode
* @return
*/
public GridBuilder setStretchMode(int mode) {
this.mStretchMode = mode;
return this;
}
@Override
public View createItemView() {
mGvContainer = new GridView(mContext);
mGvContainer.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f));
mLLayoutRoot.addView(mGvContainer);
//设置GridView相关属性
mGvContainer.setId(R.id.gv_containerActionSheetDialog);
mGvContainer.setVerticalScrollBarEnabled(false);
mGvContainer.setOverScrollMode(View.OVER_SCROLL_NEVER);
mGvContainer.setNumColumns(mNumColumns);
mGvContainer.setStretchMode(mStretchMode);
mGvContainer.setHorizontalSpacing(mHorizontalSpacing);
mGvContainer.setVerticalSpacing(mVerticalSpacing);
mGvContainer.setAdapter(mAdapter);
mGvContainer.setPadding(mGridPadding, mGridPadding, mGridPadding, mGridPadding);
setViewBackground(mGvContainer, mGridBackground);
setViewBackground(mTvTitle, mGridBackground);
if (mItemsClickBackgroundEnable) {
mGvContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mItemsClickDismissEnable) {
mDialog.dismiss();
}
if (mOnItemClickListener != null) {
mOnItemClickListener.onClick(mDialog, view, position);
}
}
});
}
return mGvContainer;
}
@Override
public ListAdapter getDefaultAdapter() {
return new GridAdapter();
}
private class GridAdapter extends SheetAdapter {
@Override
public View getView(final int i, View convertView, ViewGroup parent) {
final SheetItem data = getItem(i);
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
AlphaTextView txt = new AlphaTextView(mContext);
// 此处必须为AbsListView否则5.0以下会崩
txt.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));
convertView = txt;
holder.textView = txt;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
setTextView(holder, data, i);
DrawableUtil.setDrawableWidthHeight(data.drawable, mItemsImageWidth, mItemsImageHeight);
holder.textView.setCompoundDrawables(null, data.drawable, null, null);
holder.textView.setPadding(mItemsTextPaddingLeft, mItemsTextPaddingTop, mItemsTextPaddingRight, mItemsTextPaddingBottom);
if (!mItemsClickBackgroundEnable) {
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mItemsClickDismissEnable) {
mDialog.dismiss();
}
if (mOnItemClickListener != null) {
mOnItemClickListener.onClick(mDialog, view, i);
}
}
});
}
return convertView;
}
}
}
private static abstract class Builder<T extends Builder> extends BasisBuilder<T> implements ICreateContentView {
protected StateListDrawable mStateDrawableTop;
protected StateListDrawable mStateDrawableCenter;
protected StateListDrawable mStateDrawableBottom;
protected StateListDrawable mStateDrawableSingle;
protected Drawable mTopDrawable;
protected Drawable mTopPressedDrawable;
protected Drawable mCenterDrawable;
protected Drawable mCenterPressedDrawable;
protected Drawable mBottomDrawable;
protected Drawable mBottomPressedDrawable;
protected Drawable mSingleDrawable;
protected Drawable mSinglePressedDrawable;
protected int mMarginTop;
protected List<SheetItem> mListItem;
protected TextView mTvTitle;
protected CharSequence mTitleStr;
protected ColorStateList mTitleTextColor;
protected int mTitleGravity = Gravity.CENTER;
protected float mTitleTextSize = 16;
protected TextView mTvCancel;
protected CharSequence mCancelStr;
protected ColorStateList mCancelTextColor;
protected int mCancelGravity = Gravity.CENTER;
protected float mCancelTextSize = 16;
protected int mCancelMarginTop;
protected Map<Integer, ColorStateList> mMapItemColor;
protected ListAdapter mAdapter;
protected OnItemClickListener mOnItemClickListener;
protected int mTextDrawablePadding;
protected float mItemsTextSize = 16;
protected int mItemsMinHeight = 45;
protected ColorStateList mItemsTextColor;
protected int mItemsGravity = Gravity.CENTER;
protected int mItemsTextGravity = Gravity.CENTER;
protected int mItemsImageWidth = -1;
protected int mItemsImageHeight = -1;
protected int mItemsTextPaddingLeft;
protected int mItemsTextPaddingTop;
protected int mItemsTextPaddingRight;
protected int mItemsTextPaddingBottom;
protected boolean mItemsClickDismissEnable = true;
public Builder(Context context) {
super(context);
Log.i("height", "height:" + getScreenHeight());
setBackgroundResource(R.color.colorActionSheetNormalBackground)
.setItemsSingleDrawableResource(R.color.colorActionSheetEdge)
.setItemsSinglePressedDrawableResource(R.color.colorActionSheetEdgePressed)
.setMarginTop((int) (getScreenHeight() * 0.3))
.setTextPadding(dp2px(16), dp2px(10), dp2px(16), dp2px(10))
.setTitleTextColorResource(R.color.colorActionSheetTitleText)
.setCancelTextColorResource(R.color.colorActionSheetWeiXinText)
.setItemsTextColorResource(R.color.colorActionSheetWeiXinText)
.setItemsMinHeight(dp2px(45))
.setTextDrawablePadding(dp2px(12))
.setPadding(0);
}
/**
* 设置ContentView距离顶部间距
*
* @param top
* @return
*/
public T setMarginTop(int top) {
mMarginTop = top;
return backBuilder();
}
/**
* 设置文本与img 间距
*
* @param padding
* @return
*/
public T setTextDrawablePadding(int padding) {
this.mTextDrawablePadding = padding;
return backBuilder();
}
/**
* 设置Item、标题、取消文本间距
*
* @param left
* @param top
* @param right
* @param bottom
* @return
*/
public T setTextPadding(int left, int top, int right, int bottom) {
this.mItemsTextPaddingLeft = left;
this.mItemsTextPaddingTop = top;
this.mItemsTextPaddingRight = right;
this.mItemsTextPaddingBottom = bottom;
return backBuilder();
}
/**
* 设置标题
* {@link TextView#setText(CharSequence)}
*
* @param charSequence
* @return
*/
public T setTitle(CharSequence charSequence) {
this.mTitleStr = charSequence;
return backBuilder();
}
public T setTitle(int resId) {
return setTitle(mResourceUtil.getText(resId));
}
/**
* 设置标题文字颜色
* {@link TextView#setTextColor(ColorStateList)}
*
* @param color
* @return
*/
public T setTitleTextColor(ColorStateList color) {
this.mTitleTextColor = color;
return backBuilder();
}
public T setTitleTextColor(int color) {
return setTitleTextColor(ColorStateList.valueOf(color));
}
public T setTitleTextColorResource(int resId) {
return setTitleTextColor(mResourceUtil.getColorStateList(resId));
}
/**
* 设置标题文本尺寸
* {@link TextView#setTextSize(int, float)}
* {@link Builder#setTextSizeUnit(int)}
*
* @param size
* @return
*/
public T setTitleTextSize(float size) {
this.mTitleTextSize = size;
return backBuilder();
}
/**
* 设置标题文本对齐方式
* {@link TextView#setGravity(int)}
*
* @param gravity
* @return
*/
public T setTitleGravity(int gravity) {
this.mTitleGravity = gravity;
return backBuilder();
}
/**
* 设置取消按钮文本
*
* @param charSequence
* @return
*/
public T setCancel(CharSequence charSequence) {
this.mCancelStr = charSequence;
return backBuilder();
}
public T setCancel(int resId) {
return setCancel(mResourceUtil.getText(resId));
}
/**
* 设置取消文本颜色
*
* @param color
* @return
*/
public T setCancelTextColor(ColorStateList color) {
this.mCancelTextColor = color;
return backBuilder();
}
public T setCancelTextColor(int color) {
return setCancelTextColor(ColorStateList.valueOf(color));
}
public T setCancelTextColorResource(int resId) {
return setCancelTextColor(mResourceUtil.getColorStateList(resId));
}
/**
* 设置取消文本尺寸
*
* @param size
* @return
*/
public T setCancelTextSize(float size) {
this.mCancelTextSize = size;
return backBuilder();
}
/**
* 设置取消文本对齐方式
*
* @param gravity
* @return
*/
public T setCancelGravity(int gravity) {
this.mCancelGravity = gravity;
return backBuilder();
}
/**
* 设置取消文本与Item间距
*
* @param margin
* @return
*/
public T setCancelMarginTop(int margin) {
this.mCancelMarginTop = margin;
return backBuilder();
}
/**
* 设置Item 适配器
*
* @param adapter
* @return
*/
public T setAdapter(ListAdapter adapter) {
this.mAdapter = adapter;
return backBuilder();
}
/**
* 设置单个Item默认状态背景Drawable