【问题标题】:ActionbarSherlock and ViewpagerIndicator: how to add different actionbar menu to different Page in ViewPagerIndicator using fragmentActionbarSherlock 和 ViewpagerIndicator:如何使用片段将不同的操作栏菜单添加到 ViewPagerIndicator 中的不同页面
【发布时间】:2012-05-23 12:53:23
【问题描述】:

我的应用使用ActionbarsherlockViewpagerIndicator。每页使用 1 个片段。我希望每个页面都有不同的操作栏菜单,那么我该如何归档呢?

我尝试在每个SherlockFragment 上使用onCreateOptionsMenu,但操作栏菜单不显示,我也尝试找到一些以编程方式添加菜单的方法,但没有办法。

@Override
public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, com.actionbarsherlock.view.MenuInflater inflater)
{       
    inflater.inflate(R.menu.main, menu);    
    super.onCreateOptionsMenu(menu, inflater);
}

谢谢!

*编辑:添加更多细节 *

如果我在Activity中使用onCreateOptionsMenu,菜单(动作项)会正常显示。

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getSupportMenuInflater();
    menuInflater.inflate(R.menu.main, menu);

这就是我设置 ActionBar 和 ViewPager 的方式

final ActionBar actionbar = getSupportActionBar();
    actionbar .setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionbar .setDisplayHomeAsUpEnabled(true);
    actionbar .setTitle("olala");

    mFragmentAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());     
    mPager.setAdapter(mFragmentAdapter);

    mIndicator.setViewPager(mPager);
    mIndicator.setOnPageChangeListener(new MyPageChangeListener());
    mPager.setCurrentItem(3);

【问题讨论】:

    标签: android-viewpager actionbarsherlock viewpagerindicator


    【解决方案1】:

    我们需要在 Fragment 上添加这个(onCreate 或 onCreateView),以便通过接收对 onCreateOptionsMenu(Menu, MenuInflater) 和相关方法的调用来报告这个 Fragment 想要参与填充选项菜单。

    setHasOptionsMenu(true);
    

    参考:http://developer.android.com/reference/android/app/Fragment.html

    【讨论】:

    • facepalm 真的!对不起,我完全错过了:/
    【解决方案2】:

    没有调用super,我也有同样的情况。

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

    更新:

    我在 Activity 的 onCreate 中设置了这个...

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_activity);
        final ViewPager viewPager = (ViewPager) findViewById(R.id.profile_activity_pager);
    
        final Intent intent = getIntent();
    
        // I guess some setup should be done...
        final ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        bar.setDisplayHomeAsUpEnabled(true);
        if (intent.hasExtra(EXTRA_USER_NAME)) {
            bar.setTitle(intent.getStringExtra(EXTRA_USER_NAME));
        } else {
            bar.setTitle(R.string.title_profile);
        }
    
        final PagerAdapter tabsAdapter = new TabsAdapter(this);
        viewPager.setAdapter(tabsAdapter);
    
        final TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.profile_activity_tabs);
        titleIndicator.setViewPager(viewPager);
    
        if (intent.hasExtra(EXTRA_TAB_ID)) {
            final int tabId = intent.getIntExtra(EXTRA_TAB_ID, -1);
            if (tabId > -1 && tabId < tabsAdapter.getCount()) {
                viewPager.setCurrentItem(tabId);
            }
        }
    }
    

    【讨论】:

    • 我只是在清单上的 Activity 中使用操作栏样式。我错过了什么吗?
    • 我为操作栏设置了一些东西,也许你也应该尝试一下
    • 感谢您的支持,但菜单(操作项)仍未显示。我只是在这个问题上添加了更多细节。
    • 您能否尝试将全局主题设置为Theme.Sherlock,并且不要为您使用的活动指定任何内容。
    • 很抱歉,没有任何改变 :(
    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多