Fragment screen to call another screen Fragment

Asked

Viewed 214 times

0

I’m in a fragment, and in that fragment I want to call another screen which is also a fragment. I’m using recycleview. If I try to call one activity, I can, but I can’t call a fragment.

See the code below:

@Override
public void onNoteClick(int position) {
    listaCursos.get(position);
   // aqui deve chamar outro fragment
}

I tried some examples I found, but does not open another screen that has the fragment. Just remembering that my app only has one activity, all else only fragment.

Calling the screen using Intent is not my case.

I tried something like this I found and it wasn’t:

Fragment fr = new Fragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container, fr);
fragmentTransaction.commit();

I will put here as ta my xml:

<?xml version="1.0" encoding="utf-8"?>

tools:context=".fragmentos.consultarCursoUrl">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="10dp"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CONSULTAR CURSO"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_marginBottom="20dp"
        android:textStyle="bold"
        />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"

        >





    <EditText
        android:id="@+id/codigo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Código"
        android:layout_weight=".5"
        />

    <ImageButton
        android:id="@+id/btnConsultar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_menu_search"
        android:background="@color/colorPrimaryDark"
        android:layout_weight="3"

        />

    </LinearLayout>

    <EditText
        android:id="@+id/txt_nome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="10dp"

        />

    <EditText
        android:id="@+id/txt_professor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="10dp"

        />


    <EditText
        android:id="@+id/txt_categoria"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_marginLeft="5dp"

        />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/imagemId"
    >

    <Button
        android:id="@+id/btnAtualizar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Atualizar"
        android:layout_weight="2"
        android:layout_marginRight="1dp"
        android:layout_marginTop="122dp"
        android:background="@color/colorPrimaryDark"
        android:textColor="@color/colorWhite"
        />

    <Button
        android:id="@+id/btnDeletar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Deletar"
        android:layout_weight="2"
        android:layout_marginRight="1dp"
        android:layout_marginTop="122dp"
        android:background="@color/colorPrimaryDark"
        android:textColor="@color/colorWhite"
        />


</LinearLayout>



 <ImageView
     android:id="@+id/imagemId"
     android:layout_width="250dp"
     android:layout_height="250dp"
     android:layout_below="@+id/linearLayout"
     android:src="@drawable/sem_foto"
     android:layout_centerHorizontal="true"
     />

1 answer

1


You can start another Fragment from the following:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FrgmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer,new Fragment2()).commit(); 

Note that R.id.fragmentContainer is your xml frameLayout and Fragment2() is the fragment you want to call;

  • I tried it your way, until the screen appears, but the items are superimposed with the previous screen. I go this way as ta.

  • Now that I’ve lost track of the container. I’ve made it. Thank you very much

Browser other questions tagged

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