From 6012191345d574db2b79a744c7b6a54e56925d91 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 10 Apr 2016 00:46:37 +0200 Subject: [PATCH] Fix for ClassCastException in PopupMenu --- .../widget/menu/CustomPopupMenuHelper.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/solovyev/android/widget/menu/CustomPopupMenuHelper.java b/app/src/main/java/org/solovyev/android/widget/menu/CustomPopupMenuHelper.java index 105251ba..e8ff4ad8 100644 --- a/app/src/main/java/org/solovyev/android/widget/menu/CustomPopupMenuHelper.java +++ b/app/src/main/java/org/solovyev/android/widget/menu/CustomPopupMenuHelper.java @@ -32,6 +32,7 @@ import android.support.v7.widget.ListPopupWindow; import android.view.*; import android.view.View.MeasureSpec; import android.widget.*; +import org.solovyev.android.Check; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -432,16 +433,32 @@ public class CustomPopupMenuHelper implements AdapterView.OnItemClickListener, V final MenuItemImpl item = getItem(position); final ActionProvider actionProvider = MenuItemCompat.getActionProvider(item); if (actionProvider != null) { - return actionProvider.onCreateActionView(item); + final View actionView = actionProvider.onCreateActionView(item); + fixLayoutParams(actionView, parent); + return actionView; } final View actionView = MenuItemCompat.getActionView(item); if (actionView != null) { ((MenuView.ItemView) actionView).initialize(item, 0); + fixLayoutParams(actionView, parent); return actionView; } return getDefaultView(item, convertView, parent); } + private void fixLayoutParams(View actionView, ViewGroup parent) { + if (parent instanceof FrameLayout) { + // width measure pass, nothing to be done + return; + } + Check.isTrue(parent instanceof AbsListView); + final ViewGroup.LayoutParams lp = actionView.getLayoutParams(); + if (lp != null && !(lp instanceof AbsListView.LayoutParams)) { + // see android.widget.AbsListView.generateDefaultLayoutParams() + actionView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + } + } + @Nonnull private View getDefaultView(MenuItemImpl item, View convertView, ViewGroup parent) { if (convertView == null || convertView.getTag(DEFAULT_VIEW_TAG_KEY) != DEFAULT_VIEW_TAG) {