我想创建一个与Play Store相同的弹出菜单。弹出菜单应在网格视图项的右下角打开。但是,如果该项目位于屏幕底部,则弹出窗口应在单击它的位置上方打开。
我试过弹出菜单,但它在项目下方或顶部打开。
category_GV.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
System.out.println("position is : " + position);
final MenuItems menuItems = (MenuItems)parent.getItemAtPosition(position);
final String default_topping_link = menuItems.getLink();
try{
topping = DefaultToppingParser.parse(new FileInputStream(default_topping_link));
for(int i=0;i<topping.size();i++){
System.out.println("topping id : " + topping.get(i));
}
}catch(Exception e){
e.printStackTrace();
}
if(position == (burger_item_AL.size()-1)){
Intent intent = new Intent(MainActivity.this,CustomiseItem.class);
intent.putExtra("default_toppings_id", base_id);
System.out.println("intent");
startActivity(intent);
} else {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
....some coding
return true;
}
});
}
}
});
我在 Google 上浏览了许多弹出菜单,但找不到与 Play 商店相同的菜单。任何人都可以提出一些解决方案吗?
弹出窗口:
在适配器类中:
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
MainActivity.p = new Point();
MainActivity.p.x = (int) event.getRawX();
MainActivity.p.y = (int) event.getRawY();
if (MainActivity.p != null){
MainActivity.position = position;
MainActivity.showPopup(context, MainActivity.p);
}
return true;
}
return false;
}
});
显示弹出方法:
public static void showPopup(final Context context, Point p) {
int popupWidth = 150;
int popupHeight = 150;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) ((Activity) context).findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
System.out.println("showing popup");
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x - OFFSET_X, p.y + OFFSET_Y);
}