Change Android Fragments

1

Good people, I'm starting with Android Studio and I'm developing a small app and I made a menu, and I would like it when I click it, open another fragment. I tried several things already and nothing: /

The last one was this

FragmentManager fragmentManager = getFragmentManager();

    if (id == R.id.Aries)
    {
      fragmentManager.beginTransaction().replace(R.id.content_frame, new Aries())
         .commit();
    }

But where is "new Aries" I get the error "can not find symbol fragment_aries"

My app will be a horoscope, and then I'll connect to the online database.

Any help on this part too, thank you

Thank you

Attt

Lucas Soares

    
asked by anonymous 27.05.2016 / 23:50

3 answers

1

I will do this: First I will instantiate a listener for the menu items clicked.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //OUTRAS IMPLEMENTAÇÕES

    setContentView(R.layout.activity_main);mNavigationView = (NavigationView)findViewById(R.id.navigation_view);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            selecionarOpcaoMenu(menuItem);
            return true;
        }
    });
}

Then I implement the method that handles the click event.

private void selecionarOpcaoMenu(MenuItem menuItem){
    mOpcaoSelecionada = menuItem.getItemId();
    menuItem.setChecked(true);
    mDrawerLayout.closeDrawers();

    String titulo = menuItem.getTitle().toString();

    FragmentManager fm = getSupportFragmentManager();

    if(fm.findFragmentByTag(titulo) == null){
        Fragment primeiroNivelFragment = new Fragment();

        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.conteudo, primeiroNivelFragment, titulo)
                .commit();
    }
}

I hope I have helped you, I have an example of navigation with fragments if you want to take a look. Here

    
29.05.2016 / 17:47
1

To transition between the fragments in MainActivity with NavDrawer, I use the following:

   public void displayView(int viewId) {

        Fragment fragment = null;

        switch (viewId) {
            case R.id.fragment_1:
                fragment = new Fragment1();
                title  = "Fragment 1";
                break;
            case R.id.fragment_2:
                fragment = new Fragment2();
                title  = "Fragment 2";
                break;

        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment);
            ft.commit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);

    }

I hope I have helped. You can view more here .

    
28.05.2016 / 23:29
0

For a new Activity:

Create a new Activity with the name of MainActivity2 and enter this code below in your onNavigationItemSelected method of your MainActivity :

Intent intent = new Intent(this, MainActivity2.class); 
startActivity(intent);

For a new Fragment:

Create a new Fragment with the name of sobreFragment and enter this code below in your onNavigationItemSelected method of your MainActivity :

sobreFragment fragment = new sobreFragment();
android.support.v4.app.FragmentTransaction fragmentTrasaction =
getSupportFragmentManager().beginTransaction();
fragmentTrasaction.replace(R.id.fragment_container, fragment);
fragmentTrasaction.commit();
    
28.05.2016 / 00:04