Android的:如何重建行动吧,当片段改变片段、行动、Android

2023-09-12 02:25:18 作者:辉煌时万众人拥你如女王i

我展示一些碎片的活动。 活动视图只包含ViewPager定制FragmentPagerAdapter初始化。 该适配器提供中3个片段导航。

I have an activity showing a few fragments. Activity view contains only ViewPager initialized with custom FragmentPagerAdapter. This adapter provide navigation among 3 fragments.

一切似乎做工精细,除了行动吧。

All seems to work fine except Action bar.

我重写onCreateOptionsMenu()方法,在我的片段创造任何片段单独行动起来吧:

I override onCreateOptionsMenu() method in my fragments to create individual Action bar for any fragment:


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

当我做轻扫,新片段出现,但操作栏保持不变,几秒钟。大概几秒钟后,该方法被调用,操作栏得到改变。

When I do swipe, new fragment appears, but Action bar stay the same for a few seconds. Probably after a few seconds this method are called and Action bar get changed.

这看起来pretty的不好的时候行动起来吧刷卡完成后,在一段时间内改变。 我怎么能马上重新行动起来吧,前刷一下么?

This looks pretty bad when action bar are changed in a while after swipe is finished. How can I recreate action bar immediately, before swipe begin?

推荐答案

您可以要求机器人来重新创建动作条才通过调用自动执行 invalidateOptionsMenu();

You can ask android to re-create the actionbar before it automatically does by calling invalidateOptionsMenu();

做到这一点的地方接近的地方,你改变片段,试图降低片段之间以及动作条改变'滞后'的地步。

Do this somewhere close to the point where you change fragments to try and decrease the 'lag' between the fragment and actionbar changing.

修改

一个完整的解决方案可能是这样的:

a complete solution may look like this:

class activity extends Activity{

private void switchFragment(){

...do fragment changing stuff

activity.invalidateOptionsMenu();

}

}

class someFragment extends Fragment{

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


    //fragment specific menu creation
}

}

取片段是在开放的

whichever fragment is open during the

activity.invalidateOptionsMenu();

将调用其

 onCreateOptionsMenu

,你可以做片段特定的菜单创作在里面

and you can do fragment specific menu creation in there