工具栏项目上的复杂菜单
Complex menu on Toolbar item
如何使用 ListPopupWindow 实现这样的功能?
我试图在 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);
}
我会使用 ListPopupWindow,支持库中也提供了它。顾名思义,PopupWindow
包裹在一个 List(View) 周围,它接受一个适配器。使用自定义适配器,您应该能够轻松自定义每一行的外观。溢出视图将成为您 ListPopupWindow
的锚点视图
如何使用 ListPopupWindow 实现这样的功能?
我试图在 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);
}
我会使用 ListPopupWindow,支持库中也提供了它。顾名思义,PopupWindow
包裹在一个 List(View) 周围,它接受一个适配器。使用自定义适配器,您应该能够轻松自定义每一行的外观。溢出视图将成为您 ListPopupWindow