补充 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;
}
}
解决方案 »
- android 如何采用rtp协议进行传输
- android客户端与服务器端除了通过wifi和蓝牙还能通过什么交换
- wifi扫描热点后如何判断热点是否需要输入密码才能连接
- iMX53 QSB板怎么使用fastboot功能下载镜像到SD卡,我的老是不行!
- Android平台下三维应用的开发技术有哪些?
- activity如何给dialog传信息?
- 求助高难度问题:Dialog大小问题
- ACSII码与二进制相互转化?
- android开发:把一个byte数组转换成wav音频文件,并且播放
- appcompatv7出错,求大神帮助
- android中startManagingCursor过期问题
- android 悬浮菜单
的确是.. 用模拟器不好模拟,手机调试就能出现这种状态。
不过实验了一下,按照示例的程序,用post方法弹出菜单,也会出现这种问题,弹出的菜单位置不对。