Go from one fragment to another

-1

I have a fragment and within the fr agment I have a recyclerview with a cardview which has a button, and they are located in fragment 1 and I want the button to pass to fragment 2, I declare it in the onBindViewHolder and I get the event from the click listener, but I can not find how to make it pass to the other fragment. Any help please take more than two weeks with this problem.

    
asked by Jorge Alexander Salcedo 14.09.2017 в 08:19
source

2 answers

1

In the code you have commented, change the .show() by

transaction.replace(R.id.id_del_fragment_del_layout, nuevoFragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

As you can see the documentation , the method .show() serves to show a Fragment , that has been hidden by the method .hide() of FragmentTransaction , in your case, what you need is to replace the Fragment , with the new Fragment of Tab_Mapas_Fragment() , the transition I've put that, but apart from being optional, you have in the link above, the different options.

The option of .replace() , will serve you if you do not need to maintain the status of the previous Fragment, in case you need to keep the previous Fragment , you should use the .remove() and .add() methods. That way when you change from Fragment , and you give the back button, it will return you to the Fragment previous in the last state that you left it.

((AppCompactActivity)context).getSupportFragmentManager()
    .beginTransaction()
    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
    .remove(referencia_al_fragment_a_reemplazar)
    .commit();
((AppCompactActivity)context).getSupportFragmentManager()
    .beginTransaction()
    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    .replace(R.id.contenedor_del_fragment, nuevoFragmento)
    .commit();
    
answered by 14.09.2017 в 12:17
0
Fragment fragmenthijo = new fragmentnuevo();
FragmentTransaction transaction = ((menuActivity) context).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_fragments, fragmenthijo ).commit();

You need to do it with replace, what you are going to do is replace the current fragment with a new one. But for this we need FrameLayout in the activity where it contains the fragments, every time we replace the fragment we tell it to replace it in the FrameLayout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container_fragments"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Sorry if I do not understand well, for days I do not play Android. Here you will find more information than I speak.

    
answered by 16.09.2017 в 19:16