问题
我想在一个Activity中创建一个带有2个片段的viewpager。
这两个片段具有不同的菜单选项,其中一个片段是嵌套的 - 单击listview项目会打开具有新数据的相同片段(replace(),addToBackStack()
),但是viewpager未正确替换(执行transaction.replace(R.id.viewpager, new TwoFragment())
时)。
我使用了这个答案 - https://dev59.com/Y2cs5IYBdhLWcg3w5IBa#18020219,但是我有不同的菜单选项,并且菜单选项从 Fragment Container 中膨胀。
结果,如果我创建一个新的Fragment(作为容器),我只需将布尔extra isContainer
传递到片段中即可解决此问题。
但现在活动调用错误的onOptionsItemSelected方法(从fragment2
调用,当在viewpager中选择 fragment1
时)。
我的代码
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.addOnPageChangeListener(this);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
viewPager.setOffscreenPageLimit(2);
mAdapter = new MainViewPagerAdapter(getSupportFragmentManager());
TwoFragment fragment = new TwoFragment();
Bundle args = new Bundle(1);
args.putBoolean(TwoFragment.ISCONTAINER_EXTRA, true);
fragment.setArguments(args);
mAdapter.addFragment(fragment, "TAB1");
mAdapter.addFragment(new OneFragment(), "TAB2");
}
OneFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
setHasOptionsMenu(true);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_one, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// This method not called then this fragment selected in viewpager
return true;
}
TwoFragment:
@Override
public void onCreate(Bundle savedInstanceState) {
Bundle extras = getArguments();
if (extras != null && extras.containsKey(ISCONTAINER_EXTRA)) {
isContainer = extras.getBoolean(ISCONTAINER_EXTRA);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (isContainer) {
setHasOptionsMenu(false);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.clRoot, new TwoFragment()).commit();
return inflater.inflate(R.layout.fragment_two, container, false);
}
View view = inflater.inflate(R.layout.fragment_two, container, false);
setHasOptionsMenu(true);
// Other actions
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_two, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// This method called then always
switch (item.getItemId()) {
case android.R.id.home:
getFragmentManager().popBackStack();
break;
case R.id.action_settings:
startActivity(new Intent(getActivity(), SettingsActivity.class));
break;
case R.id.action_exit:
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
break;
}
return true;
}
问题
我如何在不使用第三方库的情况下完成这个任务?
注:对于我的糟糕英语表示抱歉。