Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android片段交换_Android_Android Fragments - Fatal编程技术网

android片段交换

android片段交换,android,android-fragments,Android,Android Fragments,现在,我正在使用片段做一个应用程序。 所以我关心一个片段的活动,有四个片段,片段a,片段B,片段C,片段D。 默认情况下,将选择片段A,在选项卡上单击,我可以切换到片段B、C、D或A 我的问题是我想检查片段a中的一个条件,这取决于切换到片段B的结果 Intent in=new Intent(src,destination); startactivity(in) 我想在不点击tab按钮的情况下切换到其他片段。下面是我用来添加片段的代码。我想检查一个条件是否取决于该条件片段a,片段b选项卡将被选中

现在,我正在使用片段做一个应用程序。 所以我关心一个片段的活动,有四个片段,片段a,片段B,片段C,片段D。 默认情况下,将选择片段A,在选项卡上单击,我可以切换到片段B、C、D或A

我的问题是我想检查片段a中的一个条件,这取决于切换到片段B的结果

Intent in=new Intent(src,destination);
startactivity(in)
我想在不点击tab按钮的情况下切换到其他片段。下面是我用来添加片段的代码。我想检查一个条件是否取决于该条件片段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);

朋友们,请帮助我。

片段很难使用,所以你应该了解它们。 我是这样做的

碎片管理器:

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);
}

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

ActionBar bar = getSupportActionBar();
if(condition){
    bar.setSelectedNavigationItem(1); //where 1 is the position of tab1
}else{
    bar.setSelectedNavigationItem(3); //for tab3
}

您是否使用
FragmentPagerAdapter
将片段添加到活动中?@vikki…我使用了以下代码。。ActionBar=getSupportActionBar();设置导航模式(操作栏导航模式选项卡);ActionBar.Tab tab1=bar.newTab();ActionBar.Tab tab2=bar.newTab();表1.setText(“片段A”);表2.setText(“片段B”);tab1.setTablListener(新的MyTablListener());tab2.setTablListener(新的MyTablListener());条.添加选项卡(表1);bar.addTab(表2);
ActionBar bar = getSupportActionBar();
if(condition){
    bar.setSelectedNavigationItem(1); //where 1 is the position of tab1
}else{
    bar.setSelectedNavigationItem(3); //for tab3
}