工具栏项目上的复杂菜单

时间:2015-10-05 13:57:07

标签: java android toolbar android-appcompat

如何使用ListPopupWindow实现类似的功能?

here

我试图在onOptionsItemSelected上获取Item View实例,因此在onCreateOptionsMenu中它返回null(正如您可以看到注释行):

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_socials, menu);    //        The line bellow was returning null
//        ImageButton view = (ImageButton) menu.findItem(R.id.fake_overflow).getActionView();
        listPopupWindow = new ListPopupWindow(
                getContext());
        listPopupWindow.setAdapter(new ArrayAdapter(
                getContext(),
                android.R.layout.simple_expandable_list_item_1, products));
        listPopupWindow.setWidth(300);
        listPopupWindow.setHeight(400);
        listPopupWindow.setModal(true);    
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        View view = getActivity().findViewById(R.id.fake_overflow);
//        listPopupWindow.show();
        listPopupWindow.setAnchorView(view);
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("TAG","action click?");
                listPopupWindow.dismiss();
            }
        });
        return super.onOptionsItemSelected(item);
    }

1 个答案:

答案 0 :(得分:1)

我会使用支持库中也提供的ListPopupWindow。顾名思义,PopupWindow包含在List(View)中,它接受一个Adapter。使用自定义适配器,您应该能够轻松自定义每行的外观和感觉。溢出视图将是ListPopupWindow

的锚点视图