Android 添加后删除片段不起作用
我的问题是我想从活动中删除Android 添加后删除片段不起作用,android,android-fragments,checkbox,fragment,Android,Android Fragments,Checkbox,Fragment,我的问题是我想从活动中删除片段。有两个步骤: 通过选中复选框,将片段添加到框架布局(R.id.frame\u获取详细信息) 通过单击TextView public class CreateTripActivity extends FragmentActivity { //Vars for Destination Fragment private CheckBox checkBox; private DestinationDetailsFragment destinationDetailsFra
片段。有两个步骤:
通过选中复选框
,将片段添加到框架布局
(R.id.frame\u获取详细信息)
通过单击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.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();
}
});
文本视图的侦听器
:
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();
}
});
}
当我选中复选框时,我可以看到片段。但是当我点击TextView
删除它时,它仍然存在。
当我一次又一次地选中复选框时,新的片段似乎覆盖了旧片段,因此我可以在编辑文本中写入占位符
我希望有人能理解我,能帮助我
谢谢 试试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();
}
});
固定的
我将片段实现为android.support.v4.app.Fragment,但这不是问题所在
在添加片段之前,我删除了复选框。bevor移除片段,我再次将复选框添加到视图中。但是我也调用了checkbox.setChecked(false)。那一次又一次的改变了
我的新代码
@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);
}
}
谢谢。你检查过你的文本视图的onClick是否被激活了吗?可以肯定的是,您可以在该方法中打印一个日志,以查看它是否到达了那里。顺便说一句,使用TextView的onClicklistener删除片段不是一个好主意。@如果有人使用支持库中的fragment
,则需要使用elvisrusu支持片段管理器android.app.Fragment
和android.support.v*.Fragment
是两种不同的野兽。您不能将这两件事混为一谈。请尝试将android:clickable=“true”
添加到文本视图中