ListFragment - How do I add a menu?

2019-07-16 07:08发布

I have a ListFragment and I have to add a menu. This is my code: listuser_menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/any_option"
    android:title="In Context Menu" />
</menu>

My ListFragment:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    View mFooterView;
    // We need to use a different list item layout for devices older than Honeycomb
    int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
    android.R.layout.simple_list_item_activated_1:android.R.layout.simple_list_item_1;
    if(getListAdapter()==null){
        // init adapter
        adapter=new UserArrayAdapter(getActivity(),
                MOBILE_OS);
    }
    else{
            adapter.notifyDataSetChanged(); 
        } 

    // set adapter
    registerForContextMenu(getListView());
    setListAdapter(adapter);

}
@Override
public void onCreateContextMenu(final ContextMenu menu, final View v,
final ContextMenuInfo menuInfo){
    menu.clear();
    super.onCreateContextMenu(menu, v, menuInfo);   
    final MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.listuser_menu, menu);   
}

But i can't see my menu..why? How register option menu selected? Thanks!

3条回答
做自己的国王
2楼-- · 2019-07-16 07:11

Use this sample code inside your MainActivity.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);



        SubMenu subMenu1 = menu.addSubMenu("Action Item");
        subMenu1.add(0, 1, 0, "Sample");
        subMenu1.add(0, 2, 0, "Menu");
        subMenu1.add(0, 3, 0, "Sair");

        MenuItem subMenu1Item = subMenu1.getItem();
        subMenu1Item.setIcon(R.drawable.ic_title_share_default);
        subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);




        return true;
    }
查看更多
【Aperson】
3楼-- · 2019-07-16 07:12

Yes, you can add menu to ListFragment, add below code into your ListFragment

   @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);
}

And after that add Fragment version onCreateOptionsMenu() method, as given below

   @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {     
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_xml, menu);
}

@ Sherlock Yiu forgot to add "super.onActivityCreated(savedInstanceState);" into onActivityCreated() method, rest is same.

Also dont forgot to add "android-support-v4.jar" into project libraries.

Above code snippet is working for me.

查看更多
我命由我不由天
4楼-- · 2019-07-16 07:29

Adding menus to fragment is possible, use the following code in your Fragment:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
setHasOptionsMenu(true);

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.listuser_menu, menu);
}

Using setHasOptionsMenu will allow your fragment to show a menu.

查看更多
登录 后发表回答