Calling screen with Fragment

Asked

Viewed 219 times

1

I created a screen with a list view based on Fragment native android studio, but I did not find solution to "call" this screen from a button.

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {

    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

1 answer

1

You need to use Fragmentmanager, example:

 FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, NomeDoFragmentAqui, "titulo do fragment")
                .commit();

And in your layout, you need to have Framelayout, which is where Fragment will be placed, example:

<FrameLayout
     android:id="@+id/container"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />
  • I did not understand where the Nomedofragment goes, because I put and the error.

  • Which error is presented? Give a confirmed if you are importing the v4 Fragment: import android.support.v4.app.Fragment;

  • I have a screen with fragment that is called Jogoscampofragment. And the error when I try to import that

  • But this Gameampofragment extends a Fragment?

  • Tries to change the getSupportFragmentManager for getFragmentManager

Browser other questions tagged

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