我想要实现的目标:
我有一个自定义的ListView适配器。我想要给每个Listitem添加一个弹出菜单,这个菜单非常类似于当前Google Play应用程序中的ListView。
这是我尝试过的内容: 我的大部分代码都来自于这个Android示例samples\android-19\ui\ActionBarCompat-ListPopupMenu
CustomFragmentPageAdapter.java:
我有一个自定义的ListView适配器。我想要给每个Listitem添加一个弹出菜单,这个菜单非常类似于当前Google Play应用程序中的ListView。
这是我尝试过的内容: 我的大部分代码都来自于这个Android示例samples\android-19\ui\ActionBarCompat-ListPopupMenu
CustomFragmentPageAdapter.java:
// 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();
}
自定义数组适配器:
public class CustomAdapter extends ArrayAdapter<WatchListPlayerItem> {
...
@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:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/install"
android:title="Install" />
<item
android:id="@+id/addtowishlist"
android:title="Add to wishlist" />
</menu>
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.<init>(ViewGroup.java:6591)
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:735)
...
在我的CustomFragment中,弹出窗口使用popup.show()时会引发错误。
这个错误明显让我疯狂,非常感谢任何帮助解决这个问题!