首页 > 解决方案 > 当我们单击相应的项目时,如何在每个 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);
            }
        }
    }`

标签: gridviewpositionpopupwindow

解决方案


    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;
            }

推荐阅读