Answer the question
In order to leave comments, you need to log in
How to implement nested fragments in viewpager with different menus?
I want to create an Activity with 2 fragments in the ViewPager (there is also a TabLayout switch), These fragments have different menu options, and they are nested (that is, when a ListView Item is clicked, the second same fragment opens but with different data). Logically, this can be implemented if you change the content inside the ViewPagera (by executing
transaction.replace(R.id.viewpager, new Fragment(), transaction.addToBackStack()).
But unfortunately, this does not work (an empty fragment appears instead of a fragment with data)
I found one solution - stackoverflow.com/a/18020219/5576117, but since I use different menu options, it doesn't work for me either (onCreateOptionsMenu will be called on FragmentContainer-a) .
As a result, I made a simple boolean extra, which I pass in the case when the fragment is a container and it works, but now there is a new problem - the onOptionsItemSelected method is called from the second fragment, even when the first one is selected in the ViewPager.
Code:
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");
}
@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) {
// Метод не вызывается даже когда этот фрагмент выбран во ViewPager-е
return true;
}
@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) {
// А вот этот вызывается всегда
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;
}
Answer the question
In order to leave comments, you need to log in
I found the answer here (I saw this advice before, but unfortunately I did not try it): https://tausiq.wordpress.com/2014/06/06/android-mu... (The article uses the ViewPagerIndicator library, but it may be easily replaced by TabLayout).
Main idea: move tablayout and viewpager from activity to fragment. I checked it myself - everything works fine.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question