Fix for ClassCastException in PopupMenu

This commit is contained in:
serso 2016-04-10 00:46:37 +02:00
parent 629de2b2f7
commit 6012191345

View File

@ -32,6 +32,7 @@ import android.support.v7.widget.ListPopupWindow;
import android.view.*; import android.view.*;
import android.view.View.MeasureSpec; import android.view.View.MeasureSpec;
import android.widget.*; import android.widget.*;
import org.solovyev.android.Check;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -432,16 +433,32 @@ public class CustomPopupMenuHelper implements AdapterView.OnItemClickListener, V
final MenuItemImpl item = getItem(position); final MenuItemImpl item = getItem(position);
final ActionProvider actionProvider = MenuItemCompat.getActionProvider(item); final ActionProvider actionProvider = MenuItemCompat.getActionProvider(item);
if (actionProvider != null) { if (actionProvider != null) {
return actionProvider.onCreateActionView(item); final View actionView = actionProvider.onCreateActionView(item);
fixLayoutParams(actionView, parent);
return actionView;
} }
final View actionView = MenuItemCompat.getActionView(item); final View actionView = MenuItemCompat.getActionView(item);
if (actionView != null) { if (actionView != null) {
((MenuView.ItemView) actionView).initialize(item, 0); ((MenuView.ItemView) actionView).initialize(item, 0);
fixLayoutParams(actionView, parent);
return actionView; return actionView;
} }
return getDefaultView(item, convertView, parent); 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 @Nonnull
private View getDefaultView(MenuItemImpl item, View convertView, ViewGroup parent) { private View getDefaultView(MenuItemImpl item, View convertView, ViewGroup parent) {
if (convertView == null || convertView.getTag(DEFAULT_VIEW_TAG_KEY) != DEFAULT_VIEW_TAG) { if (convertView == null || convertView.getTag(DEFAULT_VIEW_TAG_KEY) != DEFAULT_VIEW_TAG) {