如何删除Button Click上的片段?

时间:2017-12-08 06:10:23

标签: android fragment

我的ListView中有一个可展开的MainActivity。当选择了孩子ListView上的某个项目时,某些按钮(安装,详细信息,取消)会显示在fragment中。

lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


                FragmentManager fragmentManager = getSupportFragmentManager();
                final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                if(!status){
                    final Options op = new Options();
                    fragmentTransaction.replace(R.id.main,op);
                    fragmentTransaction.commit();

                }

此处选项是我的fragment's名称。按下选项类中的取消按钮我想删除此片段并仅显示MainActivity

Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
      btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //REMOVE THIS FRAGMENT

        }
    });

请有人帮我试试吗?

1 个答案:

答案 0 :(得分:1)

删除所有Fragments

public void clearAllFragments() {

    try {
        FragmentManager aFragmentManager = Context
            .getSupportFragmentManager();

        aFragmentManager.popBackStack(null,
                FragmentManager.POP_BACK_STACK_INCLUSIVE);
    } catch (Exception e) {

        e.printStackTrace();
    }
}

删除单个Fragment

public void removeFragment(int aCount) {

    FragmentManager aFragmentManager = Context
            .getSupportFragmentManager();

    for (int i = 0; i < aCount; i++) {

        aFragmentManager.popBackStack();
    }
}