我的问题是我要从活动中删除Fragment
。有两个步骤:
Fragment
将FrameLayout
添加到CheckBox
(R.id.frame_for_des_details)。点击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);
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
中覆盖占位符。
我希望有人了解我并能帮助我。
感谢。
答案 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);
}
}
感谢。