Acessing从片段中的“选项”菜单? Android版 [英] Acessing the 'options' menu from a fragment? Android

查看:164
本文介绍了Acessing从片段中的“选项”菜单? Android版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有,正在使用的片段由pageViewer,这样我可以在它们之间添加刷卡有4页的应用程序这些网页。

I currently have an app which has 4 pages, these pages are being added using fragments by a pageViewer, so that I can swipe between them.

我要的,是要能够访问选项菜单的每个页面(默认情况下中有一个设置项中的一个),这样我就可以一页专门执行命令,如一个刷新项,刷新当前页面的数据。

What I want, is to be able to access the 'options' menu (the one which has a 'settings' item in it by default) from each page, so that I can perform commands specifically for one page, such as a 'refresh' item, which refreshes the data in the current page.

任何人都可以点我在正确的方向?
谢谢!

Could anyone point me in the right direction? Thanks!

推荐答案

每个片段可以声明自己的菜单,这将与当前活动的菜单自动合并。

Each Fragment can declare its own menu, which will be automatically merged with the current activity's menu.

class YourPage extends Fragment
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Signal that this fragment has proper actions
        setHasOptionsMenu(true);
    }

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

        // The menu will be added to the action bar
        inflater.inflate(R.menu.fragment_page_menu, menu);      
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.action_refresh:
            {
                // Handle the action...

                return true;
            }


            default:
                return super.onOptionsItemSelected(item);
        }       
    }   
}

下面是一个完整的例如的,我做,而且相关的教程

Here's a complete example that I made, and also a relevant tutorial.

这篇关于Acessing从片段中的“选项”菜单? Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆