Custom PopupMenu (layout)

前端 未结 2 759
忘掉有多难
忘掉有多难 2020-12-03 08:29

I\'m trying to upgrade my PopupMenu so it would come with icons and custom styles.
I have created a new layout for it



        
2条回答
  •  执念已碎
    2020-12-03 09:11

    Here is my demo for custom ListPopupWindow by custom adapter

    Model

    public class ListPopupItem {
        private String title;
        private int imageRes;
    
        public ListPopupItem(String title, int imageRes) {
            this.title = title;
            this.imageRes = imageRes;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public int getImageRes() {
            return imageRes;
        }
    }
    

    ListAdapter

    public class ListPopupWindowAdapter extends BaseAdapter {
        private List items;
    
        public ListPopupWindowAdapter(List items) {
            this.items = items;
        }
    
        @Override
        public int getCount() {
            return items.size();
        }
    
        @Override
        public ListPopupItem getItem(int i) {
            return items.get(i);
        }
    
        @Override
        public long getItemId(int i) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_popup, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            holder.tvTitle.setText(getItem(position).getTitle());
            holder.ivImage.setImageResource(getItem(position).getImageRes());
            return convertView;
        }
    
        static class ViewHolder {
            TextView tvTitle;
            ImageView ivImage;
    
            ViewHolder(View view) {
                tvTitle = view.findViewById(R.id.text);
                ivImage = view.findViewById(R.id.image);
            }
        }
    }
    

    item_list_popup.xml

    
        
        
    
    

    Finally, show ListPopupWindow
    (In this demo, I show MATCH_PARENT popup here, you can show a specific with for popup (eg: 100px, 200px,...) If you set popup width = WRAP_CONTENT, popup width will equals ANCHOR width)

    private void showListPopupWindow(View anchor) {
        List listPopupItems = new ArrayList<>();
        listPopupItems.add(new ListPopupItem("Menu 1", R.mipmap.ic_launcher));
        listPopupItems.add(new ListPopupItem("Menu 2", R.mipmap.ic_launcher));
        listPopupItems.add(new ListPopupItem("Menu 3", R.mipmap.ic_launcher));
    
        final ListPopupWindow listPopupWindow =
                createListPopupWindow(anchor, ViewGroup.LayoutParams.MATCH_PARENT, listPopupItems);
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                listPopupWindow.dismiss();
                Toast.makeText(MainActivity.this, "clicked at " + position, Toast.LENGTH_SHORT)
                        .show();
            }
        });
        listPopupWindow.show();
    }
    
    private ListPopupWindow createListPopupWindow(View anchor, int width,
            List items) {
        final ListPopupWindow popup = new ListPopupWindow(this);
        ListAdapter adapter = new ListPopupWindowAdapter(items);
        popup.setAnchorView(anchor);
        popup.setWidth(width);
        popup.setAdapter(adapter);
        return popup;
    }
    

    Example using

    button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
                showListPopupWindow(view);
           }
    });
    

    DEMO

提交回复
热议问题