gridview - 当我们单击相应的项目时,如何在每个 gridView 项目的顶部显示弹出窗口?
问题描述
弹出窗口方法
private void initiatePopupWindow() {
//instantiate the popup.xml layout file
LayoutInflater layoutInflater = (LayoutInflater) ProductListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.productlist_popup_menu,null);
//instantiate popup window
final PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);
//display the popup window
popupWindow.showAtLocation(buttonTestingLayout, Gravity.CENTER, 0, 0);
}
GridView 类代码``公共类 ProductListRecyclerViewAdapter 扩展 RecyclerView.Adapter {
String[] productName;
LayoutInflater mInflater;
int[] imageId;
ProductListRecyclerViewAdapter(Context context, String[] data, int[] imageId) {
this.mInflater = LayoutInflater.from(context);
this.productName = data;
this.imageId = imageId;
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.row_product_grid, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.tvProductName.setText(productName[position]);
holder.ivPricePopupMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initiatePopupWindow();
}
});
if (position % 2 == 0) {
holder.ivProductImage.setImageResource(R.drawable.infra_bazaar_backhoe);
} else if (position % 3 == 0) {
holder.ivProductImage.setImageResource(R.drawable.infra_bazaar_crane);
} else {
holder.ivProductImage.setImageResource(R.drawable.infra_bazaar_excavator);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toast(productName[position]);
}
});
}
private void initiatePopupWindow() {
//instantiate the popup.xml layout file
LayoutInflater layoutInflater = (LayoutInflater) ProductListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.productlist_popup_menu,null);
//instantiate popup window
final PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);
//display the popup window
popupWindow.showAtLocation(buttonTestingLayout, Gravity.CENTER, 0, 0);
}
@Override
public int getItemCount() {
return productName.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvProductName;
ImageView ivProductImage, ivPricePopupMenu;
ViewHolder(View itemView) {
super(itemView);
tvProductName = itemView.findViewById(R.id.tvProductName);
ivProductImage = itemView.findViewById(R.id.ivProductImage);
ivPricePopupMenu = itemView.findViewById(R.id.ivPricePopupMenu);
}
}
}`
网格视图类
public class ProductListRecyclerViewAdapter extends RecyclerView.Adapter<ProductListRecyclerViewAdapter.ViewHolder> {
String[] productName;
LayoutInflater mInflater;
int[] imageId;
ProductListRecyclerViewAdapter(Context context, String[] data, int[] imageId) {
this.mInflater = LayoutInflater.from(context);
this.productName = data;
this.imageId = imageId;
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.row_product_grid, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.tvProductName.setText(productName[position]);
holder.ivPricePopupMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initiatePopupWindow();
}
});
if (position % 2 == 0) { holder.ivProductImage.setImageResource(R.drawable.infra_bazaar_backhoe); } else if (position % 3 == 0) { holder.ivProductImage.setImageResource(R.drawable.infra_bazaar_crane);
} else { holder.ivProductImage.setImageResource(R.drawable.infra_bazaar_excavator);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toast(productName[position]);
}
});
}
private void initiatePopupWindow() {
//instantiate the popup.xml layout file
LayoutInflater layoutInflater = (LayoutInflater) ProductListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.productlist_popup_menu,null);
//instantiate popup window
final PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);
//display the popup window
popupWindow.showAtLocation(buttonTestingLayout, Gravity.CENTER, 0, 0);
}
@Override
public int getItemCount() {
return productName.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvProductName;
ImageView ivProductImage, ivPricePopupMenu;
ViewHolder(View itemView) {
super(itemView);
tvProductName = itemView.findViewById(R.id.tvProductName);
ivProductImage = itemView.findViewById(R.id.ivProductImage);
ivPricePopupMenu = itemView.findViewById(R.id.ivPricePopupMenu);
}
}
}`
解决方案
I got the solution. we need to create one separate function to get the location and set return value in popupWindow.showAtLocation();
//ivPricePopupMenu is item on which we need to call popupWindow
ivPricePopupMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//instantiate the popup.xml layout file
LayoutInflater layoutInflater = (LayoutInflater) ProductListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.productlist_popup_menu,null);
//instantiate popup window
final PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);
//display the popup window
popupWindow.showAtLocation(buttonTestingLayout, Gravity.NO_GRAVITY,locateView(view).right,locateView(view).bottom);
}
});
//Method which return particular position of any view
public Rect locateView(View v)
{
int[] loc_int = imageId;
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
//For right and bottom side popup
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}
推荐阅读
- bash - 仅迭代存储在 git 中的文件
- python - 迭代 pandas 系列,if 和 isin 以执行不同的操作
- python - 如何打印具有给定错误概率的列表的二进制数(Python)
- python - 我正在用 Zelle graphics.py 制作圣诞灯笼,我希望中等圆圈闪烁随机彩色灯
- python - 来自导入模块的 Python 模拟函数
- multithreading - Spark/EMR 可以从 s3 多线程读取数据吗
- swift - wkwebviews之间的Cookies共享
- ruby-on-rails - 控制器方法中的命令注入问题
- javascript - 如何在“out”(可放置)事件中操纵可拖动的位置?
- python - 使用 for 循环和打印/提取序列(python)从文件夹中打开和解析多个 .fasta 文件