1

我已经使用带有视图寻呼机的导航选项卡实现了夏洛克片段活动。该活动有 3 个选项卡

public static class TabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener
{
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo
    {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args)
        {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager)
    {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args)
    {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }



    @Override
    public int getCount()
    {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position)
    {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext,info.clss.getName(),
                info.args)

        ;

    }

    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels)
    {
    }

    public void onPageSelected(int position)
    {
        mActionBar.setSelectedNavigationItem(position);
    }

    public void onPageScrollStateChanged(int state)
    {
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft)
    {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++)
        {
            if (mTabs.get(i) == tag)
            {
                mViewPager.setCurrentItem(i);
            }
        }
    }
    public void onTabUnselected(Tab tab, FragmentTransaction ft)
    {
    }
    public void onTabReselected(Tab tab, FragmentTransaction ft)
    {
    }
}

我使用以下方法将一个片段替换为另一个片段

  public void replace_fragment(SherlockFragment frag,String tag)
    {
            setContentView(R.layout.main);
            android.support.v4.app.FragmentManager fragment = getSupportFragmentManager();
            FragmentTransaction ft = fragment.beginTransaction();
            ft.addToBackStack(null)
            .replace(R.id.fragment_container,frag,tag);

            ft.commit();

        }

我的问题是按下后退键时操作栏状态未保留。

4

0 回答 0