检查Android中的布局中是否显示任何片段?

时间:2014-03-06 08:03:11

标签: android android-fragments

在我的应用程序中,有五个按钮。当按下curresponding按钮时,片段将加载到线性布局中。我想要的是,如果按下任何按钮,布局中的当前片段应该去除,并且应该放置对应于按钮的片段。我需要一种常用方法来移除先前放置的片段。我知道我们可以使用它的id.i来删除片段。不需要它。我想要一个通用的方法来删除片段。这是我的java类。

   Button b1, b2, b3, b4, b5;
        // Declare Variables
        ActionBar mActionBar;
        ViewPager mPager;
        FragmentManager fm;

setContentView(R.layout.activity_main);
        fm = getSupportFragmentManager();b1 = (Button) findViewById(R.id.fbbutton1);
        b2 = (Button) findViewById(R.id.fbbutton2);
        b3 = (Button) findViewById(R.id.fbbutton3);
        b4 = (Button) findViewById(R.id.fbbutton4);
        b5 = (Button) findViewById(R.id.fbbutton5);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentTab1 f1 = new FragmentTab1();
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(R.id.fragmentgroup, f1, "A");
                ft.commit();

            }
        });
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (getSupportFragmentManager().findFragmentById(R.id.detail_screen) != null) {

                    getSupportFragmentManager().popBackStack();

                        } //to do add fragment
                FragmentTab2 f2 = new FragmentTab2();
                FragmentTransaction ft = fm.beginTransaction();
//              fm.popBackStack(fm.getBackStackEntryCount(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
//              - Use FragmentManager.getBackStackEntryCount()/getBackStackEntryAt().getId() to retrieve the ID of the first entry on the back stack, and FragmentManager.popBackStack(int id, FragmentManager.POP_BACK_STACK_INCLUSIVE).

//                ft.replace(R.id.fragmentgroup, f2);
                ft.add(R.id.fragmentgroup, f2, "B");
                ft.commit();

            }
        });
        b3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentTab3 f3 = new FragmentTab3();
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(R.id.fragmentgroup, f3, "C");
                ft.commit();

            }
        });
        b4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentTab4 f4 = new FragmentTab4();
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(R.id.fragmentgroup, f4, "D");
                ft.commit();

            }
        });
        b5.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentTab5 f5 = new FragmentTab5();
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(R.id.fragmentgroup, f5, "E");
                ft.commit();

            }
        });

在我的xml中显示片段的布局

 <LinearLayout
        android:id="@+id/fragmentgroup"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#F2A4A9"
        android:orientation="horizontal" >

1 个答案:

答案 0 :(得分:1)

您需要一种常用方法来替换与按钮点击相对应的片段。 因此,您可以使用replace中的FragmentTransaction方法以常用方法完成工作。

代码将是,

public void replaceAFragment(int frag_container, Fragment aFragment, String tag) {
    FragmentTransaction transaction =    getSupportFragmentManager().beginTransaction();
    transaction.replace(frag_container, aFragment, tag);
    transaction.commit();
}

每当您想要加载片段时,您无需删除,只需将其替换为新片段,

b3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentTab3 f3 = new FragmentTab3();
            replaceAFragment(R.id.fragmentgroup,f3,"FRAGMENT THREE");

        }
    });
    b4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentTab4 f4 = new FragmentTab4();
            replaceAFragment(R.id.fragmentgroup,f4,"FRAGMENT FOUR");
        }
    });
    b5.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentTab5 f5 = new FragmentTab5();
            replaceAFragment(R.id.fragmentgroup,f5,"FRAGMENT FIVE");
        }
    });

现在你有一个放置片段的常用方法。您也可以使用标记detachremove