Change Fragment by clicking on a button inside another Fragment

Asked

Viewed 2,215 times

4

I have a button inside my Ragment that I want it to call another Ragment that is over the current one when it is clicked. follows the code of 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.editarMinhasInformacoes);

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

    return (view);
}

Being that in the class that calls this Fragment, it was declared so that a navigationDrawer changes the Fragments within this 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 click method (which is inside another Fragment) overwrite(replace) that very Fragment. How to do this?

  • View this code and adapt :) http://answall.com/questions/137273/como-mudar-a-activity-dentro-de-um-fragment-com-button/137276#137276 If you notice anything from my answer say

2 answers

1

I would put in the layout xml of Fragment in the button onClick event, so it is 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"/>

and 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();
     }
 }
  • No, no, no, buddy. In this case, I need the method to be done in the Fragment itself and this is invoked by clicking a button that is inside it. but to create the onclick event inside Fragment I’m using java and not by xml understood?

0

That’s my fragment class

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 inside the fragment itself.

Browser other questions tagged

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