3

现在我正在使用片段做一个应用程序。所以我关心一个带有四个片段片段 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);

请帮助我的朋友。

4

2 回答 2

2

片段相当难以使​​用,因此您应该了解它们。这是我的做法。

片段管理器:

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);
于 2012-08-07T07:27:50.187 回答
2

如果通过选择一个选项卡您可以切换到某个片段,例如tab1调出Fragment A,那么您需要做的就是选择该选项卡。例如,您当前正在查看Fragment B并需要检查一些条件,这将决定您是否切换到Fragment AC

ActionBar bar = getSupportActionBar();
if(condition){
    bar.setSelectedNavigationItem(1); //where 1 is the position of tab1
}else{
    bar.setSelectedNavigationItem(3); //for tab3
}
于 2012-08-07T08:09:46.297 回答