0
I’m trying to use the example of NavigationDrawer
android Studio. I even managed to understand the code, I remake it in my project, but I can’t change the Fragments when some menu item is triggered, Android Studio does not point any error in the code but Fragment is not called, Activity code:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stalking_app);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Toast.makeText(StalkingApp.this, item.getTitle(), Toast.LENGTH_LONG).show();
Fragment fragment = null;
Class fragmentClass;
fragmentClass = Fragment1.class;
if (id == R.id.nav_procurar) {
// Handle the camera action
fragmentClass = Fragment1.class;
} else if (id == R.id.nav_feed) {
fragmentClass = Fragment1.class;
} else if (id == R.id.nav_perfil) {
fragmentClass = Fragment1.class;
} else if (id == R.id.nav_fotos) {
fragmentClass = Fragment1.class;
} else if (id == R.id.nav_ferramentas) {
fragmentClass = Fragment1.class;
} else if (id == R.id.nav_sair) {
fragmentClass = Fragment1.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Code of my Fragment:
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return (RelativeLayout) inflater.inflate(R.layout.tab_layout_a, container, false);
}
}
My container:
<LinearLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
He doesn’t return even a mistake, someone can help me?
You are changing from one fragment to the same fragment. You will see no difference in switching from one fragment to the other.
– Caique Oliveira
but there is no "initial" Fragment, the empty container so when I call some Fragment was to display the layout of Fragment.
– Paulo Abreu