【问题标题】:Removing a fragment after adding does not work添加后删除片段不起作用
【发布时间】:2014-10-16 11:56:51
【问题描述】:

我的问题是我想从 Activity 中删除 Fragment。有两个步骤:

  1. 通过检查CheckBoxFragment 添加到FrameLayout (R.id.frame_for_des_details)。
  2. 通过单击 TextView 删除 Fragment

    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 中覆盖占位符。

我希望有人理解我并可以帮助我。

谢谢。

【问题讨论】:

  • 你检查过你的 textview 的 onClick 是否真的被触发了吗?可以肯定的是,您可以在该方法中打印日志以查看它是否到达那里。顺便说一句,使用 TextView 的 onClicklistener 来删除片段并不是一个好主意。
  • @elvisrusu 如果有人使用支持库中的Fragment,则需要使用支持片段管理器。 android.app.Fragmentandroid.support.v*.Fragment 是两个不同的野兽。你不能把这两件事混为一谈。
  • 尝试将android:clickable="true"添加到TextView

标签: android android-fragments checkbox fragment


【解决方案1】:

已修复

我将 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);
            }

        }

谢谢。

【讨论】:

    【解决方案2】:

    试试 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();
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多