SherlockFragments膨胀错误的菜单

时间:2013-08-20 14:49:22

标签: android actionbarsherlock android-viewpager android-optionsmenu

我有一个带有3个片段的SherlockFragmentActivity。一切正常,但是当我从片段滑动到片段时,会显示错误的菜单。 Fragment1显示来自fragment2的菜单,Fragment1上的SearchView正在过滤Fragment2上的数据。这是SherlockFragmentActivity的代码

public class MainHolder extends SherlockFragmentActivity {

    private static final String[] CONTENT = new String[] { "FRAGMENT1", "FRAGMENT2", "FRAGMENT3" };

    FragmentAdapter mAdapter;
    ViewPager mPager;
    TabPageIndicator mIndicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_holder);


        mAdapter = new FragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);

    }

    class FragmentAdapter  extends FragmentPagerAdapter {
            public FragmentAdapter(FragmentManager fm) {
                super(fm);
            }

            public Fragment getItem(int position) {
                 switch(position)
                       {
                                case 0:
                                    return Fragment1.newInstance(CONTENT[position % CONTENT.length]);

                                case 1:
                                    return Fragment2.newInstance(CONTENT[position % CONTENT.length]);

                                case 2:
                                    return Fragment3.newInstance(CONTENT[position % CONTENT.length]);

                                default:
                                    return null;

                         }

            }

            @Override
            public CharSequence getPageTitle(int position) {
                return CONTENT[position % CONTENT.length].toUpperCase();
            }

            @Override
            public int getCount() {
                return CONTENT.length;
            }
        }



    @Override
    public void onBackPressed() {
        Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":"+mPager.getCurrentItem());
        if (fragment != null) // could be null if not instantiated yet
        {
            if (fragment.getView() != null) {
                // Pop the backstack on the ChildManager if there is any. If not, close this activity as normal.
                if (!fragment.getChildFragmentManager().popBackStackImmediate()) {
                    finish();
                }
            }
        }
    }

}

这是片段

public class Fragment1 extends SherlockFragment {

    private static final String KEY_CONTENT = "Fragment1:Content";
    LazyAdapter fragment1adapter;

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

    MenuItem searchItem = menu.findItem(R.id.menu_search);
    final SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setQueryHint("Pretraga ...");
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextChange(String newText) {
            if (newText.length() != 0) {
                fragment1adapter.getFilter().filter(newText);
                return true;
            }
            return false;
        }



        @Override
        public boolean onQueryTextSubmit(String query) {

            return false;
        }


    });

    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            fragment1adapter.getFilter().filter("");
            return false;
        }
    });
    }


    public static Fragment1 newInstance(String content) {
        Fragment1 fragment = new Fragment1();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 20; i++) {
            builder.append(content).append(" ");
        }
        builder.deleteCharAt(builder.length() - 1);
        fragment.mContent = builder.toString();
        return fragment;
    }

    private String mContent = "???";

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

        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment1, container, false);
        setRetainInstance(true);
        setHasOptionsMenu(true);
        setMenuVisibility(true);

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT, mContent);
    }

}

其他片段是相同的,具有单独的菜单和布局。任何人都可以给我一些提示,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

解决方案是从片段setMenuVisibility(true);中移除onCreateView