当我们单击相应的项目时如何在每个gridView项的顶部显示弹出窗口?

问题描述 投票:0回答:1

弹出窗口方法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);
        }
    }
}`

Gridview类

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);
            }
        }
    }`
gridview position popupwindow
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.