添加后删除片段不起作用

时间:2014-08-22 15:09:11

标签: android android-fragments checkbox fragment

我的问题是我要从活动中删除Fragment。有两个步骤:

  1. 通过查看FragmentFrameLayout添加到CheckBox(R.id.frame_for_des_details)。
  2. 点击Fragment

    删除TextView
    public class CreateTripActivity extends FragmentActivity {
    
    //Vars for Destination Fragment
    private CheckBox checkBox;
    private DestinationDetailsFragment destinationDetailsFragment;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    //Setting the ContentView
    setContentView(R.layout.activity_create_trip);
    
    //Set the Destination Checkbox listener
    checkBox = (CheckBox) this.findViewById(R.id.checkBox_destination_now);
    
  3. CheckBox的听众:

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    
                //Adding the Fragment. THIS WORK
                destinationDetailsFragment = new DestinationDetailsFragment();
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().add(R.id.frame_for_des_details, destinationDetailsFragment).commit();
    
            }
        });
    

    TextView的听众:

        findViewById(R.id.label_later).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                //Removing the Fragment. DOES NOT WORK
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().remove(destinationDetailsFragment).commit();              
            }
        });
    
        }
    

    当我查看CheckBox时,我可以看到片段。但是当我点击TextView将其删除时,它仍然存在。 当我再次检查CheckBox时,似乎新Fragment覆盖旧片段,因此我可以在EditText中覆盖占位符。

    我希望有人了解我并能帮助我。

    感谢。

2 个答案:

答案 0 :(得分:0)

尝试android.support.v4.app.FragmentManager。这是代码。

        destinationDetailsFragment = new DestinationDetailsFragment();
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton compoundButton,
                boolean b) {

            // Adding the Fragment. THIS WORK

            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction()
                    .add(R.id.frame_for_des_details,
                            destinationDetailsFragment).commit();

        }
    });
    findViewById(R.id.label_later).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    FragmentManager fm = getSupportFragmentManager();
                    fm.beginTransaction()
                            .remove(destinationDetailsFragment).commit();
                }
            });

答案 1 :(得分:0)

固定

我将Fragment实现为android.support.v4.app.Fragment,但这不是问题。

在我添加片段之前,我删除了复选框。并且要删除片段我再次将复选框添加到视图中。但我也称为checkbox.setChecked(false)。那再次解雇了onCheckedChanged!

我的新代码

@Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if (isChecked) {
                LinearLayout layout = (LinearLayout) findViewById(R.id.layout_destination);
                //Remove and Add Content
                layout.removeView(checkBox);

                destinationDetailsFragment = new DestinationDetailsFragment();
                FragmentManager fm = getSupportFragmentManager();
                fm.beginTransaction().add(R.id.frame_for_des_details, destinationDetailsFragment).commit();

                //Enable Later Button
                findViewById(R.id.img_later).setVisibility(View.VISIBLE);
                findViewById(R.id.label_later).setVisibility(View.VISIBLE);
            }

        }

感谢。

相关问题