首页 > 解决方案 > Android Studio:无法删除片段

问题描述

我正在尝试删除一个片段,但它不起作用。

我一直在检查 logcat 并注意到该按钮已被单击并且该消息正在传递给主要活动。我认为我的代码有问题,但我不知道那是什么。将不胜感激一些帮助。

主菜单.java

package com.example.android.learninstrumentation;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class MainMenu extends AppCompatActivity implements MainMenuStFrag.MainMenuStFragListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_menu);

        //Create a new Fragment to be placed in the Activity layout
        Fragment menuSiFragment = new MainMenuSiFrag();
        Fragment menuStFragment = new MainMenuStFrag();
        //Add the fragment to the 'fragment_container' layout
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_type_si_container, menuSiFragment).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_type_st_container, menuStFragment).commit();
    }

    //The user clicked 'expand' from MainMenuStFrag. Remove MainMenuSiFrag.
    public void onExpandClick () {
        Log.i("MainMenu", "Click message received");
        Fragment oldFragment = new MainMenuSiFrag();
        getSupportFragmentManager().beginTransaction().remove(oldFragment).addToBackStack(null).commit();
    }

    public void onCheckboxClick() {

    }

}

MainMenuStFrag.java

package com.example.android.learninstrumentation;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;

public class MainMenuStFrag extends Fragment implements View.OnClickListener{

    MainMenuStFragListener mCallback;

    //Inflate the layout and set buttons for this fragment
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //Store the Fragment in the mainMenu variable
        View mainMenu = inflater.inflate(R.layout.main_menu_st_fragment, container, false);

        //Define the buttons and set onClickListeners
        ImageView expand = mainMenu.findViewById(R.id.mainMenu_imageView_SelectTopics);
        expand.setOnClickListener(this);
        CheckBox checkBox = mainMenu.findViewById(R.id.mainMenu_CheckBox_SelectAllTopics);
        checkBox.setOnClickListener(this);

        return mainMenu;
        }

    //Interface for communication with container Activity
    public interface MainMenuStFragListener {
        void onExpandClick();
        void onCheckboxClick();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //This makes sure the container Activity has implemented the callback interface.
        // If not, it throws an exception.
        try {
            mCallback = (MainMenuStFragListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement OnHeadlineSelectedListener");
        }

    }

        //onClick actions
        @Override public void onClick(View v) {
            switch (v.getId()) {
                //Remove the other 'type' item
                case R.id.mainMenu_imageView_SelectTopics:
                    Log.i("MainMenuStFrag", "Topics>Expand=Clicked");
                    mCallback.onExpandClick();
                    break;
                case R.id.mainMenu_CheckBox_SelectAllTopics:
                    mCallback.onCheckboxClick();
                    break;
            }
        }
}

MainMenuSiFrag.java

package com.example.android.learninstrumentation;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainMenuSiFrag extends Fragment {

    //Inflate the layout for this fragment
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View mainMenu = inflater.inflate(R.layout.main_menu_si_fragment, container, false);
        return mainMenu;
    }
}

标签: javaandroidandroid-studioandroid-fragments

解决方案


您已经为删除 Fragment 创建了新的 Fragment 实例

 public void onExpandClick () {
        Log.i("MainMenu", "Click message received");
        Fragment oldFragment = new MainMenuSiFrag();
        getSupportFragmentManager().beginTransaction().remove(oldFragment).addToBackStack(null).commit();
    }

而是在那找到一个片段并将其从容器中删除

Fragment oldFragment=getSupportFragmentManager().findFragmentById(R.id.fragment_type_si_container);
             if(oldFragment!=null && oldFragment instanceof MainMenuSiFrag)
             {
              getSupportFragmentManager()
                .beginTransaction().
                remove(oldFragment).commit();

             }

推荐阅读