// create new fragment
mCustomFragment = CustomFragment.newInstance(position);
CustomFragment.java
public class CustomFragment extends ListFragment implements View.OnClickListener{
...
@Override
public void onClick(final View v) {
v.post(new Runnable() {
@Override
public void run() {
showPopupMenu(v);
}
});
}
private void showPopupMenu(View view) {
PopupMenu popup = new PopupMenu(getActivity(), view);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
}
CustomArrayAdapter:
public class CustomAdapter extends ArrayAdapter {
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.watch_list_row, parent, false);
View popupButton = rowView.findViewById(R.id.imgPopUp);
popupButton.setTag(getItem(position));
popupButton.setOnClickListener(mFragment);
return rowView;
}
}
popup_menu.xml:
Logcat输出:
java.lang.RuntimeException: Failed to resolve attribute at index 6
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:603)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6423)
at android.view.ViewGroup$MarginLayoutParams.(ViewGroup.java:6591)
at android.widget.FrameLayout$LayoutParams.(FrameLayout.java:735)
...