Skip to content

Commit 5f06f9f

Browse files
author
farasource
committed
v1.2.2
1 parent 6e03f92 commit 5f06f9f

File tree

1 file changed

+71
-65
lines changed

1 file changed

+71
-65
lines changed

dropdown/src/main/java/com/farasource/component/dropdown/DropdownView.java

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import androidx.annotation.ColorInt;
3232
import androidx.annotation.DrawableRes;
33+
import androidx.annotation.IntDef;
3334
import androidx.annotation.Keep;
3435
import androidx.annotation.NonNull;
3536
import androidx.annotation.Nullable;
@@ -49,6 +50,7 @@ public class DropdownView extends LinearLayout {
4950
private int arrowTint, arrowTintExpanded;
5051
private int titleHeight, contentHeight, dividerHeight;
5152
private boolean useDivider, expanded, isMoving;
53+
@RotationModel
5254
private int arrowRotation;
5355

5456
public DropdownView(@NonNull Context context) {
@@ -64,27 +66,27 @@ public DropdownView(@NonNull Context context, @Nullable AttributeSet attrs, int
6466
setOrientation(VERTICAL);
6567
TypedArray typed = context.obtainStyledAttributes(attrs, R.styleable.DropdownView, defStyleAttr, 0);
6668
elevation = typed.getDimension(R.styleable.DropdownView_elevation, getElevation());
67-
cardCornerRadius = typed.getDimension(R.styleable.DropdownView_cardCornerRadius, px2dpf(10));
69+
cardCornerRadius = typed.getDimension(R.styleable.DropdownView_cardCornerRadius, dpf(10));
6870
cardBackground = getMultiColourAttr(getContext(), typed, R.styleable.DropdownView_cardBackground, null);
6971
cardBackgroundExpanded = getMultiColourAttr(getContext(), typed, R.styleable.DropdownView_cardBackgroundExpanded, cardBackground);
7072
int resourceId = typed.getResourceId(R.styleable.DropdownView_arrow, R.drawable.ic_round_arrow_right_24);
7173
Drawable arrowIcon = ResourcesCompat.getDrawable(context.getResources(), resourceId, null);
7274
arrowTint = typed.getColor(R.styleable.DropdownView_arrowTint, Color.WHITE);
7375
arrowTintExpanded = typed.getColor(R.styleable.DropdownView_arrowTintExpanded, arrowTint);
74-
arrowRotation = typed.getInteger(R.styleable.DropdownView_arrowRotation, RotationModel.QUARTER);
76+
arrowRotation = typed.getInteger(R.styleable.DropdownView_arrowRotation, QUARTER);
7577
useDivider = typed.getBoolean(R.styleable.DropdownView_useDivider, false);
76-
dividerHeight = (int) typed.getDimension(R.styleable.DropdownView_dividerHeight, px2dpf(1));
78+
dividerHeight = (int) typed.getDimension(R.styleable.DropdownView_dividerHeight, dpf(1));
7779
int dividerColor = typed.getColor(R.styleable.DropdownView_dividerColor, 0xffe2e2e2);
7880
titleBackgroundExpanded = getMultiColourAttr(getContext(), typed, R.styleable.DropdownView_titleBackgroundExpanded, null, true);
7981
String titleText = typed.getString(R.styleable.DropdownView_titleText);
8082
titleTextColor = typed.getColor(R.styleable.DropdownView_titleTextColor, Color.WHITE);
8183
titleTextColorExpanded = typed.getColor(R.styleable.DropdownView_titleTextColorExpanded, titleTextColor);
82-
float titleTextSize = typed.getDimension(R.styleable.DropdownView_titleTextSize, px2dpf(17));
84+
float titleTextSize = typed.getDimension(R.styleable.DropdownView_titleTextSize, dpf(17));
8385
String titleTextFont = typed.getString(R.styleable.DropdownView_titleTextFont);
8486
boolean titleTextBold = typed.getBoolean(R.styleable.DropdownView_titleTextBold, true);
8587
String contentText = typed.getString(R.styleable.DropdownView_contentText);
8688
contentTextColor = typed.getColor(R.styleable.DropdownView_contentTextColor, Color.WHITE);
87-
float contentTextSize = typed.getDimension(R.styleable.DropdownView_contentTextSize, px2dpf(17));
89+
float contentTextSize = typed.getDimension(R.styleable.DropdownView_contentTextSize, dpf(17));
8890
String contentTextFont = typed.getString(R.styleable.DropdownView_contentTextFont);
8991
boolean contentTextBold = typed.getBoolean(R.styleable.DropdownView_contentTextBold, false);
9092
expanded = typed.getBoolean(R.styleable.DropdownView_expanded, false);
@@ -145,15 +147,15 @@ public void setExpanded(boolean expanded, boolean animate) {
145147

146148
@Override
147149
public void setElevation(float elevation) {
148-
this.elevation = px2dpf(elevation);
150+
this.elevation = dpf(elevation);
149151
super.setElevation(elevation);
150152
}
151153

152-
public void setCardCornerRadius(float cardCornerRadius) {
153-
if (this.cardCornerRadius == cardCornerRadius) {
154+
public void setCardCornerRadius(float radius) {
155+
if (this.cardCornerRadius == radius) {
154156
return;
155157
}
156-
this.cardCornerRadius = cardCornerRadius;
158+
this.cardCornerRadius = radius;
157159
if (cardBackground instanceof PaintDrawable) {
158160
PaintDrawableSetCornerRadius((PaintDrawable) cardBackground, true);
159161
}
@@ -167,43 +169,46 @@ public void setCardCornerRadius(float cardCornerRadius) {
167169
titleLinearLayout.setBackground(expanded ? null : titleBackgroundExpanded);
168170
}
169171

172+
public void setTitleBackgroundColor(@ColorInt int color) {
173+
setCardBackgroundColor(color);
174+
}
175+
170176
public void setCardBackgroundColor(@ColorInt int color) {
171177
setCardBackground(createColorDrawable(color, true));
172178
}
173179

174-
public void setCardBackground(Drawable cardBackground) {
180+
public void setCardBackground(Drawable drawable) {
175181
if (this.cardBackground == cardBackgroundExpanded) {
176-
cardBackgroundExpanded = cardBackground;
182+
cardBackgroundExpanded = drawable;
177183
if (expanded) {
178-
setBackground(cardBackground);
184+
setBackground(drawable);
179185
}
180186
}
181-
this.cardBackground = cardBackground;
187+
this.cardBackground = drawable;
182188
if (!expanded) {
183-
setBackground(cardBackground);
189+
setBackground(drawable);
184190
}
185191
}
186192

187193
public void setCardBackgroundColorExpanded(@ColorInt int color) {
188194
setCardBackgroundExpanded(createColorDrawable(color, true));
189195
}
190196

191-
public void setCardBackgroundExpanded(Drawable cardBackgroundExpanded) {
192-
this.cardBackgroundExpanded = cardBackgroundExpanded;
197+
public void setCardBackgroundExpanded(Drawable drawable) {
198+
this.cardBackgroundExpanded = drawable;
193199
if (expanded) {
194-
setBackground(cardBackgroundExpanded);
200+
setBackground(drawable);
195201
}
196202
}
197203

198-
199204
public void setTitleBackgroundColorExpanded(@ColorInt int color) {
200205
setTitleBackgroundExpanded(createColorDrawable(color, false));
201206
}
202207

203-
public void setTitleBackgroundExpanded(Drawable titleBackgroundExpanded) {
204-
this.titleBackgroundExpanded = titleBackgroundExpanded;
208+
public void setTitleBackgroundExpanded(Drawable drawable) {
209+
this.titleBackgroundExpanded = drawable;
205210
if (expanded) {
206-
titleLinearLayout.setBackground(titleBackgroundExpanded);
211+
titleLinearLayout.setBackground(drawable);
207212
}
208213
}
209214

@@ -215,35 +220,35 @@ public void setArrow(Drawable drawable) {
215220
arrow.setImageDrawable(drawable);
216221
}
217222

218-
public void setArrowTint(int arrowTint) {
219-
if (this.arrowTint == arrowTint) {
223+
public void setArrowTint(@ColorInt int color) {
224+
if (this.arrowTint == color) {
220225
return;
221226
}
222227
if (this.arrowTint == arrowTintExpanded) {
223-
arrowTintExpanded = arrowTint;
228+
arrowTintExpanded = color;
224229
if (expanded) {
225-
if (Color.TRANSPARENT == arrowTint) {
230+
if (Color.TRANSPARENT == color) {
226231
arrow.clearColorFilter();
227232
} else {
228-
arrow.setColorFilter(arrowTint);
233+
arrow.setColorFilter(color);
229234
}
230235
}
231236
}
232-
this.arrowTint = arrowTint;
233-
if (Color.TRANSPARENT == arrowTint) {
237+
this.arrowTint = color;
238+
if (Color.TRANSPARENT == color) {
234239
arrow.clearColorFilter();
235240
} else if (!expanded) {
236-
arrow.setColorFilter(arrowTint);
241+
arrow.setColorFilter(color);
237242
}
238243
}
239244

240-
public void setArrowTintExpanded(int arrowTintExpanded) {
241-
if (this.arrowTintExpanded == arrowTintExpanded) {
245+
public void setArrowTintExpanded(@ColorInt int color) {
246+
if (this.arrowTintExpanded == color) {
242247
return;
243248
}
244-
this.arrowTintExpanded = arrowTintExpanded;
249+
this.arrowTintExpanded = color;
245250
if (expanded) {
246-
arrow.setColorFilter(arrowTintExpanded);
251+
arrow.setColorFilter(color);
247252
}
248253
}
249254

@@ -268,39 +273,39 @@ public void setUseDivider(boolean useDivider) {
268273
divider.setVisibility(useDivider ? VISIBLE : GONE);
269274
}
270275

271-
public void setDividerHeight(int pxHeight) {
272-
int height = (int) px2dpf(pxHeight);
276+
public void setDividerHeight(int height) {
277+
height = (int) dpf(height);
273278
if (dividerHeight == height) {
274279
return;
275280
}
276281
dividerHeight = height;
277282
divider.setLayoutParams(createLinear(LayoutParams.MATCH_PARENT, dividerHeight));
278283
}
279284

280-
public void setDividerColor(int dividerColor) {
281-
divider.setBackgroundColor(dividerColor);
285+
public void setDividerColor(@ColorInt int color) {
286+
divider.setBackgroundColor(color);
282287
}
283288

284289
public void setTitleText(String title) {
285290
this.title.setText(title);
286291
}
287292

288-
public void setTitleTextColor(int titleTextColor) {
289-
this.titleTextColor = titleTextColor;
293+
public void setTitleTextColor(@ColorInt int color) {
294+
this.titleTextColor = color;
290295
if (!expanded) {
291-
title.setTextColor(titleTextColor);
296+
title.setTextColor(color);
292297
}
293298
}
294299

295-
public void setTitleTextColorExpanded(int titleTextColorExpanded) {
296-
this.titleTextColorExpanded = titleTextColorExpanded;
300+
public void setTitleTextColorExpanded(@ColorInt int color) {
301+
this.titleTextColorExpanded = color;
297302
if (expanded) {
298-
title.setTextColor(titleTextColorExpanded);
303+
title.setTextColor(color);
299304
}
300305
}
301306

302-
public void setTitleTextSize(int titleTextColor) {
303-
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, titleTextColor);
307+
public void setTitleTextSize(int size) {
308+
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size);
304309
}
305310

306311
public void setTitleTypeface(Typeface titleTypeface) {
@@ -321,13 +326,13 @@ public void setHtmlContent(String content) {
321326
this.content.setMovementMethod(LinkMovementMethod.getInstance());
322327
}
323328

324-
public void setContentTextColor(int contentTextColor) {
325-
this.contentTextColor = contentTextColor;
326-
content.setTextColor(contentTextColor);
329+
public void setContentTextColor(@ColorInt int color) {
330+
this.contentTextColor = color;
331+
content.setTextColor(color);
327332
}
328333

329-
public void setContentTextSize(int contentTextColor) {
330-
content.setTextSize(TypedValue.COMPLEX_UNIT_DIP, contentTextColor);
334+
public void setContentTextSize(int size) {
335+
content.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size);
331336
}
332337

333338
public void setContentTypeface(Typeface contentTypeface) {
@@ -427,14 +432,14 @@ private void initView(int dividerColor) {
427432
title.setMaxLines(2);
428433
title.setEllipsize(TextUtils.TruncateAt.END);
429434
titleLinearLayout.addView(title, createLinear(0, LayoutParams.WRAP_CONTENT, 1, Gravity.CENTER_VERTICAL));
430-
titleLinearLayout.setPadding((int) px2dpf(10), 0, (int) px2dpf(10), 0);
431-
titleLinearLayout.addView(arrow, createLinear((int) px2dpf(24), (int) px2dpf(24), 0, Gravity.CENTER_VERTICAL, 10, 0, 0, 0));
432-
titleHeight = (int) px2dpf(60);
435+
titleLinearLayout.setPadding((int) dpf(10), 0, (int) dpf(10), 0);
436+
titleLinearLayout.addView(arrow, createLinear((int) dpf(24), (int) dpf(24), 0, Gravity.CENTER_VERTICAL, 10, 0, 0, 0));
437+
titleHeight = (int) dpf(60);
433438
addView(titleLinearLayout, createLinear(LayoutParams.MATCH_PARENT, titleHeight));
434439
divider.setBackgroundColor(dividerColor);
435440
divider.setVisibility(useDivider ? VISIBLE : GONE);
436441
addView(divider, createLinear(LayoutParams.MATCH_PARENT, dividerHeight, 0, Gravity.NO_GRAVITY, 0, 0, 0, 5));
437-
content.setPadding((int) px2dpf(10), 0, (int) px2dpf(10), (int) px2dpf(10));
442+
content.setPadding((int) dpf(10), 0, (int) dpf(10), (int) dpf(10));
438443
addView(content, createLinear(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
439444
post(() -> expandOrCollapseContent(expanded, false));
440445
}
@@ -443,7 +448,7 @@ private void requestHeight() {
443448
int widthMS = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST);
444449
int heightMS = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
445450
content.measure(widthMS, heightMS);
446-
contentHeight = (int) (content.getMeasuredHeight() + px2dpf(10));
451+
contentHeight = (int) (content.getMeasuredHeight() + dpf(10));
447452
}
448453

449454
@Keep
@@ -565,7 +570,7 @@ private void PaintDrawableSetCornerRadius(PaintDrawable drawable, boolean allCor
565570
}
566571
}
567572

568-
private float px2dpf(float value) {
573+
private float dpf(float value) {
569574
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics());
570575
}
571576

@@ -581,10 +586,10 @@ private LinearLayout.LayoutParams createLinear(int w, int h, int weight, int gra
581586
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(w, h);
582587
lp.weight = weight;
583588
lp.gravity = gravity;
584-
lp.topMargin = (int) px2dpf(mt);
585-
lp.bottomMargin = (int) px2dpf(mb);
586-
lp.setMarginStart((int) px2dpf(ms));
587-
lp.setMarginEnd((int) px2dpf(me));
589+
lp.topMargin = (int) dpf(mt);
590+
lp.bottomMargin = (int) dpf(mb);
591+
lp.setMarginStart((int) dpf(ms));
592+
lp.setMarginEnd((int) dpf(me));
588593
return lp;
589594
}
590595

@@ -593,9 +598,10 @@ private LinearLayout.LayoutParams createLinear(int w, int h, int weight, int gra
593598
public final static int REVERSE_QUARTER = -90;
594599

595600
@Retention(RetentionPolicy.SOURCE)
596-
public @interface RotationModel {
597-
int QUARTER = DropdownView.QUARTER;
598-
int HALF = DropdownView.HALF;
599-
int REVERSE_QUARTER = DropdownView.REVERSE_QUARTER;
600-
}
601+
@IntDef({
602+
QUARTER,
603+
HALF,
604+
REVERSE_QUARTER,
605+
})
606+
public @interface RotationModel {}
601607
}

0 commit comments

Comments
 (0)