Switch to Fragment by clicking the button inside the Fragment

Asked

Viewed 292 times

0

I have an Activity that has an empty Linearlayout that only serves to 'host' the Fragment. This Linearlayout (called layoutFundo) is the argument of Fragmenttransaction to which one of several separately created Fragments is 'hosted'. This happens when the click event of one of the items of a navigationDrawer added to the project is triggered. However, I need help in the following aspect: Inside one of these Ragments there is a button and I need this button, when clicked, to direct to another Ragment that has to be hosted in the same 'layoutFundo'. How can I perform such a procedure?

Follow code that calls Fragment to layoutFundo:

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

// Este método cria a instância da fragment e ao ser clicada faz um 'replace' para ela ir atrás da pilha

@Override
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;
}

In case the user clicks on nav_menu1 and layoutFundo hosts the fragmentMinhaConta will appear inside the Fragment a button, and by clicking this button (which is inside the fragmentMinhaConta) I need another Fragment (fragmentMinhaContent) is hosted by the layoutWorld instead of hosting the InhaConta fragment. This in case the user enters the fragmentMinhaConta wants to update its registration.

Follow the code of the fragmentMinhaContact with an event of the click of the button where it should call the other fragmentMinhaContact:

public class FragmentMinhaConta extends Fragment {

@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.btnEditarMinhasInformacoes);

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

public void mostrarMsg(String titulo, String mensagem) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setCancelable(true);
    builder.setTitle(titulo);
    builder.setMessage(mensagem);
    builder.show();
}

}

Instead of going into the show method SG, I want it to go into another method that overwrites that same Fragment by the fragmentMinhaConceptualize. How do I do that?

1 answer

0

I’ll write down what you have to do

Send your activity into the fragment, send the this

2nd It is a function in your activity where the fragment ex:

public void mudarFragmento(){
    //aqui dentro escreves o codigo que precisas, neste caso escreves o codigo para fazer o replace do fragmento
}

3º Control of the fragment, in butao Onclick() flames that function ex:

((NomeDaTuaAtividade)VariavelQueRecebesteComAAtividade).mudarFragmento();

Achob this should do the job :) If you don’t understand something says

Browser other questions tagged

You are not signed in. Login or sign up in order to post.