2

I'm using ActionBarSherlock, and my main activity has a ViewPager with three pages, all of them fragments. One of those fragments has an onCreateOptionsMenu method, and the action bar in the main activity is updated correctly.. sort of. Here's the method:

public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, com.actionbarsherlock.view.MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.search, menu);
    // A couple of other things
}

I'm using a custom FragmentPagerAdapter:

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    FragmentManager fm;

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fm = fm;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            accountFragment = Account.newInstance();
            return accountFragment;
        case 1:
            filesFragment = Files.newInstance();
            return filesFragment;
        case 2:
            transfersFragment = Transfers.newInstance();
            return transfersFragment;
        }
        return null;
    }

    private String makeFragmentName(int viewId, int index) {
        return "android:switcher:" + viewId + ":" + index;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return titleAccount;
        case 1:
            return titleFiles;
        case 2:
            return titleTransfers;
        }
        return null;
    }
}

I have no code at all in my activity that calls supportInvalidateOptionsMenu, so I assume the framework is checking the fragments' options menus for me, but I don't like how it does it. Here's what I mean:

the problem

Stock apps (Phone, People, etc) as well as some other apps do this correctly. That is, as soon as I fling to a page, that new page's menu items are displayed. But in mine, they are only displayed at the end of the swiping animation.

Here's something I've tried:

pager.setOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageSelected(int arg0) {
            supportInvalidateOptionsMenu();
        }
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {}
        @Override
        public void onPageScrollStateChanged(int arg0) {}
    });

This seems to do nothing.

Logging shows that onPageSelected is called right when I swipe to another page, which is when I want the items to update. Logging in my fragment's onCreateOptionsMenu shows that it is called only at the end of the animation, which has a considerable delay from when onPageSelected is called.

How do I fix this?

4

1 回答 1

1

股票应用程序(电话、人员等)以及其他一些应用程序可以正确执行此操作。也就是说,只要我跳转到一个页面,就会显示该新页面的菜单项。但在我的,它们只在滑动动画结束时显示。

我无法在运行 4.2.2 的 Galaxy Nexus 上使用 Phone 重现您的行为。在那里,在新页面完全可见之前,操作栏不会改变,无论是滑动还是滑动。

使用通讯录,我看到了您所看到的,因为当手指抬起时操作栏会发生变化,因此一晃,变化会更快。

坦率地说,我认为你是在把一座鼹鼠山变成一座山。话虽如此,我能看到复制联系人行为的唯一方法是完全重新实现FragmentPagerAdapter,从 中删除setMenuVisibility()呼叫setPrimaryItem(),然后安排在您认为更合适的时间拨打这些电话。

于 2013-05-27T19:14:48.343 回答