Skip to content

Commit 3484274

Browse files
committed
Add divider support for menu popup
1 parent 6d80839 commit 3484274

File tree

4 files changed

+164
-55
lines changed

4 files changed

+164
-55
lines changed

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/Dynamic.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,16 +1612,12 @@ public static <T extends View> void tintDivider(@Nullable T view) {
16121612
* @see LinearLayout#setShowDividers(int)
16131613
* @see LinearLayoutCompat#setShowDividers(int)
16141614
*/
1615-
public static <T> void setShowDividers(@Nullable T view, int showDividers) {
1615+
public static <T> void setShowDividersNoInspection(@Nullable T view, int showDividers) {
16161616
if (view == null || !Dynamic.isExpressiveVersion()) {
16171617
return;
16181618
}
16191619

1620-
if (view instanceof View && ((((View) view).getParent() instanceof DynamicSurfaceWidget
1621-
&& !((DynamicSurfaceWidget) ((View) view).getParent()).isForceElevation())
1622-
|| ((View) view).getParent() instanceof DynamicInfoView
1623-
|| ((View) view).getParent() instanceof DynamicInfoViewBig
1624-
|| ((View) view).getParent() instanceof DynamicInfoViewBigAuthor)) {
1620+
if (view instanceof View) {
16251621
if (view instanceof LinearLayout) {
16261622
((LinearLayout) view).setShowDividers(showDividers);
16271623
} else if (view instanceof LinearLayoutCompat) {
@@ -1632,6 +1628,31 @@ public static <T> void setShowDividers(@Nullable T view, int showDividers) {
16321628
}
16331629
}
16341630

1631+
/**
1632+
* Show dividers according to the supplied view.
1633+
*
1634+
* @param view The view object to show dividers.
1635+
* @param showDividers The flag to show dividers.
1636+
* @param <T> The type of the view object.
1637+
*
1638+
* @see #setShowDividersNoInspection(Object, int)
1639+
*/
1640+
public static <T> void setShowDividers(@Nullable T view, int showDividers) {
1641+
if (view == null || !Dynamic.isExpressiveVersion()) {
1642+
return;
1643+
}
1644+
1645+
if (view instanceof View && ((((View) view).getParent() instanceof DynamicSurfaceWidget
1646+
&& !((DynamicSurfaceWidget) ((View) view).getParent()).isForceElevation())
1647+
|| ((View) view).getParent() instanceof DynamicInfoView
1648+
|| ((View) view).getParent() instanceof DynamicInfoViewBig
1649+
|| ((View) view).getParent() instanceof DynamicInfoViewBigAuthor)) {
1650+
setShowDividersNoInspection(view, showDividers);
1651+
} else if (view instanceof Menu) {
1652+
setShowDividersNoInspection(view, showDividers);
1653+
}
1654+
}
1655+
16351656
/**
16361657
* Set on click listener for the dynamic object.
16371658
*

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/adapter/DynamicSpinnerChoiceAdapter.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.widget.AdapterView;
2525
import android.widget.BaseAdapter;
2626
import android.widget.ImageView;
27+
import android.widget.LinearLayout;
2728
import android.widget.TextView;
2829

2930
import androidx.annotation.NonNull;
@@ -80,6 +81,11 @@ public class DynamicSpinnerChoiceAdapter extends BaseAdapter {
8081
*/
8182
private boolean[] mHasSubmenus;
8283

84+
/**
85+
* Array of divider states used by this adapter.
86+
*/
87+
private boolean[] mDividers;
88+
8389
/**
8490
* The selected position used by this adapter.
8591
*/
@@ -333,7 +339,7 @@ public View getView(final int position,
333339
}
334340

335341
if (mOnItemClickListener != null) {
336-
Dynamic.setOnClickListener(holder.getRoot(), new View.OnClickListener() {
342+
Dynamic.setOnClickListener(holder.getItem(), new View.OnClickListener() {
337343
@Override
338344
public void onClick(View view) {
339345
mOnItemClickListener.onItemClick((AdapterView<?>) parent,
@@ -344,7 +350,7 @@ public void onClick(View view) {
344350
}
345351
});
346352
} else {
347-
Dynamic.setClickable(holder.getRoot(), false);
353+
Dynamic.setClickable(holder.getItem(), false);
348354
}
349355

350356
Dynamic.set(holder.getIcon(), getIcon(parent.getContext(), position));
@@ -371,6 +377,11 @@ public void onClick(View view) {
371377
mSelectedPosition == position ? View.VISIBLE : View.INVISIBLE);
372378
}
373379

380+
if (getDividers() != null) {
381+
Dynamic.setShowDividersNoInspection(holder.getRoot(), getDividers()[position]
382+
? LinearLayout.SHOW_DIVIDER_BEGINNING : LinearLayout.SHOW_DIVIDER_NONE);
383+
}
384+
374385
return convertView;
375386
}
376387

@@ -533,6 +544,26 @@ public void setHasSubmenus(@Nullable boolean[] entries) {
533544
notifyDataSetChanged();
534545
}
535546

547+
/**
548+
* Get the divider states used by this popup.
549+
*
550+
* @return The divider states used by this popup.
551+
*/
552+
public @Nullable boolean[] getDividers() {
553+
return mDividers;
554+
}
555+
556+
/**
557+
* Set the divider states for this popup.
558+
*
559+
* @param dividers The divider states to be set.
560+
*/
561+
public void setDividers(@Nullable boolean[] dividers) {
562+
this.mDividers = dividers;
563+
564+
notifyDataSetChanged();
565+
}
566+
536567
/**
537568
* Get the item click listener.
538569
*
@@ -584,6 +615,11 @@ static class ViewHolder {
584615
*/
585616
private final ViewGroup root;
586617

618+
/**
619+
* Item view layout.
620+
*/
621+
private final ViewGroup item;
622+
587623
/**
588624
* Image view to show the icon.
589625
*/
@@ -610,7 +646,8 @@ static class ViewHolder {
610646
* @param view The view for this view holder.
611647
*/
612648
ViewHolder(@NonNull View view) {
613-
root = view.findViewById(R.id.ads_array_item);
649+
root = view.findViewById(R.id.ads_array_item_root);
650+
item = view.findViewById(R.id.ads_array_item);
614651
icon = view.findViewById(R.id.ads_array_item_icon);
615652
title = view.findViewById(R.id.ads_array_item_title);
616653
subtitle = view.findViewById(R.id.ads_array_item_subtitle);
@@ -626,6 +663,15 @@ static class ViewHolder {
626663
return root;
627664
}
628665

666+
/**
667+
* Get the item view layout.
668+
*
669+
* @return The item view layout.
670+
*/
671+
@NonNull ViewGroup getItem() {
672+
return item;
673+
}
674+
629675
/**
630676
* Get the image view to show the icon.
631677
*

dynamic-support/src/main/java/com/pranavpandey/android/dynamic/support/popup/DynamicMenuPopup.java

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public class DynamicMenuPopup extends DynamicSimplePopup {
7979
*/
8080
private boolean[] mHasSubmenus;
8181

82+
/**
83+
* Divider states used by this popup.
84+
*/
85+
private boolean[] mDividers;
86+
8287
/**
8388
* The selected position.
8489
*/
@@ -641,23 +646,35 @@ public DynamicMenuPopup(@NonNull View anchor, @Nullable int[] iconsRes,
641646
}
642647

643648
if (mOnItemClickListener != null) {
644-
listView.setAdapter(new DynamicSpinnerChoiceAdapter(mIconsRes, mIcons,
645-
mTints, mTitles, mSubtitles, mHasSubmenus, mSelectedPosition,
646-
new AdapterView.OnItemClickListener() {
647-
@Override
648-
public void onItemClick(AdapterView<?> adapterView,
649-
View view, int position, long id) {
650-
mOnItemClickListener.onItemClick(adapterView, view, position, id);
651-
652-
dismiss();
653-
}
654-
}));
649+
listView.setAdapter(getDynamicSpinnerChoiceAdapter());
655650
}
656651

657652
setViewRoot(listView);
658653
return this;
659654
}
660655

656+
/**
657+
* Returns the adapter for the list view.
658+
*
659+
* @return The adapter for the list view.
660+
*/
661+
protected @NonNull DynamicSpinnerChoiceAdapter getDynamicSpinnerChoiceAdapter() {
662+
DynamicSpinnerChoiceAdapter adapter = new DynamicSpinnerChoiceAdapter(mIconsRes,
663+
mIcons, mTints, mTitles, mSubtitles, mHasSubmenus, mSelectedPosition,
664+
new AdapterView.OnItemClickListener() {
665+
@Override
666+
public void onItemClick(AdapterView<?> adapterView,
667+
View view, int position, long id) {
668+
mOnItemClickListener.onItemClick(adapterView, view, position, id);
669+
670+
dismiss();
671+
}
672+
});
673+
674+
adapter.setDividers(getDividers());
675+
return adapter;
676+
}
677+
661678
@Override
662679
protected @Nullable View getView() {
663680
return mView;
@@ -773,6 +790,24 @@ public void setHasSubmenus(@Nullable boolean[] hasSubmenus) {
773790
this.mHasSubmenus = hasSubmenus;
774791
}
775792

793+
/**
794+
* Get the divider states used by this popup.
795+
*
796+
* @return The divider states used by this popup.
797+
*/
798+
public @Nullable boolean[] getDividers() {
799+
return mDividers;
800+
}
801+
802+
/**
803+
* Set the divider states for this popup.
804+
*
805+
* @param dividers The divider states to be set.
806+
*/
807+
public void setDividers(@Nullable boolean[] dividers) {
808+
this.mDividers = dividers;
809+
}
810+
776811
/**
777812
* Get the selected position.
778813
*

dynamic-support/src/main/res/layout/ads_layout_array_item_popup.xml

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,55 @@
1818
<LinearLayout
1919
xmlns:android="http://schemas.android.com/apk/res/android"
2020
xmlns:app="http://schemas.android.com/apk/res-auto"
21-
style="?attr/ads_widgetContentCardClickableBackground"
22-
android:id="@+id/ads_array_item"
23-
android:orientation="horizontal">
24-
25-
<com.pranavpandey.android.dynamic.support.widget.DynamicImageView
26-
android:id="@+id/ads_array_item_icon"
27-
android:layout_width="@dimen/ads_popup_selector_icon_size"
28-
android:layout_height="@dimen/ads_popup_selector_icon_size"
29-
android:layout_marginRight="@dimen/ads_page_horizontal_margin"
30-
android:layout_marginEnd="@dimen/ads_page_horizontal_margin"
31-
android:layout_gravity="center_vertical"
32-
app:adt_colorType="tint_surface"
33-
app:adt_contrastWithColorType="surface" />
21+
style="?attr/ads_widgetContentRowVertical"
22+
android:id="@+id/ads_array_item_root"
23+
android:divider="@drawable/ads_divider_horizontal">
3424

3525
<LinearLayout
36-
style="?attr/ads_widgetContentRowVerticalWeight">
26+
style="?attr/ads_widgetContentCardClickableBackground"
27+
android:id="@+id/ads_array_item"
28+
android:orientation="horizontal">
3729

38-
<com.pranavpandey.android.dynamic.support.widget.DynamicTextView
39-
android:id="@+id/ads_array_item_title"
40-
android:layout_width="match_parent"
41-
android:layout_height="wrap_content"
42-
android:textAppearance="?attr/ads_textAppearanceSubtitle1"
30+
<com.pranavpandey.android.dynamic.support.widget.DynamicImageView
31+
android:id="@+id/ads_array_item_icon"
32+
android:layout_width="@dimen/ads_popup_selector_icon_size"
33+
android:layout_height="@dimen/ads_popup_selector_icon_size"
34+
android:layout_marginRight="@dimen/ads_page_horizontal_margin"
35+
android:layout_marginEnd="@dimen/ads_page_horizontal_margin"
36+
android:layout_gravity="center_vertical"
37+
app:adt_colorType="tint_surface"
4338
app:adt_contrastWithColorType="surface" />
4439

45-
<com.pranavpandey.android.dynamic.support.widget.DynamicTextView
46-
android:id="@+id/ads_array_item_subtitle"
47-
android:layout_width="match_parent"
48-
android:layout_height="wrap_content"
49-
android:textAppearance="?attr/ads_textAppearanceBody1"
40+
<LinearLayout
41+
style="?attr/ads_widgetContentRowVerticalWeight">
42+
43+
<com.pranavpandey.android.dynamic.support.widget.DynamicTextView
44+
android:id="@+id/ads_array_item_title"
45+
android:layout_width="match_parent"
46+
android:layout_height="wrap_content"
47+
android:textAppearance="?attr/ads_textAppearanceSubtitle1"
48+
app:adt_contrastWithColorType="surface" />
49+
50+
<com.pranavpandey.android.dynamic.support.widget.DynamicTextView
51+
android:id="@+id/ads_array_item_subtitle"
52+
android:layout_width="match_parent"
53+
android:layout_height="wrap_content"
54+
android:textAppearance="?attr/ads_textAppearanceBody1"
55+
app:adt_contrastWithColorType="surface" />
56+
57+
</LinearLayout>
58+
59+
<com.pranavpandey.android.dynamic.support.widget.DynamicImageView
60+
android:id="@+id/ads_array_item_selector"
61+
android:layout_width="@dimen/ads_popup_selector_icon_size"
62+
android:layout_height="@dimen/ads_popup_selector_icon_size"
63+
android:layout_marginLeft="@dimen/ads_page_horizontal_margin"
64+
android:layout_marginStart="@dimen/ads_page_horizontal_margin"
65+
android:layout_gravity="center_vertical"
66+
app:srcCompat="@drawable/ads_ic_check"
67+
app:adt_colorType="accent"
5068
app:adt_contrastWithColorType="surface" />
5169

5270
</LinearLayout>
5371

54-
<com.pranavpandey.android.dynamic.support.widget.DynamicImageView
55-
android:id="@+id/ads_array_item_selector"
56-
android:layout_width="@dimen/ads_popup_selector_icon_size"
57-
android:layout_height="@dimen/ads_popup_selector_icon_size"
58-
android:layout_marginLeft="@dimen/ads_page_horizontal_margin"
59-
android:layout_marginStart="@dimen/ads_page_horizontal_margin"
60-
android:layout_gravity="center_vertical"
61-
app:srcCompat="@drawable/ads_ic_check"
62-
app:adt_colorType="accent"
63-
app:adt_contrastWithColorType="surface" />
64-
6572
</LinearLayout>

0 commit comments

Comments
 (0)