现在我正在使用片段进行应用程序。 所以我关心片段活动,有四个片段片段A,片段B,片段C,片段D. 默认情况下,将选择片段A,然后在选项卡上单击,我可以切换到片段B,C,D或A.
我的问题是我想检查片段A中的条件并依赖于结果切换到片段B.Like。
Intent in=new Intent(src,destination);
startactivity(in)
我想切换到其他片段而不点击标签按钮。以下是我用来添加片段的代码。我想检查条件取决于条件片段a,片段b选项卡将被选中
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = bar.newTab();
ActionBar.Tab tab2 = bar.newTab();
tab1.setText("Fragment A");
tab2.setText("Fragment B");
tab1.setTabListener(new MyTabListener());
tab2.setTabListener(new MyTabListener());
bar.addTab(tab1);
bar.addTab(tab2);
请帮帮我朋友。
答案 0 :(得分:2)
片段很难使用,所以你应该了解它们。 我就是这样做的。
FragmentManager:
public class FragmentsUtils {
private static String DIALOG_TAG = "dialog_tag";
/*public static MyAlertDialogFragment showDialog(FragmentManager fragmentManager, int id) {
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = fragmentManager.beginTransaction();
Fragment prev = fragmentManager.findFragmentByTag(DIALOG_TAG);
if (prev != null) {
ft.remove(prev);
}
// ft.addToBackStack(null);
// Create and show the dialog.
MyAlertDialogFragment newFragment = MyAlertDialogFragment.newInstance(id);
newFragment.show(ft, DIALOG_TAG);
return newFragment;
}*/
public static void swapFragments(FragmentManager fragmentManager, int containerViewId, Fragment newFragment, String fragmentTag) {
if (fragmentManager == null)
return;
View newFragmentMainView = newFragment.getView();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// fragmentTransaction.replace(containerViewId, fragment);
if(fragmentTag == null || fragmentTag.length() < 1)
fragmentTransaction.add(containerViewId, newFragment);
else
fragmentTransaction.add(containerViewId, newFragment, fragmentTag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
public static void swapFragments(FragmentManager fragmentManager, int containerViewId, Fragment newFragment) {
swapFragments(fragmentManager, containerViewId, newFragment, null);
}
}
以下是如何使用它:
WebViewDetailsActivity webViewDetailsActivity = new WebViewDetailsActivity();
Bundle args = new Bundle();
args.putString("url", tempRestModel.ClickTableUrl); webViewDetailsActivity.setArguments(args);
FragmentsUtils.swapFragments(getBaseFragmentManager(), R.id.realtabcontent, webViewDetailsActivity, Consts.TAB_REC);
答案 1 :(得分:2)
如果通过选择标签,您可以切换到某个片段,例如tab1
会显示Fragment A
,那么您只需选择该标签即可。例如,您当前正在查看Fragment B
并需要检查一些条件,这些条件将决定您是否切换到Fragment A
或C
ActionBar bar = getSupportActionBar();
if(condition){
bar.setSelectedNavigationItem(1); //where 1 is the position of tab1
}else{
bar.setSelectedNavigationItem(3); //for tab3
}