1

我有一个带有 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);
    }

}

其他片段是相同的,具有单独的菜单和布局。谁能给我一些提示,为什么会这样?

4

1 回答 1

0

解决方法是setMenuVisibility(true);从碎片中删除onCreateView

于 2013-08-21T15:04:49.013 回答