所以我试图找出这是否可行。我需要从选中的fragments
个activity
中将两个tab
加载到两个容器中。
我发现了Android - Multiple fragments in ONE tab
但没有人回答过这个问题。
所以这里是代码示例和我尝试过的内容。
听者
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);
}
现在我尝试将第二个listener
添加到inflate
第二个fragment
这样
surveyTabListener2 = new TabListener<Store_Fragment>(this,
R.id.store_fragment_container, Store_Fragment.class);
我也尝试在if else
中使用第二个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);
if (fragment2 == null) {
String fragmentName2 = fragmentClass2.getName();
fragment2 = Fragment.instantiate(activity, fragmentName2);
ft.add(fragmentContainer2, fragment2, fragmentName2);
} else
ft.attach(fragment2);
}
既不工作,我要么得NPE (null pointer)
,要么一无所获,但我没有错。
这似乎应该经常进行,但我似乎无法弄明白。
答案 0 :(得分:2)
你需要使用一个空片段作为你的两个片段的容器,我会发布一些代码来告诉你我是如何把它分开的。
那是片段容器:
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;
}
答案 1 :(得分:1)
我突然意识到我是以错误的方式解决这个问题。我想出了一种方法来完成我正在寻找的东西,这很简单。如果其他人有这个问题,这就是解决方案。
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();
}
......那就是它。我希望这有助于其他人。