Change fragment when clicking on a button inside another fragment

4

I have a button inside my fragment that I want it to call another fragment that is over the current when it is clicked. follows the fragment code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.layout_fragment_minhaconta, null);
    Button botao = (Button) view.findViewById(R.id.editarMinhasInformacoes);

    botao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Aqui, deve-se mudar para o outro fragment
        }
    });

    return (view);
}

In the class that calls this fragment, it has been declared so that a navigationDrawer changes the fragments within the same activity:

ActionBarDrawerToggle toggle;

FragmentManager fm = getSupportFragmentManager();

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

    // Criando o menu
    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
    navigationView.setNavigationItemSelectedListener(this);

    // Fragments
    if (savedInstanceState == null) {
        FragmentInicio fragInicio = new FragmentInicio();

        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.layoutFundo, fragInicio, "fragInicio");
        ft.commit();
    }
}

public boolean onNavigationItemSelected(MenuItem item) {
    FragmentMinhaConta fragMinhaConta = new FragmentMinhaConta();
    FragmentInicio fragInicio = new FragmentInicio();
    FragmentFavoritos fragFavoritos = new FragmentFavoritos();
    FragmentCompras fragCompras = new FragmentCompras();
    FragmentConfiguracoes fragConfiguracoes = new FragmentConfiguracoes();
    FragmentSobre fragSobre = new FragmentSobre();

    FragmentTransaction ft = fm.beginTransaction();

    switch (item.getItemId()) {

        case R.id.nav_menu1:
            ft.replace(R.id.layoutFundo, fragMinhaConta, "fragMinhaConta");
            ft.addToBackStack("pilha");
            break;
        case R.id.nav_menu2:
            ft.replace(R.id.layoutFundo, fragInicio, "fragInicio");
            ft.addToBackStack("pilha");
            break;
        case R.id.nav_menu3:
            ft.replace(R.id.layoutFundo, fragFavoritos, "fragFavoritos");
            ft.addToBackStack("pilha");
            break;
        case R.id.nav_menu4:
            ft.replace(R.id.layoutFundo, fragCompras, "fragCompras");
            ft.addToBackStack("pilha");
            break;
        case R.id.nav_menu5:
            ft.replace(R.id.layoutFundo, fragConfiguracoes, "fragConfiguracoes");
            ft.addToBackStack("pilha");
            break;
        case R.id.nav_menu6:
            ft.replace(R.id.layoutFundo, fragSobre, "fragSobre");
            ft.addToBackStack("pilha");
            break;
        case R.id.nav_menu7:
            finish();
            break;
    }
    ft.commit();

    DrawerLayout dl = (DrawerLayout) findViewById(R.id.drawerLayout);
    if(dl.isDrawerOpen(GravityCompat.START))
        dl.closeDrawer(GravityCompat.START);

    return false;
}

What I would like now is that the fragment called in the button's click method (which is inside another fragment) overwrites that fragment itself. How to do this?

    
asked by anonymous 28.06.2016 / 15:40

2 answers

1

I would put in the fragment layout xml in the onClick event of the button, so it gets accessible to your main activity.

 <ImageButton
        android:id="@+id/img_lg_logon"
        android:layout_width="match_parent"
        android:layout_height="50sp"
        android:tint="@color/colorWhite"
        android:src="@drawable/ic_login"
        android:onClick="onLogon"/>

In Activity you create the event with public:

 public void onLogon(View view){
     if (savedInstanceState == null) {
        FragmentInicio fragInicio = new FragmentInicio();

        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.layoutFundo, fragInicio, "fragInicio");
        ft.commit();
     }
 }
    
28.06.2016 / 16:34
0

This is my class fragment

public class TrainingFragment extends Fragment implements View.OnClickListener{

ImageButton btCone;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_training, container, false);
   }

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    btCone = view.findViewById(R.id.image_button_fragment_edit_training_cone);

    btCone.setOnClickListener(this);


   }

@Override
public void onClick(View v) {
    if(v.getId() == btCone.getId()){
        Toast.makeText(getActivity(),"TESTEBUTTON",Toast.LENGTH_SHORT).show();

        FragmentManager fragmentTransaction = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fragmentTransaction.beginTransaction();

        TesteFragment testeFragment = new TesteFragment();

        transaction.replace(R.id.container_fragment, testeFragment);
        transaction.addToBackStack(null);

        transaction.commit();

    }
    }

}

So I can click the button and change the fragment within the fragment itself.

    
22.11.2018 / 14:29