Reputation: 339
So I'm trying to find out if this is possible. I need to load two fragments
into two containers in the same activity
from a single tab
being selected.
I found this Android - Multiple fragments in ONE tab
but no one ever answered this question.
So here is a sample of the code and what I've tried.
listener
surveyTabListener = new TabListener<Load_Fragment>(this,
R.id.header_fragment_container, Load_Fragment.class);
onTabSelected
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (fragment == null) {
String fragmentName = fragmentClass.getName();
fragment = Fragment.instantiate(activity, fragmentName);
ft.add(fragmentContainer, fragment, fragmentName);
} else
ft.attach(fragment);
}
Now I've tried to add a second listener
to inflate
the 2nd fragment
like this
surveyTabListener2 = new TabListener<Store_Fragment>(this,
R.id.store_fragment_container, Store_Fragment.class);
and I've also tried to use a second if else
statement in onTabSelected
like this
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (fragment == null) {
String fragmentName = fragmentClass.getName();
fragment = Fragment.instantiate(activity, fragmentName);
ft.add(fragmentContainer, fragment, fragmentName);
} else
ft.attach(fragment);
if (fragment2 == null) {
String fragmentName2 = fragmentClass2.getName();
fragment2 = Fragment.instantiate(activity, fragmentName2);
ft.add(fragmentContainer2, fragment2, fragmentName2);
} else
ft.attach(fragment2);
}
Neither work, I either get a NPE (null pointer)
or absolutely nothing happens, but I get no errors.
This seems like something that should be done fairly often but I can't seem to figure it out.
Upvotes: 1
Views: 1449
Reputation: 339
It dawned on me that I was going about this the wrong way. I figured out a way to do exactly what I was looking for and it was fairly simple. If anyone else has this problem here is the solution.
public void onTabSelected(Tab tab, FragmentTransaction ft) {
//get the name of the tab currently selected
String name = (String) tab.getText().toString();
// compare it to the tab you would like to do something with
if (name == "Assets") {
//load the 1st fragment
fragment = new Store_Fragment();
getFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out)
.replace(R.id.store_fragment_container, fragment).commit();
//load the 2nd fragment
fragment2 = new Load_Fragment();
getFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out)
.replace(R.id.header_fragment_container, fragment2).commit();
}
...and that's it. I hope this helps others.
Upvotes: 1
Reputation: 649
You need to use an empty fragment as a container for your two fragments, I'm gonna post some code to show you how I dit it.
That's the fragment container:
public static class FragmentInsideContainer extends Fragment {
ListView mainListView;
ArrayAdapter<Producer> listAdapter;
TextView inputSearch;
static FragmentInsideContainer containerReference;
String idProd;
public FragmentInsideContainer() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layoutcontaineprod, container, false);
FragmentInsideContainer.containerReference = this;
FragmentDetailProd fragmentDetailProd = new FragmentDetailProd();
FragmentDetailProd.id = this.idProd;
DummySectionFragment2.currentDetailProdFragment = fragmentDetailProd;
FragmentListWine fragmentListWine = new FragmentListWine();
fragmentListWine.id = this.idProd;
DummySectionFragment2.currentListWineFragment = fragmentListWine;
FragmentInsideContainer.containerReference.getChildFragmentManager().beginTransaction().replace(R.id.containerDetail, fragmentDetailProd).commit();
FragmentInsideContainer.containerReference.getChildFragmentManager().executePendingTransactions();
FragmentInsideContainer.containerReference.getChildFragmentManager().beginTransaction().add(R.id.containerDetail, fragmentListWine).commit();
FragmentInsideContainer.containerReference.getChildFragmentManager().executePendingTransactions();
return rootView;
}
Upvotes: 2