补充 private void showPopupMenu(View view) {
final PopupAdapter adapter = (PopupAdapter) getListAdapter(); // Retrieve the clicked item from view's tag
final String item = (String) view.getTag(); // Create a PopupMenu, giving it the clicked view for an anchor
PopupMenu popup = new PopupMenu(getActivity(), view); // Inflate our menu resource into the PopupMenu's Menu
popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu()); // Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_remove:
// Remove the item from the adapter
adapter.remove(item);
return true;
}
return false;
}
}); // Finally show the PopupMenu
popup.show();
}class PopupAdapter extends ArrayAdapter<String> { PopupAdapter(ArrayList<String> items) {
super(getActivity(), R.layout.list_item, android.R.id.text1, items);
} @Override
public View getView(int position, View convertView, ViewGroup container) {
// Let ArrayAdapter inflate the layout and set the text
View view = super.getView(position, convertView, container);
// Retrieve the popup button from the inflated view
View popupButton = view.findViewById(R.id.button_popup); // Set the item as the button's tag so it can be retrieved later
popupButton.setTag(getItem(position)); // Set the fragment instance as the OnClickListener
popupButton.setOnClickListener(PopupListFragment.this);
// Finally return the view to be displayed
return view;
}
}
final PopupAdapter adapter = (PopupAdapter) getListAdapter(); // Retrieve the clicked item from view's tag
final String item = (String) view.getTag(); // Create a PopupMenu, giving it the clicked view for an anchor
PopupMenu popup = new PopupMenu(getActivity(), view); // Inflate our menu resource into the PopupMenu's Menu
popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu()); // Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_remove:
// Remove the item from the adapter
adapter.remove(item);
return true;
}
return false;
}
}); // Finally show the PopupMenu
popup.show();
}class PopupAdapter extends ArrayAdapter<String> { PopupAdapter(ArrayList<String> items) {
super(getActivity(), R.layout.list_item, android.R.id.text1, items);
} @Override
public View getView(int position, View convertView, ViewGroup container) {
// Let ArrayAdapter inflate the layout and set the text
View view = super.getView(position, convertView, container);
// Retrieve the popup button from the inflated view
View popupButton = view.findViewById(R.id.button_popup); // Set the item as the button's tag so it can be retrieved later
popupButton.setTag(getItem(position)); // Set the fragment instance as the OnClickListener
popupButton.setOnClickListener(PopupListFragment.this);
// Finally return the view to be displayed
return view;
}
}
解决方案 »
- scrollview里面的图片如何实现竖直循环???很有挑战的问题
- spinner adapter问题
- GridView有菜单项,现需增加触发事件,在线等
- 安卓本地软件修改密码的实现原理。
- 请问如何在设置VideoView的全屏播放和保持视频原尺寸播放
- subMenu标题背景颜色设置?
- 求解android_maven编译报错原因
- 关于Android sqlite
- 怎么不显示界面了自定义内部类baseadapter实现gridview从R文件取id
- 报异常java.net.MalformedURLException: no protocol:
- android中startManagingCursor过期问题
- android 悬浮菜单
的确是.. 用模拟器不好模拟,手机调试就能出现这种状态。
不过实验了一下,按照示例的程序,用post方法弹出菜单,也会出现这种问题,弹出的菜单位置不对。