Replace one fragment with an another fragment

前端 未结 8 1546
再見小時候
再見小時候 2020-12-16 19:37

I want to replace an old Fragment with a new Fragment, but i still get the buttons of the old Fragment that is still visible in the ne

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 20:16

    Try the following code.

    A) Create Activity as follows :

    MainActivity

    import android.app.FragmentTransaction;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity implements ShowNextFragment{
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentA fragmentA=new FragmentA();
        FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container,fragmentA);
        fragmentTransaction.addToBackStack("A");
        fragmentTransaction.commitAllowingStateLoss();
    
    }
    
    @Override
    public void showFragment() {
        FragmentB fragmentB=new FragmentB();
        FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container,fragmentB);
        fragmentTransaction.addToBackStack("B");
        fragmentTransaction.commitAllowingStateLoss();
    }
    }
    

    B) Create 2 fragments as follows :

    Fragment A

     import android.app.Fragment;
     import android.os.Bundle;
     import android.support.annotation.Nullable;
     import android.util.Log;
     import android.view.LayoutInflater;
     import android.view.View;
     import android.view.ViewGroup;
    
    
    public class FragmentA extends Fragment {
    private ShowNextFragment showNextFragment;
    
    
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        try {
    
            showNextFragment=(ShowNextFragment)getActivity();
            Log.e("CAllback","Set");
        }catch (ClassCastException e){
            Log.e("Error","Please Implement ShowFragment Interface");
        }
        return inflater.inflate(R.layout.fragment_a,container,false);
    }
    
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (showNextFragment!=null){
                    showNextFragment.showFragment();
                }
            }
        });
    
    }
    }
    

    Fragment B

    import android.app.Fragment;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.LayoutInflater; 
    import android.view.View;
    import android.view.ViewGroup;
    
    public class FragmentB extends Fragment {
    
    
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_b,container,false);
    }
    
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
    
    }
    }
    

    C) Create an interface as follows

    public interface ShowNextFragment {
    void showFragment();
    }
    

    D) create following xmls as :

    i) activity_main

       
       
    
      
    

    ii) fragment_a

    
    
    
    

    iii) fragment_b

    
    
    
    
    
    
     
    

提交回复
热议问题